requestLocationPermission method

void requestLocationPermission()

Implementation

void requestLocationPermission() async {
  bool serviceEnabled;
  LocationPermission permission;

  // Check if location services are enabled
  serviceEnabled = await Geolocator.isLocationServiceEnabled();
  if (!serviceEnabled) {
    AppUtils.showSnackBar('Location services are disabled. Please enable them.');
    await storageUtils.setLocationPermission('false');
    return;
  }

  // Check for location permissions
  permission = await Geolocator.checkPermission();
  if (permission == LocationPermission.denied ||
      permission == LocationPermission.deniedForever) {
    permission = await Geolocator.requestPermission();
  }

  // Handle permission responses
  if (permission == LocationPermission.denied) {
    AppUtils.showSnackBar(
        'Location permissions are denied. Please allow them.');
    await storageUtils.setLocationPermission('false');
    return;
  }

  if (permission == LocationPermission.deniedForever) {
    // AppUtils.showSnackBar(
    //   'Location permissions are permanently denied. Please enable them in settings.',
    // );
    await storageUtils.setLocationPermission('false');
    return;
  }

  // If permission is granted
  if (permission == LocationPermission.whileInUse ||
      permission == LocationPermission.always) {
    await _updateCurrentLocation(); // Fetch and update location
    await storageUtils.setLocationPermission('true'); // Mark login success
  }
}