feat:统一权限dialog的控制,针对android华为的过审要求
This commit is contained in:
parent
68f5eaddb3
commit
96e6fd5463
84
star_lock/lib/permission/permission_dialog.dart
Normal file
84
star_lock/lib/permission/permission_dialog.dart
Normal file
@ -0,0 +1,84 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:get/get.dart';
|
||||
import 'package:permission_handler/permission_handler.dart';
|
||||
import 'package:star_lock/app_settings/app_settings.dart';
|
||||
|
||||
class PermissionDialog {
|
||||
static Map<Permission, String> titles = {
|
||||
Permission.camera: '相机',
|
||||
Permission.photos: '相册',
|
||||
Permission.storage: '读写',
|
||||
Permission.location: '定位',
|
||||
};
|
||||
|
||||
static Map<Permission, String> contents = {
|
||||
Permission.camera: '需要访问相机权限才能拍照上传文件例如头像上传',
|
||||
Permission.photos: '需要访问相机权限才能使用相册图片上传文件上传头像',
|
||||
Permission.storage: '需要访问读写权限才能使用本地图片上传头像',
|
||||
Permission.location: '需要访问定位权限才能使用添加钥匙功能的位置信息',
|
||||
};
|
||||
|
||||
//显示权限判断申请框
|
||||
static Future<bool> request(Permission permission) async {
|
||||
if (Get.context == null) {
|
||||
return false;
|
||||
}
|
||||
bool application = true;
|
||||
if (AppPlatform.isAndroid) {
|
||||
application = await showDialog(
|
||||
context: Get.context!,
|
||||
builder: (BuildContext context) {
|
||||
return AlertDialog(
|
||||
title: Text('申请${titles[permission] ?? ''}权限'),
|
||||
content: Text(contents[permission] ?? ''),
|
||||
actions: <Widget>[
|
||||
TextButton(
|
||||
child: const Text('不允许'),
|
||||
onPressed: () {
|
||||
Get.back(result: false);
|
||||
},
|
||||
),
|
||||
TextButton(
|
||||
child: const Text('允许'),
|
||||
onPressed: () {
|
||||
Get.back(result: true);
|
||||
},
|
||||
),
|
||||
],
|
||||
);
|
||||
},
|
||||
);
|
||||
}
|
||||
if (!application) {
|
||||
showSet(permission);
|
||||
return false;
|
||||
} else {
|
||||
return await permission.request().isGranted;
|
||||
}
|
||||
}
|
||||
|
||||
//显示设置页面
|
||||
static Future<void> showSet(Permission permission) async {
|
||||
if (Get.context == null) {
|
||||
return;
|
||||
}
|
||||
showDialog(
|
||||
context: Get.context!,
|
||||
builder: (BuildContext context) {
|
||||
return AlertDialog(
|
||||
title: Text('${titles[permission] ?? ''}权限被拒绝'),
|
||||
content: Text('${contents[permission] ?? ''},请手动在系统设置中开启${titles[permission]}权限以继续使用应用。'),
|
||||
actions: <Widget>[
|
||||
TextButton(
|
||||
child: Text('去设置'),
|
||||
onPressed: () {
|
||||
Get.back(); // 关闭对话框
|
||||
openAppSettings(); // 打开系统设置页面
|
||||
},
|
||||
),
|
||||
],
|
||||
);
|
||||
},
|
||||
);
|
||||
}
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user