Flutter-权限管理 permission_handler

姬天逸
2023-12-01

权限管理

dependencies:
	permission_handler: # 权限管理
 ///调用原生的收车检测和交车检测功能
  addUsedCarOrder(int index) async {
    ///Android 需要额外判断权限
    if (Platform.isAndroid) {
      if (await _requestPermission()) {
        GlobalConfig.getInstance().addUsedCarOrder(index);
      }
    } else if (Platform.isIOS) {
      GlobalConfig.getInstance().addUsedCarOrder(index);
    }
  }

  ///请求权限
  Future<bool> _requestPermission() async {
    var status = await Permission.camera.status;
    var status1 = await Permission.storage.status;

    if (status1 != PermissionStatus.granted ||
        status != PermissionStatus.granted) {
      var future = await [Permission.camera, Permission.storage].request();
      for (final item in future.entries) {
        ///拒绝并不再询问
        if (item.value.isPermanentlyDenied) {
          ///BrnToast.show("请您去应用程序设置手动开启存储权限", context);
          openAppSettings();
          return false;
        }
        if (item.value != PermissionStatus.granted) {
          return false;
        }
      }
    }

    return true;
  }
 类似资料: