117 lines
3.2 KiB
Dart
Executable File
117 lines
3.2 KiB
Dart
Executable File
|
|
import 'package:flutter/cupertino.dart';
|
|
import 'package:flutter/services.dart';
|
|
import 'package:get/get.dart';
|
|
import 'package:permission_handler/permission_handler.dart';
|
|
|
|
import '../app_settings/app_settings.dart';
|
|
|
|
/// @ClassName ${NAME}
|
|
/// 作者: szj
|
|
/// 时间: ${DATE} ${TIME}
|
|
/// CSDN:https://blog.csdn.net/weixin_44819566
|
|
/// 公众号:码上变有钱
|
|
|
|
typedef BlockScuessStatus = void Function();
|
|
class PermissionUtil {
|
|
PermissionUtil(this._context);
|
|
|
|
final List<String> _list = [
|
|
'为了更好地应用体验,请确定权限'.tr,
|
|
'您第一次拒绝权限,请确定权限'.tr,
|
|
'您第二次拒绝权限,请去应用设置开启权限'.tr
|
|
];
|
|
|
|
final BuildContext _context ;
|
|
Future<void> checkPermission({PermissionStatus? status, BlockScuessStatus? blockScuessStatus}) async {
|
|
//申请权限 permission_handler: ^5.0.1+1
|
|
|
|
//位置权限
|
|
Permission permission = Permission.location;
|
|
|
|
status ??= await permission.status;
|
|
// AppLog.log('statusstatusstatus:$status');
|
|
if (status.isDenied) {
|
|
//第一次申请
|
|
showPermissionDialog(_list[0], '同意'.tr, permission);
|
|
} else if (status.isDenied) {
|
|
//第一次申请拒绝
|
|
showPermissionDialog(_list[1], '重试'.tr, permission);
|
|
} else if (status.isPermanentlyDenied) {
|
|
//第二次申请
|
|
showPermissionDialog(_list[2], '去应用市场'.tr, permission,isUndetermined: true);
|
|
} else if (status.isGranted) {
|
|
// 通过
|
|
blockScuessStatus!();
|
|
} else {
|
|
//通过
|
|
|
|
}
|
|
}
|
|
|
|
///是否去设置中心
|
|
bool isGoAppSetteng = false;
|
|
|
|
///msg 提示文案
|
|
///rightMsg 右侧按钮显示文案
|
|
///要申请的权限
|
|
/// isUndetermined 可选参数,传入的是当前是否去设置中心
|
|
void showPermissionDialog(
|
|
String msg, String rightMsg, Permission permission,{bool isUndetermined = false}) {
|
|
//使用苹果的dialog
|
|
showCupertinoDialog(
|
|
builder: (BuildContext context) {
|
|
return CupertinoAlertDialog(
|
|
title: Text('温馨提示'.tr),
|
|
content: Container(
|
|
child: Text(msg),
|
|
),
|
|
actions: [
|
|
//左边按钮
|
|
CupertinoDialogAction(
|
|
child: Text('关闭应用'.tr),
|
|
onPressed: closeAPP,
|
|
),
|
|
//右边
|
|
CupertinoDialogAction(
|
|
child: Text(rightMsg),
|
|
onPressed: () {
|
|
|
|
//关闭弹框
|
|
Navigator.pop(context);
|
|
if(isUndetermined){
|
|
isGoAppSetteng = true;
|
|
//去设置中心
|
|
openAppSettings();
|
|
}else{
|
|
//申请权限
|
|
isGoAppSetteng = false;
|
|
requestPermission(context,permission);
|
|
}
|
|
|
|
|
|
},
|
|
),
|
|
],
|
|
);
|
|
},
|
|
context: _context);
|
|
}
|
|
|
|
void requestPermission(BuildContext context,Permission permission) async {
|
|
|
|
//请求权限
|
|
PermissionStatus status = await permission.request();
|
|
|
|
//校验
|
|
checkPermission(status: status);
|
|
|
|
}
|
|
//关闭应用
|
|
void closeAPP() {
|
|
SystemChannels.platform.invokeMethod('SystemNavigator.pop');
|
|
}
|
|
|
|
|
|
}
|