request static method

Future<bool> request({
  1. required Permission permission,
  2. String? title,
  3. String? confirmText,
  4. required String message,
  5. required String error,
  6. double? width,
})

申请权限

Implementation

static Future<bool> request({
  required Permission permission,
  String? title,
  String? confirmText,
  required String message,
  required String error,
  double? width,
}) async {
  if (await permission.isGranted) {
    return true;
  }
  Completer<bool> completer = Completer<bool>();
  DialogX.to.showPromptDialog(
    icon: DialogIcon.warn,
    title: title ?? '授权确认',
    message: message,
    barrierDismissible: false,
    confirmText: confirmText ?? '确认授权',
    width: width,
    onConfirm: () async {
      var status = await permission.request();
      if (status.isGranted) {
        completer.complete(true);
      } else {
        completer.complete(false);
      }
    },
    onCancel: () {
      completer.complete(false);
    },
  );

  if (await completer.future) {
    return true;
  } else {
    showError(error);
    return false;
  }
}