164 lines
5.7 KiB
Dart
164 lines
5.7 KiB
Dart
import 'package:flutter/cupertino.dart';
|
||
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';
|
||
import 'package:star_lock/tools/storage.dart';
|
||
import 'package:star_lock/translations/trans_lib.dart';
|
||
|
||
class PermissionDialog {
|
||
static Map<Permission, String> titles = {
|
||
Permission.camera: '相机'.tr,
|
||
Permission.photos: '相册'.tr,
|
||
Permission.storage: '读写'.tr,
|
||
Permission.location: '定位'.tr,
|
||
Permission.bluetooth: '蓝牙'.tr,
|
||
Permission.bluetoothScan: '蓝牙'.tr,
|
||
Permission.bluetoothConnect: '蓝牙'.tr,
|
||
};
|
||
|
||
static Map<Permission, String> contents = {
|
||
Permission.camera: '需要访问相机权限才能拍照上传文件例如头像上传'.tr,
|
||
Permission.photos: '需要访问相机权限才能使用相册图片上传文件上传头像'.tr,
|
||
Permission.storage: '需要访问读写权限才能使用本地图片上传头像'.tr,
|
||
Permission.location: '需要访问定位权限才能使用添加钥匙功能的位置信息'.tr,
|
||
Permission.bluetooth: '需要访问蓝牙权限才能使用添加钥匙功能的位置信息'.tr,
|
||
Permission.bluetoothScan: '需要访问蓝牙权限才能使用添加钥匙功能的位置信息'.tr,
|
||
Permission.bluetoothConnect: '需要访问蓝牙权限才能使用添加钥匙功能的位置信息'.tr,
|
||
};
|
||
|
||
static Future<bool> requestBluetooth() async {
|
||
if (Get.context == null) {
|
||
return false;
|
||
}
|
||
bool application = true;
|
||
Map<Permission, PermissionStatus> statuses = await [
|
||
Permission.bluetoothScan,
|
||
Permission.bluetoothConnect,
|
||
].request();
|
||
Permission permission = Permission.bluetoothScan;
|
||
dynamic cache = await Storage.getString(titles[permission]);
|
||
bool isGranted = statuses.values.every((element) => element.isGranted);
|
||
if (AppPlatform.isAndroid && !isGranted && cache is! String) {
|
||
application = await showCupertinoDialog(
|
||
context: Get.context!,
|
||
builder: (context) {
|
||
return PopScope(
|
||
canPop: false,
|
||
child: CupertinoAlertDialog(
|
||
title: Text('${'申请'.tr}${titles[permission] ?? ''}${'权限'.tr}'),
|
||
content: Text(contents[permission] ?? ''),
|
||
actions: [
|
||
CupertinoDialogAction(
|
||
child: Text('不允许'.tr),
|
||
onPressed: () {
|
||
Get.back(result: false);
|
||
},
|
||
),
|
||
CupertinoDialogAction(
|
||
child: Text('允许'.tr),
|
||
onPressed: () {
|
||
Get.back(result: true);
|
||
},
|
||
),
|
||
],
|
||
));
|
||
},
|
||
);
|
||
|
||
if (application) {
|
||
await Storage.setString(titles[permission], titles[permission]);
|
||
}
|
||
} else if (cache is String) {
|
||
bool isDenied = statuses.values.every((element) => element.isDenied);
|
||
if (isDenied) {
|
||
showSet(permission);
|
||
return false;
|
||
}
|
||
}
|
||
if (application) {
|
||
PermissionStatus status = await permission.request();
|
||
return status.isGranted;
|
||
}
|
||
return false;
|
||
}
|
||
|
||
//显示权限判断申请框
|
||
static Future<bool> request(Permission permission, [String? content]) async {
|
||
if (Get.context == null) {
|
||
return false;
|
||
}
|
||
bool application = true;
|
||
PermissionStatus status = await permission.status;
|
||
dynamic cache = await Storage.getString(titles[permission]);
|
||
if (AppPlatform.isAndroid && !status.isGranted && cache is! String) {
|
||
application = await showCupertinoDialog(
|
||
context: Get.context!,
|
||
builder: (context) {
|
||
return PopScope(
|
||
canPop: false,
|
||
child: CupertinoAlertDialog(
|
||
title: Text('${'申请'.tr}${titles[permission] ?? ''}${'权限'.tr}'),
|
||
content: Text(content ?? contents[permission] ?? ''),
|
||
actions: [
|
||
CupertinoDialogAction(
|
||
child: Text('不允许'.tr),
|
||
onPressed: () {
|
||
Get.back(result: false);
|
||
},
|
||
),
|
||
CupertinoDialogAction(
|
||
child: Text('允许'.tr),
|
||
onPressed: () {
|
||
Get.back(result: true);
|
||
},
|
||
),
|
||
],
|
||
));
|
||
},
|
||
);
|
||
if (application) {
|
||
await Storage.setString(titles[permission], titles[permission]);
|
||
}
|
||
} else if (cache is String) {
|
||
if (status.isDenied) {
|
||
showSet(permission);
|
||
return false;
|
||
}
|
||
}
|
||
if (application) {
|
||
PermissionStatus status = await permission.request();
|
||
return status.isGranted;
|
||
}
|
||
return false;
|
||
}
|
||
|
||
//显示设置页面
|
||
static Future<void> showSet(Permission permission) async {
|
||
if (Get.context == null) {
|
||
return;
|
||
}
|
||
showCupertinoDialog(
|
||
context: Get.context!,
|
||
builder: (context) {
|
||
return PopScope(
|
||
canPop: false,
|
||
child: CupertinoAlertDialog(
|
||
title: Text('${titles[permission] ?? ''}${'权限被拒绝'.tr}'),
|
||
content: Text(
|
||
'${contents[permission] ?? ''},${'请手动在系统设置中开启'.tr}${titles[permission]}${'权限以继续使用应用'.tr}'),
|
||
actions: [
|
||
CupertinoDialogAction(
|
||
child: Text('去设置'.tr),
|
||
onPressed: () {
|
||
Get.back(); // 关闭对话框
|
||
openAppSettings(); // 打开系统设置页面
|
||
},
|
||
),
|
||
],
|
||
));
|
||
},
|
||
);
|
||
}
|
||
}
|