Flutter动态权限申请教程

1.添加外部资源包

permission_handler: 3.2.2

2.使用

Future requestPermission() async {
    // 申请权限
    Map<PermissionGroup, PermissionStatus> permissions =
    await PermissionHandler().requestPermissions([PermissionGroup.location,PermissionGroup.storage]);
    // 申请结果
    PermissionStatus permission =
    await PermissionHandler().checkPermissionStatus(PermissionGroup.location);

    if (permission == PermissionStatus.granted) {
      //权限申请成功
      preLogin();
    } else {
      //权限申请被拒绝
      CargoUtils.showMessage("亲!必须开启位置权限才能进入主程序~");
      // 申请权限
      //    只有当用户同时点选了拒绝开启权限和不再提醒后才会true
      bool isSHow = await PermissionHandler()
          .shouldShowRequestPermissionRationale(PermissionGroup.location);
      if(!isSHow){
        CargoUtils.showMessage("当前已被您禁用申请权限,请在设置中手动开启后退出重新进入程序");
        await PermissionHandler().openAppSettings();
      }else{
        PermissionStatus ps =
        await PermissionHandler().checkPermissionStatus(PermissionGroup.location);
        if(ps == PermissionStatus.granted){
          preLogin();
        }else{
          requestPermission();
        }
      }

    }

  }

你可能感兴趣的:(Flutter,权限)