@@ -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 =
@@ -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 );
@@ -158,16 +163,28 @@ class _WeatherScreenState extends State<WeatherScreen>
158163 backgroundColor: Colors .white,
159164 title: Text ('Change city' , style: TextStyle (color: Colors .black)),
160165 actions: < Widget > [
166+ FlatButton (
167+ child: Icon (
168+ Icons .my_location,
169+ color: Colors .black,
170+ size: 16 ,
171+ ),
172+ onPressed: () {
173+ _fetchWeatherWithLocation ().catchError ((error) {
174+ _fetchWeatherWithCity ();
175+ });
176+ Navigator .of (context).pop ();
177+ }),
161178 FlatButton (
162179 child: Text (
163180 'ok' ,
164- style: TextStyle (color: Colors .black),
181+ style: TextStyle (color: Colors .black, fontSize : 16 ),
165182 ),
166183 onPressed: () {
167- _fetchWeather ();
184+ _fetchWeatherWithCity ();
168185 Navigator .of (context).pop ();
169186 },
170- )
187+ ),
171188 ],
172189 content: TextField (
173190 autofocus: true ,
@@ -195,7 +212,52 @@ class _WeatherScreenState extends State<WeatherScreen>
195212 }
196213 }
197214
198- _fetchWeather () {
215+ _fetchWeatherWithCity () {
199216 _weatherBloc.dispatch (FetchWeather (cityName: _cityName));
200217 }
218+
219+ _fetchWeatherWithLocation () async {
220+ var permissionHandler = PermissionHandler ();
221+ var permissionResult = await permissionHandler
222+ .requestPermissions ([PermissionGroup .locationWhenInUse]);
223+
224+ switch (permissionResult[PermissionGroup .locationWhenInUse]) {
225+ case PermissionStatus .denied:
226+ case PermissionStatus .unknown:
227+ print ('location permission denied' );
228+ _showLocationDeniedDialog (permissionHandler);
229+ throw Error ();
230+ }
231+
232+ Position position = await Geolocator ()
233+ .getCurrentPosition (desiredAccuracy: LocationAccuracy .low);
234+ print (position);
235+ _weatherBloc.dispatch (FetchWeather (
236+ longitude: position.longitude, latitude: position.latitude));
237+ }
238+
239+ void _showLocationDeniedDialog (PermissionHandler permissionHandler) {
240+ showDialog (
241+ context: context,
242+ barrierDismissible: true ,
243+ builder: (BuildContext context) {
244+ return AlertDialog (
245+ backgroundColor: Colors .white,
246+ title: Text ('Location is disabled :(' ,
247+ style: TextStyle (color: Colors .black)),
248+ actions: < Widget > [
249+ FlatButton (
250+ child: Text (
251+ 'Enable!' ,
252+ style: TextStyle (color: Colors .green, fontSize: 16 ),
253+ ),
254+ onPressed: () {
255+ permissionHandler.openAppSettings ().then ((val) => print (val));
256+ Navigator .of (context).pop ();
257+ },
258+ ),
259+ ],
260+ );
261+ });
262+ }
201263}
0 commit comments