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); } }