i'm using permission_handler package to get the location

i want to display this message is the user denied the location

like this image

check image

this is the code

Future<bool> getUserLocation() async {
final loc = await Permission.location.request();
final locWhenUse = await Permission.locationWhenInUse.request();
try {
  if (loc == PermissionStatus.permanentlyDenied ||
      locWhenUse == PermissionStatus.permanentlyDenied) {
    //i want to show the pop up like image here

    setState(() {
      error = "Please enable location permission".tr;
    });
    return false;
  }
  if (await userLocation.getUserLocation() == null) {
    throw Exception();
  }
} on Exception {
  setState(() {
    error = "Something went wrong".tr;
  });
  return false;
} catch (e) {
  setState(() {
    error = "Error Happened while Getting your Location".tr;
  });
  return false;
}
return true;

}


Solution 1: Osama Mohamed

getLocation() async { Location location = Location();

bool _serviceEnabled;
PermissionStatus _permissionGranted;
LocationData _locationData;

_serviceEnabled = await location.serviceEnabled();
if (!_serviceEnabled) {
  _serviceEnabled = await location.requestService();
  if (!_serviceEnabled) {
    return;
  }
}

_permissionGranted = await location.hasPermission();
if (_permissionGranted == PermissionStatus.denied) {
  _permissionGranted = await location.requestPermission();
  if (_permissionGranted != PermissionStatus.granted) {
    return;
  }
}

_locationData = await location.getLocation();
print(_locationData.longitude)

}


Solution 2: MikeNabil

i solved the problem by create a custom dialog like the image i attached

so if the premission is denied, i will show this dialog

Future<bool> showAlertDialog({
required BuildContext context,
}) async {
if (!Platform.isIOS) {
return await showDialog(
  context: context,
  builder: (context) => AlertDialog(
    title: const Text('Location Services Disabled'),
    content: const Text('You need to enable Location Services in Setting'),
    actions: <Widget>[
      TextButton(
        child: const Text('Cancel'),
        onPressed: () => Navigator.of(context).pop(false),
      ),
      TextButton(
        child: const Text('Setting'),
        onPressed: () {
          openAppSettings();
          Navigator.of(context).pop(false);
        },
      ),
    ],
  ),
);
}

// todo : showDialog for ios
return await showCupertinoDialog(
context: context,
builder: (context) => CupertinoAlertDialog(
  title: const Text('Location Services Disabled'),
  content: const Text('You need to enable location services in setting'),
  actions: <Widget>[
    CupertinoDialogAction(
      child: const Text('Cancel'),
      onPressed: () => Navigator.of(context).pop(false),
    ),
    CupertinoDialogAction(
      child: const Text('Setting'),
      onPressed: () {
        openAppSettings();
        Navigator.of(context).pop(false);
      },
    ),
  ],
),
);
}