5/21/1
This commit is contained in:
@@ -1,72 +1,28 @@
|
||||
import 'dart:async';
|
||||
import 'package:SEFER/constant/box_name.dart';
|
||||
import 'package:SEFER/controller/auth/captin/login_captin_controller.dart';
|
||||
import 'package:background_location/background_location.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:get/get.dart';
|
||||
import 'package:permission_handler/permission_handler.dart';
|
||||
|
||||
import '../../main.dart';
|
||||
|
||||
class LocationBackgroundController extends GetxController {
|
||||
@override
|
||||
Future<void> onInit() async {
|
||||
void onInit() {
|
||||
super.onInit();
|
||||
await requestLocationPermission();
|
||||
await configureBackgroundLocation();
|
||||
requestLocationPermission();
|
||||
configureBackgroundLocation();
|
||||
}
|
||||
|
||||
Future<void> requestLocationPermission() async {
|
||||
var status = await Permission.locationAlways.status;
|
||||
print('Initial status: $status');
|
||||
|
||||
if (status == PermissionStatus.denied ||
|
||||
status == PermissionStatus.restricted) {
|
||||
// Show dialog to inform the driver about background GPS location usage
|
||||
await Get.dialog(
|
||||
AlertDialog(
|
||||
title: Text('Location Permission'.tr),
|
||||
content: Text(
|
||||
'We use GPS location in the background to enable you to receive orders.'
|
||||
.tr),
|
||||
actions: [
|
||||
TextButton(
|
||||
onPressed: () async {
|
||||
Get.back(); // Close the dialog
|
||||
// Request permission
|
||||
status = await Permission.locationAlways.request();
|
||||
print('Requested status: $status');
|
||||
_handlePermissionStatus(status);
|
||||
},
|
||||
child: Text('OK'),
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
} else {
|
||||
_handlePermissionStatus(status);
|
||||
}
|
||||
}
|
||||
|
||||
void _handlePermissionStatus(PermissionStatus status) async {
|
||||
status = await Permission.locationAlways.status;
|
||||
if (!status.isGranted) {
|
||||
// Open app settings if permission is permanently denied
|
||||
openAppSettings();
|
||||
box.write(BoxName.locationPermission, 'true');
|
||||
Get.find<LoginCaptinController>().update();
|
||||
} else if (status.isGranted) {
|
||||
// Permission granted
|
||||
box.write(BoxName.locationPermission, 'true');
|
||||
Get.find<LoginCaptinController>().update();
|
||||
await Permission.locationAlways.request();
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> configureBackgroundLocation() async {
|
||||
await BackgroundLocation.setAndroidNotification(
|
||||
title: "Sefer Driver ",
|
||||
title: "Background Location",
|
||||
message: "Tracking location...",
|
||||
icon: "app_icon",
|
||||
icon: "@mipmap/ic_launcher",
|
||||
);
|
||||
|
||||
BackgroundLocation.setAndroidConfiguration(1000);
|
||||
|
||||
Reference in New Issue
Block a user