Better location handling on map

This commit is contained in:
2024-09-24 23:48:07 +02:00
parent ed60fcba06
commit d323194ea7
6 changed files with 96 additions and 53 deletions

View File

@@ -7,7 +7,6 @@ import 'package:url_launcher/url_launcher.dart';
bool debugMode = false;
bool useLocation = false;
class SettingsPage extends StatefulWidget {
@override
@@ -15,7 +14,6 @@ class SettingsPage extends StatefulWidget {
}
class _SettingsPageState extends State<SettingsPage> {
@override
Widget build(BuildContext context) {
return ListView(
@@ -90,6 +88,7 @@ class _SettingsPageState extends State<SettingsPage> {
],
);
}
Widget darkMode() {
return Row(
children: [
@@ -115,46 +114,58 @@ class _SettingsPageState extends State<SettingsPage> {
],
);
}
Widget setLocationUsage() {
Future<SharedPreferences> preferences = SharedPreferences.getInstance();
return Row(
children: [
Text('Use location services'),
// white space
Spacer(),
Switch(
value: useLocation,
onChanged: (bool? newValue) async {
await Permission.locationWhenInUse
.onDeniedCallback(() {
rootScaffoldMessengerKey.currentState!.showSnackBar(
SnackBar(content: Text('Location services are required for this feature'))
);
})
.onGrantedCallback(() {
rootScaffoldMessengerKey.currentState!.showSnackBar(
SnackBar(content: Text('Location services are now enabled'))
);
setState(() {
useLocation = newValue!;
});
SharedPreferences.getInstance().then(
(SharedPreferences prefs) {
prefs.setBool('useLocation', useLocation);
}
);
})
.onPermanentlyDeniedCallback(() {
rootScaffoldMessengerKey.currentState!.showSnackBar(
SnackBar(content: Text('Location services are required for this feature'))
);
})
.request();
FutureBuilder(
future: preferences,
builder: (context, snapshot) {
if (snapshot.hasData) {
bool useLocation = snapshot.data!.getBool('useLocation') ?? false;
return Switch(
value: useLocation,
onChanged: setUseLocation,
);
} else {
return CircularProgressIndicator();
}
}
)
],
);
}
void setUseLocation(bool newValue) async {
await Permission.locationWhenInUse
.onDeniedCallback(() {
rootScaffoldMessengerKey.currentState!.showSnackBar(
SnackBar(content: Text('Location services are required for this feature'))
);
})
.onGrantedCallback(() {
rootScaffoldMessengerKey.currentState!.showSnackBar(
SnackBar(content: Text('Location services are now enabled'))
);
SharedPreferences.getInstance().then(
(SharedPreferences prefs) {
setState(() {
prefs.setBool('useLocation', newValue);
});
}
);
})
.onPermanentlyDeniedCallback(() {
rootScaffoldMessengerKey.currentState!.showSnackBar(
SnackBar(content: Text('Location services are required for this feature'))
);
})
.request();
}
Widget privacyInfo() {
return Center(
@@ -163,8 +174,8 @@ class _SettingsPageState extends State<SettingsPage> {
Text('Our privacy policy is available under:'),
TextButton.icon(
icon: Icon(Icons.info),
label: Text(PRIVACY_URL),
icon: Icon(Icons.info, color: Colors.white),
label: Text(PRIVACY_URL, style: TextStyle(color: Colors.white)),
onPressed: () async{
await launchUrl(Uri.parse(PRIVACY_URL));
}