checkAndRequestPermissions static method
Future<bool>
checkAndRequestPermissions({
- required List<
Permission> permissions, - required String permissionIcon,
- required String permissionContent,
- required String permissionPermanentlyDeniedContent,
- required BuildContext context,
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,
required BuildContext context}) async {
// var info = await PackageInfo.fromPlatform();
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
if (context.mounted) {
var popupValue = await customPermissionDialog(
icon: permissionIcon,
content: permissionContent,
context: context,
appName: AppConstants.appName);
if (popupValue) {
var afterAskRationale = await permissions.request();
var hasGrantedPermissionAfterAsk = afterAskRationale.values
.toList()
.where((element) => element.isGranted);
LogMessage.d("checkAndRequestPermissions",
"rationale hasGrantedPermissionAfterAsk : $hasGrantedPermissionAfterAsk hasPermanentlyDeniedPermission : $hasPermanentlyDeniedPermission");
if (hasPermanentlyDeniedPermission) {
if (context.mounted) {
return await showPermanentlyDeniedPopup(
permissions: permissions,
permissionIcon: permissionIcon,
permissionPermanentlyDeniedContent:
permissionPermanentlyDeniedContent,
context: context,
appName: AppConstants.appName);
}
return false;
} else {
return (hasGrantedPermissionAfterAsk.length ==
permissions.length);
}
}
return popupValue;
}
return false;
} else {
if (context.mounted) {
// Request permissions without showing rationale
var popupValue = await customPermissionDialog(
icon: permissionIcon,
content: permissionContent,
context: context,
appName: AppConstants.appName);
if (popupValue) {
var afterAsk = await permissions.request();
var hasGrantedPermissionAfterAsk =
afterAsk.values.toList().where((element) => element.isGranted);
LogMessage.d("checkAndRequestPermissions",
"hasGrantedPermissionAfterAsk : $hasGrantedPermissionAfterAsk hasPermanentlyDeniedPermission : $hasPermanentlyDeniedPermission");
if (hasPermanentlyDeniedPermission) {
if (context.mounted) {
return await showPermanentlyDeniedPopup(
permissions: permissions,
permissionIcon: permissionIcon,
permissionPermanentlyDeniedContent:
permissionPermanentlyDeniedContent,
context: context,
appName: AppConstants.appName);
}
return false;
} else {
return (hasGrantedPermissionAfterAsk.length ==
permissions.length);
}
} else {
//user clicked not now in popup
return popupValue;
}
}
}
return false;
} else if (hasPermanentlyDeniedPermission) {
if (context.mounted) {
return await showPermanentlyDeniedPopup(
permissions: permissions,
permissionIcon: permissionIcon,
permissionPermanentlyDeniedContent:
permissionPermanentlyDeniedContent,
context: context,
appName: AppConstants.appName);
}
return false;
} else {
// Permissions are already granted, proceed with your logic
return true;
}
}