2024-04-10 15:21:08 +08:00
|
|
|
|
import 'package:flutter/cupertino.dart';
|
2024-04-10 09:47:05 +08:00
|
|
|
|
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';
|
2024-04-10 15:21:08 +08:00
|
|
|
|
import 'package:star_lock/tools/storage.dart';
|
|
|
|
|
|
import 'package:star_lock/translations/trans_lib.dart';
|
2024-04-10 09:47:05 +08:00
|
|
|
|
|
|
|
|
|
|
class PermissionDialog {
|
|
|
|
|
|
static Map<Permission, String> titles = {
|
2024-04-10 15:21:08 +08:00
|
|
|
|
Permission.camera: TranslationLoader.lanKeys!.permissionDialogCamera!.tr,
|
|
|
|
|
|
Permission.photos: TranslationLoader.lanKeys!.permissionDialogPhotos!.tr,
|
|
|
|
|
|
Permission.storage: TranslationLoader.lanKeys!.permissionDialogStorage!.tr,
|
|
|
|
|
|
Permission.location:
|
|
|
|
|
|
TranslationLoader.lanKeys!.permissionDialogLocation!.tr,
|
2024-04-10 09:47:05 +08:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
static Map<Permission, String> contents = {
|
2024-04-10 15:21:08 +08:00
|
|
|
|
Permission.camera:
|
|
|
|
|
|
TranslationLoader.lanKeys!.permissionDialogCameraText!.tr,
|
|
|
|
|
|
Permission.photos: TranslationLoader.lanKeys!.permissionDialogPhotos!.tr,
|
|
|
|
|
|
Permission.storage:
|
|
|
|
|
|
TranslationLoader.lanKeys!.permissionDialogStorageText!.tr,
|
|
|
|
|
|
Permission.location:
|
|
|
|
|
|
TranslationLoader.lanKeys!.permissionDialogLocationText!.tr,
|
2024-04-10 09:47:05 +08:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
//显示权限判断申请框
|
|
|
|
|
|
static Future<bool> request(Permission permission) async {
|
|
|
|
|
|
if (Get.context == null) {
|
|
|
|
|
|
return false;
|
|
|
|
|
|
}
|
|
|
|
|
|
bool application = true;
|
2024-04-10 15:21:08 +08:00
|
|
|
|
PermissionStatus status = await permission.status;
|
|
|
|
|
|
dynamic cache = await Storage.getString(titles[permission]);
|
|
|
|
|
|
if (AppPlatform.isAndroid && !status.isGranted && cache is! String) {
|
|
|
|
|
|
application = await showCupertinoDialog(
|
2024-04-10 09:47:05 +08:00
|
|
|
|
context: Get.context!,
|
2024-04-10 15:21:08 +08:00
|
|
|
|
builder: (context) {
|
|
|
|
|
|
return PopScope(
|
|
|
|
|
|
canPop: false,
|
|
|
|
|
|
child: CupertinoAlertDialog(
|
|
|
|
|
|
title: Text(
|
|
|
|
|
|
'${TranslationLoader.lanKeys!.permissionDialogApplication!.tr}${titles[permission] ?? ''}${TranslationLoader.lanKeys!.permissionDialogAuthority!.tr}'),
|
|
|
|
|
|
content: Text(contents[permission] ?? ''),
|
|
|
|
|
|
actions: [
|
|
|
|
|
|
CupertinoDialogAction(
|
|
|
|
|
|
child: Text(TranslationLoader
|
|
|
|
|
|
.lanKeys!.permissionDialogNotAllowed!.tr),
|
|
|
|
|
|
onPressed: () {
|
|
|
|
|
|
Get.back(result: false);
|
|
|
|
|
|
},
|
|
|
|
|
|
),
|
|
|
|
|
|
CupertinoDialogAction(
|
|
|
|
|
|
child: Text(
|
|
|
|
|
|
TranslationLoader.lanKeys!.permissionDialogAllowed!.tr),
|
|
|
|
|
|
onPressed: () {
|
|
|
|
|
|
Get.back(result: true);
|
|
|
|
|
|
},
|
|
|
|
|
|
),
|
|
|
|
|
|
],
|
|
|
|
|
|
));
|
2024-04-10 09:47:05 +08:00
|
|
|
|
},
|
|
|
|
|
|
);
|
2024-04-10 15:21:08 +08:00
|
|
|
|
if(application){
|
|
|
|
|
|
await Storage.setString(titles[permission], titles[permission]);
|
|
|
|
|
|
}
|
|
|
|
|
|
} else if (cache is String) {
|
|
|
|
|
|
if (status.isDenied) {
|
|
|
|
|
|
showSet(permission);
|
|
|
|
|
|
return false;
|
|
|
|
|
|
}
|
2024-04-10 09:47:05 +08:00
|
|
|
|
}
|
2024-04-10 15:21:08 +08:00
|
|
|
|
if (application) {
|
|
|
|
|
|
PermissionStatus status = await permission.request();
|
|
|
|
|
|
return status.isGranted;
|
2024-04-10 09:47:05 +08:00
|
|
|
|
}
|
2024-04-10 15:21:08 +08:00
|
|
|
|
return false;
|
2024-04-10 09:47:05 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//显示设置页面
|
|
|
|
|
|
static Future<void> showSet(Permission permission) async {
|
|
|
|
|
|
if (Get.context == null) {
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
2024-04-10 15:21:08 +08:00
|
|
|
|
showCupertinoDialog(
|
2024-04-10 09:47:05 +08:00
|
|
|
|
context: Get.context!,
|
2024-04-10 15:21:08 +08:00
|
|
|
|
builder: (context) {
|
|
|
|
|
|
return PopScope(
|
|
|
|
|
|
canPop: false,
|
|
|
|
|
|
child: CupertinoAlertDialog(
|
|
|
|
|
|
title: Text(
|
|
|
|
|
|
'${titles[permission] ?? ''}${TranslationLoader.lanKeys!.permissionDialogPermissionDenied!.tr}'),
|
|
|
|
|
|
content: Text(
|
|
|
|
|
|
'${contents[permission] ?? ''},${TranslationLoader.lanKeys!.permissionDialogOpenPermissions!.tr}${titles[permission]}${TranslationLoader.lanKeys!.permissionDialogCameraText!.tr}'),
|
|
|
|
|
|
actions: [
|
|
|
|
|
|
CupertinoDialogAction(
|
|
|
|
|
|
child: Text(
|
|
|
|
|
|
TranslationLoader.lanKeys!.permissionDialogGoSetUp!.tr),
|
|
|
|
|
|
onPressed: () {
|
|
|
|
|
|
Get.back(); // 关闭对话框
|
|
|
|
|
|
openAppSettings(); // 打开系统设置页面
|
|
|
|
|
|
},
|
|
|
|
|
|
),
|
|
|
|
|
|
],
|
|
|
|
|
|
));
|
2024-04-10 09:47:05 +08:00
|
|
|
|
},
|
|
|
|
|
|
);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|