Created
April 2, 2021 10:23
-
-
Save arulwastaken/c9793490c319b70c3f1a8f0b11836c50 to your computer and use it in GitHub Desktop.
Location manger flutter
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class LocationManager { | |
Location location = new Location(); | |
bool _serviceEnabled = false; | |
PermissionStatus? _permissionGranted; | |
late LocationData _locationData; | |
Function(LocationState) callback; | |
LocationManager({required this.callback}); | |
Future<bool> requestService() async { | |
callback.call(LocationState.loading()); | |
callback.call(LocationState( | |
state: LocationEnum.REQUESTING_LOCATION_SERVICE, | |
message: "Requesting location service")); | |
_serviceEnabled = await location.serviceEnabled(); | |
if (!_serviceEnabled) { | |
_serviceEnabled = await location.requestService(); | |
if (!_serviceEnabled) { | |
return Future.value(false); | |
} | |
} else { | |
return Future.value(true); | |
} | |
return Future.value(false); | |
} | |
Future<bool> requestPermission() async { | |
_permissionGranted = await location.hasPermission(); | |
if (_permissionGranted == PermissionStatus.denied) { | |
_permissionGranted = await location.requestPermission(); | |
if (_permissionGranted != PermissionStatus.granted) { | |
return Future.value(false); | |
} else { | |
return Future.value(true); | |
} | |
} else { | |
return Future.value(true); | |
} | |
} | |
requestLocation() async { | |
bool serviceEnabled = await requestService(); | |
if (!serviceEnabled) { | |
callback.call(LocationState.error( | |
message: "Service disabled, Please enable to continue")); | |
return; | |
} | |
bool permissionState = await requestPermission(); | |
if (!permissionState) { | |
callback.call(LocationState.error( | |
message: "Permission denied, Please enable location to continue")); | |
return; | |
} | |
callback.call(LocationState( | |
state: LocationEnum.REQUESTING_LOCATION, message: "requesting location")); | |
_locationData = await location.getLocation(); | |
callback.call(LocationState( | |
state: LocationEnum.LOCATION_FOUND, location: _locationData)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment