diff --git a/android/app/src/main/AndroidManifest.xml b/android/app/src/main/AndroidManifest.xml index dbb772ca4a24f998d8af6f03bae333f1ba460e4b..0985d1b55cc90a335ac134d7ab43bb63ffdd9f84 100644 --- a/android/app/src/main/AndroidManifest.xml +++ b/android/app/src/main/AndroidManifest.xml @@ -1,5 +1,6 @@ <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.upsmf.beta"> + <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> <application android:label="UP SMF" android:usesCleartextTraffic="true" diff --git a/lib/pages/inspection_summary.dart b/lib/pages/inspection_summary.dart index ae73853f1bc889e3056094922f9848b9f1a3a741..6fa260a7329e009b3651714d50a77e58662f368c 100644 --- a/lib/pages/inspection_summary.dart +++ b/lib/pages/inspection_summary.dart @@ -7,6 +7,8 @@ import 'package:smf_mobile/constants/color_constants.dart'; // import 'package:smf_mobile/models/form_model.dart'; import 'package:smf_mobile/pages/login_email_page.dart'; import 'package:smf_mobile/repositories/application_repository.dart'; +import 'package:geolocator/geolocator.dart'; +import 'package:smf_mobile/services/location_service.dart'; // import 'package:smf_mobile/repositories/form_repository.dart'; import 'package:smf_mobile/util/helper.dart'; import 'package:smf_mobile/widgets/people_card.dart'; @@ -126,7 +128,16 @@ class _InspectionSummaryPageState extends State<InspectionSummaryPage> { } Future<void> _submitInspection() async { + Position position; bool isInternetConnected = await Helper.isInternetConnected(); + + try { + position = await LocationService.getCurrentPosition(); + } catch (error) { + Helper.toastMessage(error.toString()); + return; + } + // await Future.delayed(const Duration(milliseconds: 10)); if (isInternetConnected) { _validateUser(); diff --git a/lib/services/location_service.dart b/lib/services/location_service.dart new file mode 100644 index 0000000000000000000000000000000000000000..9fdf290e6a9afe52833302a07d27e80378955a46 --- /dev/null +++ b/lib/services/location_service.dart @@ -0,0 +1,47 @@ +import 'package:geolocator/geolocator.dart'; +import 'package:smf_mobile/util/helper.dart'; + +class LocationService { + /// Determine the current position of the device. + /// + /// When the location services are not enabled or permissions + /// are denied the `Future` will return an error. + static Future<Position> getCurrentPosition() async { + bool serviceEnabled; + LocationPermission permission; + + // Test if location services are enabled. + serviceEnabled = await Geolocator.isLocationServiceEnabled(); + permission = await Geolocator.checkPermission(); + if (!serviceEnabled) { + // Location services are not enabled don't continue + // accessing the position and request users of the + // App to enable the location services. + Helper.toastMessage( + "Location service is turned off on the device, turn on to submit the form"); + permission = await Geolocator.requestPermission(); + } + + if (permission == LocationPermission.denied) { + // permission = await Geolocator.requestPermission(); + // Permissions are denied, next time you could try + // requesting permissions again (this is also where + // Android's shouldShowRequestPermissionRationale + // returned true. According to Android guidelines + // your App should show an explanatory UI now. + return Future.error( + 'Location permissions are denied, turn on location to submit the form'); + } + + if (permission == LocationPermission.deniedForever) { + // Permissions are denied forever, handle appropriately. + return Future.error( + 'Location permissions are permanently denied, turn on location to submit the form'); + } + + // When we reach here, permissions are granted and we can + // continue accessing the position of the device. + return await Geolocator.getCurrentPosition( + desiredAccuracy: LocationAccuracy.high); + } +} diff --git a/pubspec.lock b/pubspec.lock index a69edf7a62b18d35071e6ddbb52e58498ae94836..06ceb299bc8e8a772d589b80eeb8717967497e2f 100644 --- a/pubspec.lock +++ b/pubspec.lock @@ -371,6 +371,48 @@ packages: url: "https://pub.dartlang.org" source: hosted version: "8.1.1" + geolocator: + dependency: "direct main" + description: + name: geolocator + url: "https://pub.dartlang.org" + source: hosted + version: "9.0.2" + geolocator_android: + dependency: transitive + description: + name: geolocator_android + url: "https://pub.dartlang.org" + source: hosted + version: "4.1.4" + geolocator_apple: + dependency: transitive + description: + name: geolocator_apple + url: "https://pub.dartlang.org" + source: hosted + version: "2.2.3" + geolocator_platform_interface: + dependency: transitive + description: + name: geolocator_platform_interface + url: "https://pub.dartlang.org" + source: hosted + version: "4.0.7" + geolocator_web: + dependency: transitive + description: + name: geolocator_web + url: "https://pub.dartlang.org" + source: hosted + version: "2.1.6" + geolocator_windows: + dependency: transitive + description: + name: geolocator_windows + url: "https://pub.dartlang.org" + source: hosted + version: "0.1.1" google_fonts: dependency: "direct main" description: diff --git a/pubspec.yaml b/pubspec.yaml index 9b07b00dfd3bccd3cfbeecf3c37ee54eb6933fe7..d6a260c5ebae5996de1aac0557ed63adb59c79ff 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -56,6 +56,7 @@ dependencies: image_cropper: ^3.0.1 dio: ^4.0.4 webview_flutter: ^3.0.1 + geolocator: ^9.0.2 dev_dependencies: flutter_test: diff --git a/windows/flutter/generated_plugin_registrant.cc b/windows/flutter/generated_plugin_registrant.cc index 9993ebde9a80b7d5da1db8e490a03da6b197ab0c..30847a358bba7d1367b18dd2fc7bb291e08dced5 100644 --- a/windows/flutter/generated_plugin_registrant.cc +++ b/windows/flutter/generated_plugin_registrant.cc @@ -8,10 +8,13 @@ #include <connectivity_plus_windows/connectivity_plus_windows_plugin.h> #include <flutter_secure_storage_windows/flutter_secure_storage_windows_plugin.h> +#include <geolocator_windows/geolocator_windows.h> void RegisterPlugins(flutter::PluginRegistry* registry) { ConnectivityPlusWindowsPluginRegisterWithRegistrar( registry->GetRegistrarForPlugin("ConnectivityPlusWindowsPlugin")); FlutterSecureStorageWindowsPluginRegisterWithRegistrar( registry->GetRegistrarForPlugin("FlutterSecureStorageWindowsPlugin")); + GeolocatorWindowsRegisterWithRegistrar( + registry->GetRegistrarForPlugin("GeolocatorWindows")); } diff --git a/windows/flutter/generated_plugins.cmake b/windows/flutter/generated_plugins.cmake index d50a7cab5bd03ec47e53859ff4be7e686ec60391..2c0197c6a7e27dfdd58ec81ff4a0183ba8bee639 100644 --- a/windows/flutter/generated_plugins.cmake +++ b/windows/flutter/generated_plugins.cmake @@ -5,6 +5,7 @@ list(APPEND FLUTTER_PLUGIN_LIST connectivity_plus_windows flutter_secure_storage_windows + geolocator_windows ) list(APPEND FLUTTER_FFI_PLUGIN_LIST