determinePosition method

Future<Position?> determinePosition({
  1. bool requestPermission = false,
  2. bool showLoader = true,
  3. bool openAppSetting = true,
})

Implementation

Future<Position?> determinePosition({
  bool requestPermission = false,
  bool showLoader = true,
  bool openAppSetting = true,
}) async {
  LocationPermission permission;
  Position? position;
  permission = await Geolocator.checkPermission();
  if (permission == LocationPermission.deniedForever) {
    if (openAppSetting) {
      Geolocator.openAppSettings();
    }
  } else if (permission == LocationPermission.denied && requestPermission) {
    permission = await Geolocator.requestPermission();
    if (permission == LocationPermission.deniedForever) {
      if (openAppSetting) {
        Geolocator.openAppSettings();
      }
    } else if (permission == LocationPermission.always ||
        permission == LocationPermission.whileInUse) {
      if(showLoader) {
        LoadingUtils.showLoader();
      }
      final location = lp.Location();

      bool enabled = await location.serviceEnabled();
      if (!enabled) {
        enabled = await location.requestService();
      }
      if (enabled) {
        position = await Geolocator.getCurrentPosition(
            desiredAccuracy: LocationAccuracy.high);
      } else {
        // Fluttertoast.showToast(
        //     msg: ErrorMessages.locationServiceAreNotEnabled);
      }
      LoadingUtils.hideLoader();
    }
  } else if (permission == LocationPermission.always ||
      permission == LocationPermission.whileInUse) {
    if (showLoader) {
      LoadingUtils.showLoader();
    }
    final location = lp.Location();

    bool enabled = await location.serviceEnabled();
    if (!enabled) {
      enabled = await location.requestService();
    }
    if (enabled) {
      position = await Geolocator.getCurrentPosition(
          desiredAccuracy: LocationAccuracy.high);
    } else {
      // Fluttertoast.showToast(msg: ErrorMessages.locationServiceAreNotEnabled);
    }
    LoadingUtils.hideLoader();
  }
  return position;
}