Skip to content

Commit be9c366

Browse files
committed
fix location permission flow
1 parent 8ad56db commit be9c366

File tree

1 file changed

+51
-39
lines changed

1 file changed

+51
-39
lines changed

lib/src/screens/weather_screen.dart

Lines changed: 51 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@ import 'package:flutter_weather/src/bloc/weather_event.dart';
44
import 'package:flutter_weather/src/bloc/weather_state.dart';
55
import 'package:flutter_bloc/flutter_bloc.dart';
66
import 'package:flutter_weather/src/widgets/weather_widget.dart';
7+
import 'package:geolocator/geolocator.dart';
78
import 'package:intl/intl.dart';
9+
import 'package:permission_handler/permission_handler.dart';
810

911
import '../bloc/weather_bloc.dart';
1012

@@ -20,11 +22,14 @@ class _WeatherScreenState extends State<WeatherScreen>
2022
String _cityName = 'bengaluru';
2123
AnimationController _fadeController;
2224
Animation<double> _fadeAnimation;
25+
WeatherBloc _weatherBloc;
2326

2427
@override
2528
void initState() {
2629
super.initState();
2730

31+
_weatherBloc = BlocProvider.of<WeatherBloc>(context);
32+
2833
_fetchWeatherWithLocation().catchError((error) {
2934
_fetchWeatherWithCity();
3035
});
@@ -210,52 +215,59 @@ class _WeatherScreenState extends State<WeatherScreen>
210215
}
211216

212217
_fetchWeatherWithCity() {
213-
BlocProvider.of<WeatherBloc>(context)
214-
.add(FetchWeather(cityName: _cityName));
218+
_weatherBloc.add(FetchWeather(cityName: _cityName));
215219
}
216220

217221
_fetchWeatherWithLocation() async {
218-
// var permissionHandler = PermissionHandler();
219-
// var permissionResult = await permissionHandler
220-
// .requestPermissions([PermissionGroup.locationWhenInUse]);
222+
var permissionResult = await Permission.locationWhenInUse.status;
223+
224+
switch (permissionResult) {
225+
case PermissionStatus.restricted:
226+
case PermissionStatus.permanentlyDenied:
227+
print('location permission denied');
228+
_showLocationDeniedDialog();
229+
break;
221230

222-
// switch (permissionResult[PermissionGroup.locationWhenInUse]) {
223-
// case PermissionStatus.denied:
224-
// case PermissionStatus.unknown:
225-
// print('location permission denied');
226-
// _showLocationDeniedDialog(permissionHandler);
227-
// throw Error();
228-
// }
231+
case PermissionStatus.denied:
232+
await Permission.locationWhenInUse.request();
233+
_fetchWeatherWithLocation();
234+
break;
229235

230-
// Position position = await Geolocator()
231-
// .getCurrentPosition(desiredAccuracy: LocationAccuracy.low);
232-
// _weatherBloc.dispatch(FetchWeather(
233-
// longitude: position.longitude, latitude: position.latitude));
236+
case PermissionStatus.limited:
237+
case PermissionStatus.granted:
238+
Position position = await Geolocator.getCurrentPosition(
239+
desiredAccuracy: LocationAccuracy.low);
240+
241+
_weatherBloc.add(FetchWeather(
242+
longitude: position.longitude,
243+
latitude: position.latitude,
244+
));
245+
break;
246+
}
234247
}
235248

236249
void _showLocationDeniedDialog() {
237-
//(PermissionHandler permissionHandler) {
238-
// showDialog(
239-
// context: context,
240-
// barrierDismissible: true,
241-
// builder: (BuildContext context) {
242-
// return AlertDialog(
243-
// backgroundColor: Colors.white,
244-
// title: Text('Location is disabled :(',
245-
// style: TextStyle(color: Colors.black)),
246-
// actions: <Widget>[
247-
// FlatButton(
248-
// child: Text(
249-
// 'Enable!',
250-
// style: TextStyle(color: Colors.green, fontSize: 16),
251-
// ),
252-
// onPressed: () {
253-
// permissionHandler.openAppSettings();
254-
// Navigator.of(context).pop();
255-
// },
256-
// ),
257-
// ],
258-
// );
259-
// });
250+
showDialog(
251+
context: context,
252+
barrierDismissible: true,
253+
builder: (BuildContext context) {
254+
return AlertDialog(
255+
backgroundColor: Colors.white,
256+
title: Text('Location is disabled :(',
257+
style: TextStyle(color: Colors.black)),
258+
actions: <Widget>[
259+
FlatButton(
260+
child: Text(
261+
'Enable!',
262+
style: TextStyle(color: Colors.green, fontSize: 16),
263+
),
264+
onPressed: () {
265+
openAppSettings();
266+
Navigator.of(context).pop();
267+
},
268+
),
269+
],
270+
);
271+
});
260272
}
261273
}

0 commit comments

Comments
 (0)