checkLocationAlways static method

dynamic checkLocationAlways(
  1. BuildContext context, {
  2. VoidCallback? onSuccess,
  3. bool needLocationAlways = true,
  4. VoidCallback? onFailed,
  5. VoidCallback? goSetting,
})

关于这个在文档有描述 The locationAlways permission can not be requested directly, the user has to request the locationWhenInUse permission first. Accepting this permission by clicking on the 'Allow While Using App' gives the user the possibility to request the locationAlways permission. This will then bring up another permission popup asking you to Keep Only While Using or to Change To Always Allow

Implementation

static checkLocationAlways(BuildContext context,
    {VoidCallback? onSuccess,
    bool needLocationAlways = true, //针对安卓的场景,有时可能不需要这个(q版本才有  之前的finelocation就够了)
    VoidCallback? onFailed,
    VoidCallback? goSetting}) async {
  /// Android没有这一步 ios会先访问这个再访问其他的
  PermissionStatus status = PermissionStatus.granted;

  ///获取前置状态
  status = await _checkSinglePermission(Permission.locationWhenInUse);

  ///获取第二个状态
  PermissionStatus status2 = PermissionStatus.denied;

  bool isNeed = Platform.isIOS ? true : needLocationAlways;

  ///如果前置状态为成功才能执行获取第二个状态
  if (status.isGranted && isNeed) {
    status2 = await _checkSinglePermission(Permission.locationAlways);
  }

  ///如果两个都成功那么就返回成功
  if (status.isGranted) {
    if (isNeed) {
      if (status2.isGranted) {
        if (onSuccess != null) {
          onSuccess();
        }
      } else if (status2.isDenied) {
        if (onFailed != null) {
          onFailed();
        }
      } else {
        _showDeniedDialog(context, Permission.locationAlways);
      }
    } else {
      if (onSuccess != null) {
        onSuccess();
      }
    }

    ///如果有一个拒绝那么就失败了
  } else if (status.isDenied) {
    //status是拒绝另外一个直接不申请
    if (onFailed != null) {
      onFailed();
    }
  } else {
    _showDeniedDialog(context, Permission.locationWhenInUse);
  }
}