146 lines
4.5 KiB
Dart
146 lines
4.5 KiB
Dart
import 'dart:async';
|
|
import 'package:flutter/material.dart';
|
|
import 'package:get/get.dart';
|
|
import 'package:geolocator/geolocator.dart';
|
|
|
|
class LocationController extends GetxController {
|
|
// Reactive variable to hold the current position
|
|
Rx<Position?> currentPosition = Rx<Position?>(null);
|
|
|
|
// Subscription to the position stream
|
|
StreamSubscription<Position>? positionStream;
|
|
bool isStreamPaused = false;
|
|
|
|
@override
|
|
void onInit() {
|
|
super.onInit();
|
|
// Start listening to location updates when the controller is initialized
|
|
startPositionStream();
|
|
}
|
|
|
|
void startPositionStream() async {
|
|
// Check for location service and permissions before starting the stream
|
|
bool serviceEnabled = await Geolocator.isLocationServiceEnabled();
|
|
if (!serviceEnabled) {
|
|
// Use GetX's context to show a dialog
|
|
Get.dialog(
|
|
AlertDialog(
|
|
title: const Text('Location Services Disabled'),
|
|
content: const Text(
|
|
'Please enable location services to continue using the app.'),
|
|
actions: <Widget>[
|
|
TextButton(
|
|
child: const Text('OK'),
|
|
onPressed: () {
|
|
// Close the dialog
|
|
Get.back();
|
|
// Optionally, you can direct the user to the settings page
|
|
// Geolocator.openLocationSettings();
|
|
},
|
|
),
|
|
],
|
|
),
|
|
barrierDismissible: false, // User must tap button to close dialog
|
|
);
|
|
return;
|
|
}
|
|
|
|
LocationPermission permission = await Geolocator.checkPermission();
|
|
if (permission == LocationPermission.denied) {
|
|
permission = await Geolocator.requestPermission();
|
|
if (permission == LocationPermission.denied) {
|
|
// Show a dialog if permissions are still denied
|
|
Get.dialog(
|
|
AlertDialog(
|
|
title: const Text('Location Permission Denied'),
|
|
content: const Text(
|
|
'This app requires location permissions to function properly. Please enable location permissions in your device settings.'),
|
|
actions: <Widget>[
|
|
TextButton(
|
|
child: const Text('OK'),
|
|
onPressed: () {
|
|
// Close the dialog
|
|
Get.back();
|
|
// Optionally, direct the user to the app settings
|
|
// Geolocator.openAppSettings();
|
|
},
|
|
),
|
|
],
|
|
),
|
|
barrierDismissible: false,
|
|
);
|
|
return;
|
|
}
|
|
}
|
|
|
|
if (permission == LocationPermission.deniedForever) {
|
|
// Show a dialog if permissions are permanently denied
|
|
Get.dialog(
|
|
AlertDialog(
|
|
title: const Text('Location Permission Needed'),
|
|
content: const Text(
|
|
'Location permissions have been permanently denied. Please open app settings to enable location permissions.'),
|
|
actions: <Widget>[
|
|
TextButton(
|
|
child: const Text('Open Settings'),
|
|
onPressed: () {
|
|
// Close the dialog and open app settings
|
|
Get.back();
|
|
Geolocator.openAppSettings();
|
|
},
|
|
),
|
|
],
|
|
),
|
|
barrierDismissible: false,
|
|
);
|
|
return;
|
|
}
|
|
|
|
// Set up the location options
|
|
const locationOptions = LocationSettings(
|
|
accuracy: LocationAccuracy.bestForNavigation,
|
|
distanceFilter: 0,
|
|
timeLimit: Duration(microseconds: 2500),
|
|
);
|
|
|
|
// Start listening to the position stream
|
|
positionStream =
|
|
Geolocator.getPositionStream(locationSettings: locationOptions).listen(
|
|
(Position position) {
|
|
currentPosition.value = position;
|
|
}, onError: (e) {
|
|
// Handle errors or exceptions in the stream
|
|
// You might want to log this error or use a state to show error messages
|
|
});
|
|
|
|
// Resume stream if it was paused previously
|
|
if (isStreamPaused) {
|
|
positionStream?.resume();
|
|
isStreamPaused = false;
|
|
}
|
|
}
|
|
|
|
// Method to stop the position stream
|
|
void stopPositionStream() {
|
|
if (positionStream != null) {
|
|
positionStream!.pause();
|
|
isStreamPaused = true;
|
|
}
|
|
}
|
|
|
|
// Method to resume the position stream
|
|
void resumePositionStream() {
|
|
if (positionStream != null && isStreamPaused) {
|
|
positionStream!.resume();
|
|
isStreamPaused = false;
|
|
}
|
|
}
|
|
|
|
@override
|
|
void onClose() {
|
|
// Cancel the position stream subscription when the controller is closed
|
|
positionStream?.cancel();
|
|
super.onClose();
|
|
}
|
|
}
|