81 lines
2.5 KiB
Dart
81 lines
2.5 KiB
Dart
/*
|
|
* @Descripttion: 获取本地权限
|
|
* @version: UpgradePermission
|
|
* @Author: wy
|
|
* @Date: 2020-07-30 15:41:39
|
|
* @LastEditors: wangyang 1147192855@qq.com
|
|
* @LastEditTime: 2022-08-01 14:08:57
|
|
*/
|
|
import 'package:flutter/material.dart';
|
|
import 'package:flutter_screenutil/flutter_screenutil.dart';
|
|
import 'package:marking_app/utils/index.dart';
|
|
import 'package:permission_handler/permission_handler.dart';
|
|
|
|
class UpgradePermission {
|
|
final String _flatform;
|
|
const UpgradePermission(this._flatform);
|
|
|
|
/// 检查是否有权限,用于安卓
|
|
Future<bool> checkPermission(BuildContext context) async {
|
|
if (_flatform != 'android') return true; // 非安卓
|
|
var status = await Permission.storage.request();
|
|
if (status.isGranted) return true;
|
|
|
|
if (status.isDenied) {
|
|
// 普通拒绝 可以再进行提示
|
|
await showDialog<bool>(
|
|
context: context,
|
|
barrierDismissible: false,
|
|
builder: (BuildContext context) {
|
|
return AlertDialog(
|
|
title: const Text("权限提示"),
|
|
content: const Text("无法获取存储权限,请同意获取设备存储权限"),
|
|
actions: [
|
|
MaterialButton(
|
|
color: Theme.of(context).primaryColor,
|
|
child: const Text("同意", style: TextStyle(color: Colors.white)),
|
|
onPressed: () => Navigator.of(context).pop(),
|
|
),
|
|
],
|
|
);
|
|
},
|
|
);
|
|
|
|
if (isPad()) {
|
|
// 平板无法发起权限询问 直接进入APP权限设置页面
|
|
await openAppSettings();
|
|
return false;
|
|
}
|
|
|
|
return checkPermission(context);
|
|
}
|
|
|
|
// 拒绝并不再提示
|
|
await showDialog(
|
|
context: context,
|
|
barrierDismissible: false,
|
|
builder: (BuildContext context) {
|
|
return AlertDialog(
|
|
title: const Text("权限提示"),
|
|
content: const Text("储存权限被永久拒绝,并且不再提示。请前往设置页面同意储存权限"),
|
|
actions: [
|
|
MaterialButton(
|
|
color: Colors.green.shade900,
|
|
child: const Text("退出APP", style: TextStyle(color: Colors.white)),
|
|
onPressed: () => Navigator.of(context).pop(false),
|
|
),
|
|
MaterialButton(
|
|
color: Colors.green.shade900,
|
|
child: const Text("确定", style: TextStyle(color: Colors.white)),
|
|
onPressed: () => Navigator.of(context).pop(true),
|
|
),
|
|
],
|
|
);
|
|
},
|
|
);
|
|
|
|
await openAppSettings();
|
|
return false;
|
|
}
|
|
}
|