I have a problem with my flutter code. In my app I have a page when I open it, if the location of my device is disabled, I display a page 'your location is disabled' with a button to access the location settings. My problem is that if I go to the location setting and activate and go back to my app, my app doesn't detect the change. I tried with Navigator.of(context).pop()
but I think it doesn't exactly solve the problem.
Note: I use the app_settings
package to access the device settings and geolocator
package to know the location of my users
The function that checks the location service
// Verify location permission
_permission() async {
bool serviceEnabled;
LocationPermission permission;
// Test if location services are enabled.
serviceEnabled = await Geolocator.isLocationServiceEnabled();
if (!serviceEnabled) {
// this is the page I display when localization is disabled
Navigator.of(context).push(MaterialPageRoute(
builder: (context) => const DisabledLocationScreen()));
}
permission = await Geolocator.checkPermission();
if (permission == LocationPermission.denied) {
permission = await Geolocator.requestPermission();
if (permission == LocationPermission.denied) {
return 'Location permissions are denied';
}
}
if (permission == LocationPermission.deniedForever) {
// Permissions are denied forever, handle appropriately.
return 'Location permissions are permanently denied, we cannot request permissions.';
}
return true;
}
Here the page displayed and which allows access to the parameters of the device
import 'package:flutter/material.dart';
import 'package:app_settings/app_settings.dart';
class DisabledLocationScreen extends StatelessWidget {
const DisabledLocationScreen({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return Scaffold(
body: SafeArea(
child: Padding(
padding: const EdgeInsets.all(15.0),
child: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: [
Container(
child: const Icon(
Icons.location_on,
color: Color.fromARGB(255, 20, 174, 92),
size: 150.0,
),
),
Column(children: [
const Text(
'Your Location is disabled',
style: TextStyle(
fontWeight: FontWeight.w700,
fontSize: 30,
),
),
const SizedBox(
height: 10.0,
),
const Text(
'Activate your location to be able to meet other people near you.'),
]),
ElevatedButton(
style: ElevatedButton.styleFrom(
elevation: 0,
side: BorderSide(
color: Colors.grey[800]!,
width: 0.5,
),
foregroundColor: Colors.grey[900],
backgroundColor: Colors.white),
onPressed: (() {
AppSettings.openLocationSettings(callback: () {
// Navigator.of(context).pop();
});
}),
child: const Text('Access location settings'),
),
],
),
),
),
),
);
}
}
```