From f693ffa6b86b6e7925f93b23168262cc2e4fa384 Mon Sep 17 00:00:00 2001 From: Valentin CZERYBA Date: Sat, 23 Nov 2024 09:56:37 +0100 Subject: [PATCH] merge commit --- covas_mobile/lib/pages/ListItemMenu.dart | 42 ++++++++++++++---------- 1 file changed, 25 insertions(+), 17 deletions(-) diff --git a/covas_mobile/lib/pages/ListItemMenu.dart b/covas_mobile/lib/pages/ListItemMenu.dart index f0f581c..e29ba66 100644 --- a/covas_mobile/lib/pages/ListItemMenu.dart +++ b/covas_mobile/lib/pages/ListItemMenu.dart @@ -137,8 +137,6 @@ class _MyHomePageState extends State { super.initState(); // Initialize data fetch when the page loads _getCurrentLocation(); - - Datepicker.text = DateFormat("dd-MM-yyyy").format(new DateTime.now()); } // Get the device's current location @@ -149,19 +147,32 @@ class _MyHomePageState extends State { print("Location permission granted"); // Get the current position with high accuracy - LocationSettings locationSettings = LocationSettings( - accuracy: LocationAccuracy.high, - distanceFilter: - 10, // Optional: Minimum distance (in meters) to trigger location update - ); - - Position position = await Geolocator.getCurrentPosition( - locationSettings: locationSettings, - ); + const LocationSettings locationSettings = LocationSettings( + accuracy: LocationAccuracy.medium, timeLimit: Duration(seconds: 5)); + Position? position; + try { + position = await Geolocator.getCurrentPosition( + locationSettings: locationSettings); + } on LocationServiceDisabledException { + // Handle location services disabled + print('Location services are disabled.'); + position = await Geolocator.getLastKnownPosition(); + if (position == null) { + print('No last known position available.'); + } + } catch (e) { + // Handle other errors + print('Failed to get location: $e'); + position = await Geolocator.getLastKnownPosition(); + if (position == null) { + print('No last known position available.'); + } + } // Reverse geocode: Get city and country from latitude and longitude using Mapbox Search API - final place = - await _getCityAndCountry(position.latitude, position.longitude); + if (position != null) { + _getCityAndCountry(position!.latitude, position!.longitude); + } } } @@ -321,10 +332,7 @@ class _MyHomePageState extends State { DateTime dateEvent = DateTime.now(); DateTime? pickedDate = await showDatePicker( - context: context, - firstDate: dateEvent, - initialDate: dateEvent, - lastDate: DateTime(2104)); + context: context, firstDate: dateEvent, lastDate: DateTime(2104)); if (pickedDate == null) return; Datepicker.text = DateFormat("dd-MM-yyyy").format(pickedDate); }