@@ -10,6 +10,8 @@ import 'package:flutter_bloc/flutter_bloc.dart';
1010import 'package:flutter_weather/src/widgets/weather_widget.dart' ;
1111import 'package:http/http.dart' as http;
1212import 'package:intl/intl.dart' ;
13+ import 'package:geolocator/geolocator.dart' ;
14+ import 'package:permission_handler/permission_handler.dart' ;
1315
1416enum OptionsMenu { changeCity, settings }
1517
@@ -32,7 +34,9 @@ class _WeatherScreenState extends State<WeatherScreen>
3234 void initState () {
3335 super .initState ();
3436 _weatherBloc = WeatherBloc (weatherRepository: widget.weatherRepository);
35- _weatherBloc.dispatch (FetchWeather (cityName: _cityName));
37+ _fetchWeatherWithLocation ().catchError ((error) {
38+ _fetchWeatherWithCity ();
39+ });
3640 _fadeController = AnimationController (
3741 duration: const Duration (milliseconds: 1000 ), vsync: this );
3842 _fadeAnimation =
@@ -49,7 +53,7 @@ class _WeatherScreenState extends State<WeatherScreen>
4953 mainAxisAlignment: MainAxisAlignment .center,
5054 children: < Widget > [
5155 Text (
52- DateFormat ('EEEE, MMMM yyyy' ).format (DateTime .now ()),
56+ DateFormat ('EEEE, d MMMM yyyy' ).format (DateTime .now ()),
5357 style: TextStyle (
5458 color: AppStateContainer .of (context)
5559 .theme
@@ -90,6 +94,7 @@ class _WeatherScreenState extends State<WeatherScreen>
9094 bloc: _weatherBloc,
9195 builder: (_, WeatherState weatherState) {
9296 if (weatherState is WeatherLoaded ) {
97+ this ._cityName = weatherState.weather.cityName;
9398 _fadeController.reset ();
9499 _fadeController.forward ();
95100 return WeatherWidget (
@@ -131,7 +136,7 @@ class _WeatherScreenState extends State<WeatherScreen>
131136 .theme
132137 .accentColor),
133138 ),
134- onPressed: _fetchWeather ,
139+ onPressed: _fetchWeatherWithCity ,
135140 )
136141 ],
137142 );
@@ -161,24 +166,37 @@ class _WeatherScreenState extends State<WeatherScreen>
161166 FlatButton (
162167 child: Text (
163168 'ok' ,
164- style: TextStyle (color: Colors .black),
169+ style: TextStyle (color: Colors .black, fontSize : 16 ),
165170 ),
166171 onPressed: () {
167- _fetchWeather ();
172+ _fetchWeatherWithCity ();
168173 Navigator .of (context).pop ();
169174 },
170- )
175+ ),
171176 ],
172177 content: TextField (
173178 autofocus: true ,
174179 onChanged: (text) {
175180 _cityName = text;
176181 },
177182 decoration: InputDecoration (
178- hintText: 'Enter the name of your city' ,
179- hintStyle: TextStyle (color: Colors .black),
180- ),
183+ hintText: 'Name of your city' ,
184+ hintStyle: TextStyle (color: Colors .black),
185+ suffixIcon: GestureDetector (
186+ onTap: () {
187+ _fetchWeatherWithLocation ().catchError ((error) {
188+ _fetchWeatherWithCity ();
189+ });
190+ Navigator .of (context).pop ();
191+ },
192+ child: Icon (
193+ Icons .my_location,
194+ color: Colors .black,
195+ size: 16 ,
196+ ),
197+ )),
181198 style: TextStyle (color: Colors .black),
199+ cursorColor: Colors .black,
182200 ),
183201 );
184202 });
@@ -195,7 +213,52 @@ class _WeatherScreenState extends State<WeatherScreen>
195213 }
196214 }
197215
198- _fetchWeather () {
216+ _fetchWeatherWithCity () {
199217 _weatherBloc.dispatch (FetchWeather (cityName: _cityName));
200218 }
219+
220+ _fetchWeatherWithLocation () async {
221+ var permissionHandler = PermissionHandler ();
222+ var permissionResult = await permissionHandler
223+ .requestPermissions ([PermissionGroup .locationWhenInUse]);
224+
225+ switch (permissionResult[PermissionGroup .locationWhenInUse]) {
226+ case PermissionStatus .denied:
227+ case PermissionStatus .unknown:
228+ print ('location permission denied' );
229+ _showLocationDeniedDialog (permissionHandler);
230+ throw Error ();
231+ }
232+
233+ Position position = await Geolocator ()
234+ .getCurrentPosition (desiredAccuracy: LocationAccuracy .low);
235+ print (position);
236+ _weatherBloc.dispatch (FetchWeather (
237+ longitude: position.longitude, latitude: position.latitude));
238+ }
239+
240+ void _showLocationDeniedDialog (PermissionHandler permissionHandler) {
241+ showDialog (
242+ context: context,
243+ barrierDismissible: true ,
244+ builder: (BuildContext context) {
245+ return AlertDialog (
246+ backgroundColor: Colors .white,
247+ title: Text ('Location is disabled :(' ,
248+ style: TextStyle (color: Colors .black)),
249+ actions: < Widget > [
250+ FlatButton (
251+ child: Text (
252+ 'Enable!' ,
253+ style: TextStyle (color: Colors .green, fontSize: 16 ),
254+ ),
255+ onPressed: () {
256+ permissionHandler.openAppSettings ().then ((val) => print (val));
257+ Navigator .of (context).pop ();
258+ },
259+ ),
260+ ],
261+ );
262+ });
263+ }
201264}
0 commit comments