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.
297 lines
8.6 KiB
297 lines
8.6 KiB
import 'package:flutter/material.dart';
|
|
import 'package:get/get.dart';
|
|
import 'package:permission_handler/permission_handler.dart';
|
|
import 'package:flutter_screenutil/flutter_screenutil.dart';
|
|
import 'dart:ui';
|
|
import 'dart:io' show Platform;
|
|
|
|
import '../controllers/permissions_controller.dart';
|
|
import '../models/permission_group.dart';
|
|
|
|
class PermissionsView extends GetView<PermissionsController> {
|
|
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: Obx(() {
|
|
if (controller.permissionStatuses.isEmpty) {
|
|
return const Center(child: CircularProgressIndicator());
|
|
}
|
|
|
|
return RefreshIndicator(
|
|
onRefresh: controller.checkAllPermissions,
|
|
child: SingleChildScrollView(
|
|
padding: EdgeInsets.symmetric(horizontal: 16.w, vertical: 16.h),
|
|
child: _buildPermissionGroups(isDarkMode),
|
|
),
|
|
);
|
|
}),
|
|
);
|
|
}
|
|
|
|
Widget _buildPermissionGroups(bool isDarkMode) {
|
|
return FutureBuilder<List<PermissionGroupModel>>(
|
|
future: PermissionGroup.getPlatformGroups(),
|
|
builder: (context, snapshot) {
|
|
if (snapshot.connectionState == ConnectionState.waiting) {
|
|
return const Center(child: CircularProgressIndicator());
|
|
}
|
|
|
|
if (snapshot.hasError) {
|
|
return Center(child: Text('加载权限组失败: ${snapshot.error}'));
|
|
}
|
|
|
|
final groups = snapshot.data ?? [];
|
|
final List<Widget> widgets = [];
|
|
|
|
for (int i = 0; i < groups.length; i++) {
|
|
final group = groups[i];
|
|
|
|
// 过滤平台特定权限
|
|
if (_shouldShowGroup(group)) {
|
|
widgets.add(_buildPermissionSection(
|
|
group.titleKey,
|
|
group.icon,
|
|
group.permissions,
|
|
controller.permissionStatuses,
|
|
isDarkMode,
|
|
));
|
|
|
|
if (i < groups.length - 1) {
|
|
widgets.add(SizedBox(height: 20.h));
|
|
}
|
|
}
|
|
}
|
|
|
|
return Column(children: widgets);
|
|
},
|
|
);
|
|
}
|
|
|
|
bool _shouldShowGroup(PermissionGroupModel group) {
|
|
// Android特定权限组
|
|
if (group.titleKey == 'communicationPermissions' ||
|
|
group.titleKey == 'alarmPermissions') {
|
|
return Platform.isAndroid;
|
|
}
|
|
// 蓝牙权限组:Android 12+或iOS才显示
|
|
if (group.titleKey == 'bluetoothPermissions') {
|
|
return controller.shouldShowBluetoothPermissions();
|
|
}
|
|
// 存储权限组:Android 10及以下版本或iOS才显示
|
|
if (group.titleKey == 'storagePermissions') {
|
|
return controller.shouldShowStoragePermissions();
|
|
}
|
|
// 通知权限和麦克风权限在所有平台显示
|
|
if (group.titleKey == 'notificationPermissions' ||
|
|
group.titleKey == 'microphonePermissions') {
|
|
return true;
|
|
}
|
|
return true;
|
|
}
|
|
|
|
Widget _buildPermissionSection(
|
|
String titleKey,
|
|
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(
|
|
titleKey.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 = [];
|
|
|
|
for (int i = 0; i < permissions.length; i++) {
|
|
final permission = permissions[i];
|
|
final status = statuses[permission] ?? PermissionStatus.denied;
|
|
|
|
items.add(
|
|
_buildPermissionItem(
|
|
permission: permission,
|
|
status: status,
|
|
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 Permission permission,
|
|
required PermissionStatus status,
|
|
required bool isDarkMode,
|
|
}) {
|
|
return Padding(
|
|
padding: EdgeInsets.all(16.w),
|
|
child: Row(
|
|
mainAxisAlignment: MainAxisAlignment.spaceBetween,
|
|
children: [
|
|
Expanded(
|
|
child: Column(
|
|
crossAxisAlignment: CrossAxisAlignment.start,
|
|
children: [
|
|
Text(
|
|
controller.getPermissionName(permission),
|
|
style: TextStyle(
|
|
fontSize: 16.sp,
|
|
fontWeight: FontWeight.w500,
|
|
color: isDarkMode ? Colors.white : Colors.black87,
|
|
),
|
|
),
|
|
SizedBox(height: 4.h),
|
|
Text(
|
|
controller.getPermissionDescription(permission),
|
|
style: TextStyle(
|
|
fontSize: 12.sp,
|
|
color: isDarkMode ? Colors.white70 : Colors.grey[600],
|
|
),
|
|
),
|
|
SizedBox(height: 4.h),
|
|
_buildStatusText(status, isDarkMode),
|
|
],
|
|
),
|
|
),
|
|
Obx(() => Switch(
|
|
value: status.isGranted,
|
|
onChanged: controller.isRequestingPermission.value
|
|
? null
|
|
: (value) =>
|
|
_handlePermissionToggle(permission, status, value),
|
|
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,
|
|
),
|
|
);
|
|
}
|
|
|
|
void _handlePermissionToggle(
|
|
Permission permission,
|
|
PermissionStatus status,
|
|
bool value,
|
|
) {
|
|
if (value) {
|
|
// 请求权限
|
|
controller.requestPermission(permission);
|
|
} else if (status.isGranted) {
|
|
// 已授权但用户想关闭,引导到系统设置
|
|
controller.openSettings();
|
|
}
|
|
}
|
|
}
|
|
|