checkAndRequestPermissions static method
This checkAndRequestPermissions is used to Check and Request List of Permission .
permissions
list ofPermission
to check and requestpermissionIcon
that shows in the popup before asking permission, used in customPermissionDialogpermissionContent
that shows in the popup before asking permission, used in customPermissionDialogpermissionPermanentlyDeniedContent
that shows in the popup when the permission isPermissionStatus.permanentlyDenied
, used in customPermissionDialog
Implementation
static Future<bool> checkAndRequestPermissions(
{required List<Permission> permissions,
required String permissionIcon,
required String permissionContent,
required String permissionPermanentlyDeniedContent}) async {
var permissionStatusList = await permissions.status();
var hasDeniedPermission = permissionStatusList.values
.where((element) => element.isDenied)
.isNotEmpty;
var permanentlyDeniedPermission = permissionStatusList.values
.where((element) => element.isPermanentlyDenied);
var hasPermanentlyDeniedPermission = permanentlyDeniedPermission.isNotEmpty;
LogMessage.d("checkAndRequestPermissions",
"hasDeniedPermission : $hasDeniedPermission ,hasPermanentlyDeniedPermission : $hasPermanentlyDeniedPermission");
if (hasDeniedPermission) {
// Permissions are denied, check if rationale should be shown
var permissionRationaleList = await permissions.shouldShowRationale();
// bool shouldShowRationale = await Permission.camera.shouldShowRequestRationale || await Permission.microphone.shouldShowRequestRationale;
var hasShowRationale = permissionRationaleList
.where((element) => element == true)
.isNotEmpty;
LogMessage.d(
"checkAndRequestPermissions", "hasShowRationale : $hasShowRationale");
if (Platform.isAndroid && hasShowRationale) {
// Show rationale dialog explaining why the permissions are needed
var popupValue = await customPermissionDialog(
icon: permissionIcon,
content: permissionContent,
dialogStyle: AppStyleConfig.dialogStyle);
if (popupValue) {
var afterAskRationale = await permissions.request();
var hasGrantedPermissionAfterAsk =
afterAskRationale.values.where((element) => element.isGranted);
LogMessage.d("checkAndRequestPermissions",
"rationale hasGrantedPermissionAfterAsk : $hasGrantedPermissionAfterAsk hasPermanentlyDeniedPermission : $hasPermanentlyDeniedPermission");
if (hasPermanentlyDeniedPermission) {
return await showPermanentlyDeniedPopup(
permissions: permissions,
permissionIcon: permissionIcon,
permissionPermanentlyDeniedContent:
permissionPermanentlyDeniedContent);
} else {
return (hasGrantedPermissionAfterAsk.length >= permissions.length);
}
}
return popupValue;
} else {
// Request permissions without showing rationale
var popupValue = await customPermissionDialog(
icon: permissionIcon,
content: permissionContent,
dialogStyle: AppStyleConfig.dialogStyle);
if (popupValue) {
var afterAsk = await permissions.request();
var hasGrantedPermissionAfterAsk =
afterAsk.values.where((element) => element.isGranted);
LogMessage.d("checkAndRequestPermissions",
"hasGrantedPermissionAfterAsk : $hasGrantedPermissionAfterAsk hasPermanentlyDeniedPermission : $hasPermanentlyDeniedPermission");
if (hasPermanentlyDeniedPermission) {
return await showPermanentlyDeniedPopup(
permissions: permissions,
permissionIcon: permissionIcon,
permissionPermanentlyDeniedContent:
permissionPermanentlyDeniedContent);
} else {
return (hasGrantedPermissionAfterAsk.length >= permissions.length);
}
} else {
//user clicked not now in popup
return popupValue;
}
}
} else if (hasPermanentlyDeniedPermission) {
return await showPermanentlyDeniedPopup(
permissions: permissions,
permissionIcon: permissionIcon,
permissionPermanentlyDeniedContent:
permissionPermanentlyDeniedContent);
} else {
// Permissions are already granted, proceed with your logic
return true;
}
}