You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
380 lines
12 KiB
380 lines
12 KiB
import 'package:flutter/material.dart';
|
|
import 'package:get/get.dart';
|
|
import 'package:permission_handler/permission_handler.dart';
|
|
import '../controllers/settings_controller.dart';
|
|
import 'package:flutter_screenutil/flutter_screenutil.dart';
|
|
import 'dart:ui';
|
|
import '../../../core/utils/permission_util.dart'; // 引入PermissionUtil
|
|
|
|
class PermissionsView extends GetView<SettingsController> {
|
|
const PermissionsView({Key? key}) : super(key: key);
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
final isDarkMode = Theme.of(context).brightness == Brightness.dark;
|
|
|
|
return Scaffold(
|
|
backgroundColor: isDarkMode ? Colors.black : Colors.grey[50],
|
|
appBar: AppBar(
|
|
backgroundColor: isDarkMode ? Colors.black : Colors.white,
|
|
elevation: 0.5,
|
|
leading: IconButton(
|
|
icon: Icon(
|
|
Icons.arrow_back_ios,
|
|
size: 20.w,
|
|
color: isDarkMode ? Colors.white : Colors.black87,
|
|
),
|
|
onPressed: () => Get.back(),
|
|
),
|
|
title: Text(
|
|
'permissionManagement'.tr, // 权限管理
|
|
style: TextStyle(
|
|
fontSize: 17.sp,
|
|
fontWeight: FontWeight.w600,
|
|
color: isDarkMode ? Colors.white : Colors.black,
|
|
),
|
|
),
|
|
centerTitle: true,
|
|
),
|
|
body: FutureBuilder<Map<Permission, PermissionStatus>>(
|
|
future: controller.checkAllPermissions(),
|
|
builder: (context, snapshot) {
|
|
if (snapshot.connectionState == ConnectionState.waiting) {
|
|
return const Center(child: CircularProgressIndicator());
|
|
}
|
|
|
|
if (snapshot.hasError) {
|
|
return Center(
|
|
child: Text('getPermissionStatusFailed'.tr +
|
|
': ${snapshot.error}')); // 获取权限状态失败
|
|
}
|
|
|
|
final permissionStatuses = snapshot.data ?? {};
|
|
|
|
return ListView(
|
|
padding: EdgeInsets.symmetric(horizontal: 16.w, vertical: 16.h),
|
|
children: [
|
|
// 通讯权限组
|
|
_buildPermissionSection(
|
|
'communicationPermissions'.tr, // 通讯权限
|
|
Icons.phone_android,
|
|
[Permission.phone, Permission.sms],
|
|
permissionStatuses,
|
|
isDarkMode),
|
|
|
|
SizedBox(height: 20.h),
|
|
|
|
// 日历权限组
|
|
_buildPermissionSection(
|
|
'calendarPermissions'.tr, // 日历权限
|
|
Icons.calendar_today,
|
|
[Permission.calendarFullAccess],
|
|
permissionStatuses,
|
|
isDarkMode),
|
|
|
|
SizedBox(height: 20.h),
|
|
|
|
// 位置权限组
|
|
_buildPermissionSection(
|
|
'locationPermissions'.tr, // 位置权限
|
|
Icons.location_on,
|
|
[Permission.location],
|
|
permissionStatuses,
|
|
isDarkMode),
|
|
],
|
|
);
|
|
},
|
|
),
|
|
);
|
|
}
|
|
|
|
// 构建权限组区域
|
|
Widget _buildPermissionSection(
|
|
String title,
|
|
IconData icon,
|
|
List<Permission> permissions,
|
|
Map<Permission, PermissionStatus> statuses,
|
|
bool isDarkMode) {
|
|
return Column(
|
|
crossAxisAlignment: CrossAxisAlignment.start,
|
|
children: [
|
|
// 组标题
|
|
Padding(
|
|
padding: EdgeInsets.only(left: 4.w, bottom: 8.h),
|
|
child: Text(
|
|
title.tr,
|
|
style: TextStyle(
|
|
fontSize: 12.sp,
|
|
fontWeight: FontWeight.w500,
|
|
color: isDarkMode ? Colors.blue[400] : Colors.blue[600],
|
|
letterSpacing: 1,
|
|
),
|
|
),
|
|
),
|
|
|
|
// 权限项容器
|
|
Container(
|
|
decoration: BoxDecoration(
|
|
color: isDarkMode ? Colors.white.withOpacity(0.05) : Colors.white,
|
|
borderRadius: BorderRadius.circular(16.r),
|
|
boxShadow: [
|
|
BoxShadow(
|
|
color: isDarkMode
|
|
? Colors.black.withOpacity(0.2)
|
|
: Colors.black.withOpacity(0.05),
|
|
blurRadius: 10,
|
|
offset: const Offset(0, 2),
|
|
),
|
|
],
|
|
),
|
|
child: ClipRRect(
|
|
borderRadius: BorderRadius.circular(16.r),
|
|
child: BackdropFilter(
|
|
filter: isDarkMode
|
|
? ImageFilter.blur(sigmaX: 5, sigmaY: 5)
|
|
: ImageFilter.blur(sigmaX: 0, sigmaY: 0),
|
|
child: Column(
|
|
children:
|
|
_buildPermissionItems(permissions, statuses, isDarkMode),
|
|
),
|
|
),
|
|
),
|
|
),
|
|
],
|
|
);
|
|
}
|
|
|
|
// 构建权限项列表
|
|
List<Widget> _buildPermissionItems(List<Permission> permissions,
|
|
Map<Permission, PermissionStatus> statuses, bool isDarkMode) {
|
|
final List<Widget> items = [];
|
|
|
|
// 获取名称
|
|
String getName(Permission permission) {
|
|
switch (permission) {
|
|
case Permission.phone:
|
|
return 'phone'.tr; // 电话
|
|
case Permission.sms:
|
|
return 'sms'.tr; // 短信
|
|
case Permission.calendarWriteOnly:
|
|
return 'calendarWrite'.tr; // 日历写入
|
|
case Permission.calendarFullAccess:
|
|
return 'calendarFullAccess'.tr; // 日历完整访问
|
|
case Permission.location:
|
|
return 'locationInfo'.tr; // 位置信息
|
|
default:
|
|
return permission.toString();
|
|
}
|
|
}
|
|
|
|
// 获取描述文本
|
|
String getDescription(Permission permission) {
|
|
switch (permission) {
|
|
case Permission.phone:
|
|
return 'phonePermissionDesc'.tr; // 用于拨打电话功能
|
|
case Permission.sms:
|
|
return 'smsPermissionDesc'.tr; // 用于发送短信功能
|
|
case Permission.calendarWriteOnly:
|
|
return 'calendarWritePermissionDesc'.tr; // 用于创建日历事件和提醒
|
|
case Permission.calendarFullAccess:
|
|
return 'calendarFullAccessPermissionDesc'.tr; // 用于读取和管理日历事件及提醒
|
|
case Permission.location:
|
|
return 'locationPermissionDesc'.tr; // 用于获取当前位置信息
|
|
default:
|
|
return 'appFunctionNeeds'.tr; // 应用功能需要
|
|
}
|
|
}
|
|
|
|
for (int i = 0; i < permissions.length; i++) {
|
|
final permission = permissions[i];
|
|
final status = statuses[permission] ?? PermissionStatus.denied;
|
|
|
|
items.add(
|
|
_buildPermissionItem(
|
|
name: getName(permission),
|
|
description: getDescription(permission),
|
|
status: status,
|
|
permission: permission,
|
|
isDarkMode: isDarkMode,
|
|
),
|
|
);
|
|
|
|
// 添加分隔线,最后一个不加
|
|
if (i < permissions.length - 1) {
|
|
items.add(Divider(
|
|
height: 1,
|
|
color: isDarkMode ? Colors.white.withOpacity(0.1) : Colors.grey[200],
|
|
));
|
|
}
|
|
}
|
|
|
|
return items;
|
|
}
|
|
|
|
// 构建单个权限项
|
|
Widget _buildPermissionItem({
|
|
required String name,
|
|
required String description,
|
|
required PermissionStatus status,
|
|
required Permission permission,
|
|
required bool isDarkMode,
|
|
}) {
|
|
return Padding(
|
|
padding: EdgeInsets.all(16.w),
|
|
child: Row(
|
|
mainAxisAlignment: MainAxisAlignment.spaceBetween,
|
|
children: [
|
|
Expanded(
|
|
child: Column(
|
|
crossAxisAlignment: CrossAxisAlignment.start,
|
|
children: [
|
|
Text(
|
|
name.tr,
|
|
style: TextStyle(
|
|
fontSize: 16.sp,
|
|
fontWeight: FontWeight.w500,
|
|
color: isDarkMode ? Colors.white : Colors.black87,
|
|
),
|
|
),
|
|
SizedBox(height: 4.h),
|
|
Text(
|
|
description.tr,
|
|
style: TextStyle(
|
|
fontSize: 12.sp,
|
|
color: isDarkMode ? Colors.white70 : Colors.grey[600],
|
|
),
|
|
),
|
|
SizedBox(height: 4.h),
|
|
_buildStatusText(status, isDarkMode),
|
|
],
|
|
),
|
|
),
|
|
Switch(
|
|
value: status.isGranted,
|
|
onChanged: (value) {
|
|
if (value) {
|
|
_handlePermissionAction(permission, status);
|
|
} else if (status.isGranted) {
|
|
controller.openSettings();
|
|
}
|
|
},
|
|
activeColor: Colors.blue[600],
|
|
),
|
|
],
|
|
),
|
|
);
|
|
}
|
|
|
|
// 状态文本
|
|
Widget _buildStatusText(PermissionStatus status, bool isDarkMode) {
|
|
String text;
|
|
Color color;
|
|
|
|
if (status.isGranted) {
|
|
text = 'authorized'.tr; // 已授权
|
|
color = Colors.green;
|
|
} else if (status.isDenied) {
|
|
text = 'unauthorized'.tr; // 未授权
|
|
color = Colors.red;
|
|
} else if (status.isPermanentlyDenied) {
|
|
text = 'needOpenInSystemSettings'.tr; // 需要在系统设置中开启
|
|
color = Colors.orange;
|
|
} else if (status.isLimited) {
|
|
text = 'partialAuthorization'.tr; // 部分授权
|
|
color = Colors.orange;
|
|
} else {
|
|
text = 'unknownStatus'.tr; // 未知状态
|
|
color = Colors.grey;
|
|
}
|
|
|
|
return Text(
|
|
text,
|
|
style: TextStyle(
|
|
color: color,
|
|
fontSize: 12.sp,
|
|
fontWeight: FontWeight.w500,
|
|
),
|
|
);
|
|
}
|
|
|
|
// 处理权限请求
|
|
Future<void> _handlePermissionAction(
|
|
Permission permission, PermissionStatus status) async {
|
|
try {
|
|
// final context = Get.context;
|
|
// if (context == null) return;
|
|
|
|
final permissionName = _getPermissionName(permission);
|
|
final explanationText = getDetailedDescription(permission);
|
|
|
|
// 使用 PermissionUtil 请求权限
|
|
final hasPermission = await PermissionUtil.instance.requestPermission(
|
|
// context: context,
|
|
permissionType: permission,
|
|
permissionName: permissionName,
|
|
explanationText: explanationText,
|
|
permanentDenialText: 'permanentDenialText'.tr.replaceAll(
|
|
'{permission}', permissionName), // 您已永久拒绝{permission}权限,请前往应用设置开启
|
|
settingsButtonText: 'goToSettings'.tr, // 去设置
|
|
cancelButtonText: 'cancel'.tr, // 取消
|
|
);
|
|
|
|
if (hasPermission) {
|
|
Get.snackbar(
|
|
'success'.tr, // 成功
|
|
'permissionGranted'.tr, // 权限已授予
|
|
snackPosition: SnackPosition.BOTTOM,
|
|
backgroundColor: Colors.green,
|
|
colorText: Colors.white,
|
|
);
|
|
}
|
|
|
|
// 刷新界面,重新获取权限状态
|
|
Get.forceAppUpdate();
|
|
} catch (e) {
|
|
Get.snackbar(
|
|
'error'.tr, // 错误
|
|
'permissionRequestError'.tr, // 请求权限时出错
|
|
snackPosition: SnackPosition.BOTTOM,
|
|
backgroundColor: Colors.red,
|
|
colorText: Colors.white,
|
|
);
|
|
}
|
|
}
|
|
|
|
// 获取权限的用户友好名称
|
|
String _getPermissionName(Permission permission) {
|
|
switch (permission) {
|
|
case Permission.phone:
|
|
return 'phone'.tr; // 电话
|
|
case Permission.sms:
|
|
return 'sms'.tr; // 短信
|
|
case Permission.calendarWriteOnly:
|
|
return 'calendarWrite'.tr; // 日历写入
|
|
case Permission.calendarFullAccess:
|
|
return 'calendarFullAccess'.tr; // 日历完整访问
|
|
case Permission.location:
|
|
return 'locationInfo'.tr; // 位置信息
|
|
default:
|
|
return permission.toString();
|
|
}
|
|
}
|
|
|
|
// 获取权限说明文本
|
|
String getDetailedDescription(Permission permission) {
|
|
switch (permission) {
|
|
case Permission.phone:
|
|
return 'phonePermissionDetailDesc'.tr; // 拨打电话功能需要电话权限
|
|
case Permission.sms:
|
|
return 'smsPermissionDetailDesc'.tr; // 发送短信功能需要短信权限
|
|
case Permission.calendarWriteOnly:
|
|
return 'calendarWritePermissionDetailDesc'.tr; // 创建日历事件需要日历写入权限
|
|
case Permission.calendarFullAccess:
|
|
return 'calendarFullAccessPermissionDetailDesc'.tr; // 管理日历事件需要完整日历权限
|
|
case Permission.location:
|
|
return 'locationPermissionDetailDesc'.tr; // 获取位置信息需要位置权限
|
|
default:
|
|
return 'appNeedsPermissionForFullFunction'.tr; // 应用需要此权限以提供完整功能
|
|
}
|
|
}
|
|
}
|
|
|