You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
105 lines
3.8 KiB
Dart
105 lines
3.8 KiB
Dart
import 'dart:async';
|
|
|
|
import 'package:flutter/material.dart';
|
|
import 'package:geolocator/geolocator.dart';
|
|
import 'package:permission_handler/permission_handler.dart';
|
|
|
|
class LocationUtilities {
|
|
static void havePermission(Function(bool) callback) {
|
|
Geolocator.checkPermission().then((value) async {
|
|
if (value == LocationPermission.denied || value == LocationPermission.deniedForever) {
|
|
value = await Geolocator.requestPermission();
|
|
callback(![LocationPermission.denied, LocationPermission.deniedForever].contains(value));
|
|
} else {
|
|
callback(true);
|
|
}
|
|
});
|
|
}
|
|
|
|
static void isEnabled(Function(bool) callback) {
|
|
Geolocator.isLocationServiceEnabled().then((value) => callback(value));
|
|
}
|
|
|
|
static bool _listeningSettingChange = true;
|
|
|
|
static void listenGPS({bool change = true, Function(bool)? onChange}) async {
|
|
_listeningSettingChange = change;
|
|
if (change == false) return;
|
|
|
|
Future.doWhile(() async {
|
|
await Future.delayed(const Duration(milliseconds: 1000));
|
|
var enable = await Geolocator.isLocationServiceEnabled();
|
|
onChange!(enable);
|
|
return _listeningSettingChange;
|
|
});
|
|
}
|
|
|
|
static void locationFun(Function(bool) completion, BuildContext context) {
|
|
Permission.location.isGranted.then((isGranted) {
|
|
if (!isGranted) {
|
|
Permission.location.request().then((granted) {
|
|
completion(granted == PermissionStatus.granted);
|
|
});
|
|
}
|
|
completion(isGranted);
|
|
});
|
|
}
|
|
|
|
static void getCurrentLocation(Function(Position position, bool isMocked) callback, Function errorCallBack, BuildContext context) {
|
|
Geolocator.getCurrentPosition(desiredAccuracy: LocationAccuracy.medium, timeLimit: const Duration(seconds: 5)).then((position) {
|
|
bool isMocked = position.isMocked;
|
|
callback(position, isMocked);
|
|
}).catchError((err) {
|
|
errorCallBack();
|
|
});
|
|
// return;
|
|
// Permission.location.isGranted.then((isGranted) {
|
|
// if (!isGranted) {
|
|
// Permission.location.request().then((granted) {
|
|
// print("granted:$granted");
|
|
// if (granted == PermissionStatus.granted) {
|
|
// Geolocator.getCurrentPosition(desiredAccuracy: LocationAccuracy.medium, timeLimit: const Duration(seconds: 5)).then((position) {
|
|
// bool isMocked = position.isMocked;
|
|
// callback(position, isMocked);
|
|
// }).catchError((err) {
|
|
// print("getCurrentPositionError:$err");
|
|
// errorCallBack();
|
|
// });
|
|
// } else {
|
|
// errorCallBack();
|
|
// }
|
|
// });
|
|
// } else {
|
|
// Geolocator.getCurrentPosition(desiredAccuracy: LocationAccuracy.medium, timeLimit: const Duration(seconds: 5)).then((position) {
|
|
// bool isMocked = position.isMocked;
|
|
// callback(position, isMocked);
|
|
// }).catchError((err) {
|
|
// print("getCurrentPositionError:$err");
|
|
// errorCallBack();
|
|
// });
|
|
// }
|
|
// });
|
|
//
|
|
// // Geolocator.getCurrentPosition(desiredAccuracy: LocationAccuracy.medium, timeLimit: const Duration(seconds: 5)).then((position) {
|
|
// // bool isMocked = position.isMocked;
|
|
// // callback(position, isMocked);
|
|
// // }).catchError((err) {
|
|
// // print("getCurrentPositionError:$err");
|
|
// // errorCallBack();
|
|
// // });
|
|
//
|
|
// // locationFun((granted) {
|
|
// // if (granted) {
|
|
// // Geolocator.getCurrentPosition(desiredAccuracy: LocationAccuracy.medium, timeLimit: const Duration(seconds: 5)).then((value) {
|
|
// // done(value);
|
|
// // }).catchError((err) {
|
|
// // print("getCurrentPositionError:$err");
|
|
// // errorCallBack();
|
|
// // });
|
|
// // } else {
|
|
// // // AppPermissions
|
|
// // }
|
|
// // }, context);
|
|
}
|
|
}
|