checkAndRequestPermissions static method

Future<bool> checkAndRequestPermissions({
  1. required List<Permission> permissions,
  2. required String permissionIcon,
  3. required String permissionContent,
  4. required String permissionPermanentlyDeniedContent,
  5. required BuildContext context,
})

This checkAndRequestPermissions is used to Check and Request List of Permission .

  • permissions list of Permission to check and request
  • permissionIcon that shows in the popup before asking permission, used in customPermissionDialog
  • permissionContent that shows in the popup before asking permission, used in customPermissionDialog
  • permissionPermanentlyDeniedContent that shows in the popup when the permission is PermissionStatus.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;
  }
}