From d323194ea74d1135d955dcfe3f5ead15038d649c Mon Sep 17 00:00:00 2001 From: Remy Moll Date: Tue, 24 Sep 2024 23:48:07 +0200 Subject: [PATCH] Better location handling on map --- frontend/lib/constants.dart | 13 +--- frontend/lib/modules/current_trip_map.dart | 31 ++++++-- frontend/lib/modules/new_trip_map.dart | 22 +++++- frontend/lib/pages/current_trip.dart | 4 +- frontend/lib/pages/new_trip_preferences.dart | 4 +- frontend/lib/pages/settings.dart | 75 +++++++++++--------- 6 files changed, 96 insertions(+), 53 deletions(-) diff --git a/frontend/lib/constants.dart b/frontend/lib/constants.dart index 3320062..7856578 100644 --- a/frontend/lib/constants.dart +++ b/frontend/lib/constants.dart @@ -34,26 +34,19 @@ ThemeData APP_THEME = ThemeData( textButtonTheme: TextButtonThemeData( + style: TextButton.styleFrom( backgroundColor: PRIMARY_COLOR, textStyle: TextStyle( - color: Colors.black, - ), + color: Colors.red + ) ), ), - - iconButtonTheme: IconButtonThemeData( - style: ButtonStyle( - backgroundColor: MaterialStateProperty.all(PRIMARY_COLOR), - ) - ), - buttonTheme: ButtonThemeData( buttonColor: PRIMARY_COLOR, textTheme: ButtonTextTheme.primary, ), - ); diff --git a/frontend/lib/modules/current_trip_map.dart b/frontend/lib/modules/current_trip_map.dart index 6ce4b4e..3c770f3 100644 --- a/frontend/lib/modules/current_trip_map.dart +++ b/frontend/lib/modules/current_trip_map.dart @@ -6,22 +6,23 @@ import 'package:flutter/material.dart'; import 'package:anyway/structs/landmark.dart'; import 'package:anyway/structs/trip.dart'; import 'package:google_maps_flutter/google_maps_flutter.dart'; +import 'package:shared_preferences/shared_preferences.dart'; import 'package:widget_to_marker/widget_to_marker.dart'; -class MapWidget extends StatefulWidget { +class CurrentTripMap extends StatefulWidget { final Trip? trip; - MapWidget({ + CurrentTripMap({ this.trip }); @override - State createState() => _MapWidgetState(); + State createState() => _CurrentTripMapState(); } -class _MapWidgetState extends State { +class _CurrentTripMapState extends State { late GoogleMapController mapController; CameraPosition _cameraPosition = CameraPosition( @@ -67,9 +68,27 @@ class _MapWidgetState extends State { }); } + @override Widget build(BuildContext context) { widget.trip?.addListener(setMapMarkers); + Future preferences = SharedPreferences.getInstance(); + + return FutureBuilder( + future: preferences, + builder: (context, snapshot) { + if (snapshot.hasData) { + SharedPreferences prefs = snapshot.data as SharedPreferences; + bool useLocation = prefs.getBool('useLocation') ?? true; + return _buildMap(useLocation); + } else { + return const CircularProgressIndicator(); + } + } + ); + } + + Widget _buildMap(bool useLocation) { return GoogleMap( onMapCreated: _onMapCreated, initialCameraPosition: _cameraPosition, @@ -79,7 +98,9 @@ class _MapWidgetState extends State { cloudMapId: MAP_ID, mapToolbarEnabled: false, zoomControlsEnabled: false, - myLocationEnabled: true, + myLocationEnabled: useLocation, + myLocationButtonEnabled: false, ); } + } diff --git a/frontend/lib/modules/new_trip_map.dart b/frontend/lib/modules/new_trip_map.dart index 2966454..9c6c2cd 100644 --- a/frontend/lib/modules/new_trip_map.dart +++ b/frontend/lib/modules/new_trip_map.dart @@ -7,6 +7,7 @@ import 'package:anyway/structs/landmark.dart'; import 'package:anyway/structs/trip.dart'; import 'package:flutter/material.dart'; import 'package:google_maps_flutter/google_maps_flutter.dart'; +import 'package:shared_preferences/shared_preferences.dart'; import 'package:widget_to_marker/widget_to_marker.dart'; @@ -72,6 +73,23 @@ class _NewTripMapState extends State { @override Widget build(BuildContext context) { widget.trip.addListener(updateTripDetails); + Future preferences = SharedPreferences.getInstance(); + + return FutureBuilder( + future: preferences, + builder: (context, snapshot) { + if (snapshot.hasData) { + SharedPreferences prefs = snapshot.data as SharedPreferences; + bool useLocation = prefs.getBool('useLocation') ?? true; + return _buildMap(useLocation); + } else { + return const CircularProgressIndicator(); + } + } + ); + } + + Widget _buildMap(bool useLocation) { return GoogleMap( onMapCreated: _onMapCreated, initialCameraPosition: _cameraPosition, @@ -80,8 +98,8 @@ class _NewTripMapState extends State { cloudMapId: MAP_ID, mapToolbarEnabled: false, zoomControlsEnabled: false, - // TODO: should be loaded from the sharedprefs - myLocationEnabled: true, + myLocationButtonEnabled: false, + myLocationEnabled: useLocation, ); } } \ No newline at end of file diff --git a/frontend/lib/pages/current_trip.dart b/frontend/lib/pages/current_trip.dart index 793f22f..3e3c57e 100644 --- a/frontend/lib/pages/current_trip.dart +++ b/frontend/lib/pages/current_trip.dart @@ -29,14 +29,14 @@ class _TripPageState extends State { return SlidingUpPanel( panelBuilder: (sc) => _panelFull(sc), // collapsed: _floatingCollapsed(), - body: MapWidget(trip: widget.trip), + body: CurrentTripMap(trip: widget.trip), // renderPanelSheet: false, // backdropEnabled: true, maxHeight: MediaQuery.of(context).size.height * 0.8, padding: EdgeInsets.only(left: 10, right: 10, top: 25, bottom: 10), // panelSnapping: false, borderRadius: BorderRadius.only(topLeft: Radius.circular(25), topRight: Radius.circular(25)), - boxShadow: [ + boxShadow: const [ BoxShadow( blurRadius: 20.0, color: Colors.black, diff --git a/frontend/lib/pages/new_trip_preferences.dart b/frontend/lib/pages/new_trip_preferences.dart index a41b140..5fb7640 100644 --- a/frontend/lib/pages/new_trip_preferences.dart +++ b/frontend/lib/pages/new_trip_preferences.dart @@ -84,6 +84,8 @@ class _NewTripPreferencesPageState extends State { for (SinglePreference pref in prefs) { sliders.add( Card( + margin: const EdgeInsets.only(left: 10, right: 10, top: 10, bottom: 0), + shadowColor: Colors.grey, child: ListTile( leading: pref.icon, title: Text(pref.name), @@ -100,8 +102,6 @@ class _NewTripPreferencesPageState extends State { }, ) ), - margin: const EdgeInsets.only(left: 10, right: 10, top: 10, bottom: 0), - shadowColor: Colors.grey, ) ); } diff --git a/frontend/lib/pages/settings.dart b/frontend/lib/pages/settings.dart index ba4297d..4cb0248 100644 --- a/frontend/lib/pages/settings.dart +++ b/frontend/lib/pages/settings.dart @@ -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 { - @override Widget build(BuildContext context) { return ListView( @@ -90,6 +88,7 @@ class _SettingsPageState extends State { ], ); } + Widget darkMode() { return Row( children: [ @@ -115,46 +114,58 @@ class _SettingsPageState extends State { ], ); } + Widget setLocationUsage() { + Future 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 { 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)); }