Browse Source
- 升级 permission_handler 依赖至 12.0.0+1 - 创建 PermissionUtil 工具类统一处理权限请求逻辑 - 在 AgentController 和 PairingController 中使用 PermissionUtil 替代原有权限检查方法 - 改进权限请求时的用户引导界面和提示信息 - 在权限设置页面集成新的权限请求流程newdev_shunjiawei
5 changed files with 396 additions and 209 deletions
@ -1,134 +1,202 @@ |
|||
import 'package:flutter/material.dart'; |
|||
import 'package:get/get.dart'; |
|||
import 'package:permission_handler/permission_handler.dart'; |
|||
import 'package:app_settings/app_settings.dart'; |
|||
import 'dart:developer'; |
|||
import 'logger.dart'; |
|||
|
|||
// class Logger { |
|||
// static void e(String tag, String message) { |
|||
// debugPrint('[$tag] ERROR: $message'); |
|||
// } |
|||
// } |
|||
|
|||
class PermissionUtil { |
|||
/// 检查并请求多个权限 |
|||
/// |
|||
/// [permissions] 要检查的权限列表,如 [Permission.microphone, Permission.location] |
|||
/// [rationaleTitle] 弹窗标题 |
|||
/// [rationaleMessage] 权限用途说明内容(支持多行) |
|||
/// |
|||
/// 返回是否所有权限都已被授予 |
|||
static Future<bool> checkAndRequestPermissions({ |
|||
required List<Permission> permissions, |
|||
required String rationaleTitle, |
|||
required List<String> rationaleMessage, |
|||
static final PermissionUtil instance = PermissionUtil._(); |
|||
PermissionUtil._(); |
|||
|
|||
OverlayEntry? _explanationOverlayEntry; |
|||
|
|||
Future<bool> requestPermission({ |
|||
required BuildContext context, |
|||
required Permission permissionType, |
|||
required String permissionName, |
|||
required String explanationText, |
|||
required String permanentDenialText, |
|||
String settingsButtonText = '前往设置', |
|||
String cancelButtonText = '取消', |
|||
}) async { |
|||
try { |
|||
Map<Permission, PermissionStatus> statuses = {}; |
|||
|
|||
// 检查是否都已授权 |
|||
bool allGranted = true; |
|||
for (var permission in permissions) { |
|||
final status = await permission.status; |
|||
if (!status.isGranted) { |
|||
allGranted = false; |
|||
statuses[permission] = status; |
|||
final PermissionStatus status = await permissionType.status; |
|||
log('初始权限状态 for $permissionName: $status'); |
|||
|
|||
// 处理已授权状态(包括授予状态和临时通知权限) |
|||
if (status == PermissionStatus.granted || |
|||
status == PermissionStatus.provisional) { |
|||
log('$permissionName 权限已授予或临时授权。'); |
|||
return true; |
|||
} |
|||
|
|||
// 处理永久拒绝状态 |
|||
if (status == PermissionStatus.permanentlyDenied) { |
|||
log('$permissionName 权限被永久拒绝,显示引导对话框。'); |
|||
await _showPermanentDenialDialog( |
|||
context: context, |
|||
permissionName: permissionName, |
|||
permanentDenialText: permanentDenialText, |
|||
settingsButtonText: settingsButtonText, |
|||
cancelButtonText: cancelButtonText, |
|||
); |
|||
return false; |
|||
} |
|||
|
|||
// 处理初始拒绝/未决定状态 |
|||
if (status == PermissionStatus.denied) { |
|||
log('$permissionName 权限被拒绝或未决定,显示说明横幅并请求。'); |
|||
_showExplanationOverlay( |
|||
context: context, |
|||
permissionName: permissionName, |
|||
explanationText: explanationText, |
|||
); |
|||
|
|||
final PermissionStatus newStatus = await permissionType.request(); |
|||
log('请求后 $permissionName 的新权限状态: $newStatus'); |
|||
|
|||
_hideExplanationOverlay(); |
|||
|
|||
// 处理请求后的状态 |
|||
if (newStatus == PermissionStatus.granted || |
|||
newStatus == PermissionStatus.provisional) { |
|||
log('$permissionName 权限在请求后被授予或临时授权。'); |
|||
return true; |
|||
} else if (newStatus == PermissionStatus.permanentlyDenied) { |
|||
log('$permissionName 权限在请求后被永久拒绝,再次显示引导对话框。'); |
|||
await _showPermanentDenialDialog( |
|||
context: context, |
|||
permissionName: permissionName, |
|||
permanentDenialText: permanentDenialText, |
|||
settingsButtonText: settingsButtonText, |
|||
cancelButtonText: cancelButtonText, |
|||
); |
|||
} |
|||
return false; |
|||
} |
|||
|
|||
if (allGranted) return true; |
|||
|
|||
// 判断是否包含永久拒绝 |
|||
bool hasPermanentDenied = |
|||
statuses.values.any((status) => status.isPermanentlyDenied); |
|||
|
|||
// 显示提示弹窗 |
|||
_showPermissionDialog( |
|||
isPermanent: hasPermanentDenied, |
|||
title: rationaleTitle, |
|||
messages: rationaleMessage, |
|||
onConfirm: () async { |
|||
if (hasPermanentDenied) { |
|||
AppSettings.openAppSettings(); |
|||
} else { |
|||
final newStatuses = await permissions.request(); |
|||
final allGrantedNow = |
|||
newStatuses.values.every((status) => status.isGranted); |
|||
if (!allGrantedNow) { |
|||
print("部分权限被拒绝: $newStatuses"); |
|||
} |
|||
} |
|||
}, |
|||
); |
|||
// 处理受限状态 |
|||
if (status == PermissionStatus.restricted || |
|||
status == PermissionStatus.limited) { |
|||
log('$permissionName 权限受限或受限访问: $status'); |
|||
await _showRestrictedPermissionDialog(context, permissionName); |
|||
return false; |
|||
} |
|||
|
|||
// 其他未知状态处理 |
|||
log('$permissionName 处于未知状态: $status'); |
|||
return false; |
|||
} catch (e) { |
|||
print("权限检查异常: $e"); |
|||
Logger.e('PermissionUtil', '请求 $permissionName 权限失败: $e'); |
|||
_hideExplanationOverlay(); |
|||
return false; |
|||
} |
|||
} |
|||
|
|||
/// 权限说明弹窗 |
|||
static void _showPermissionDialog({ |
|||
required bool isPermanent, |
|||
required String title, |
|||
required List<String> messages, |
|||
required VoidCallback onConfirm, |
|||
void _showExplanationOverlay({ |
|||
required BuildContext context, |
|||
required String permissionName, |
|||
required String explanationText, |
|||
}) { |
|||
Get.generalDialog( |
|||
barrierDismissible: false, |
|||
barrierLabel: '权限提示', |
|||
transitionBuilder: (context, anim1, anim2, child) { |
|||
return SlideTransition( |
|||
position: Tween<Offset>( |
|||
begin: Offset(0, -1), |
|||
end: Offset(0, 0), |
|||
).animate(CurvedAnimation(parent: anim1, curve: Curves.easeOut)), |
|||
child: child, |
|||
); |
|||
}, |
|||
pageBuilder: (context, anim1, anim2) { |
|||
return SafeArea( |
|||
child: Align( |
|||
alignment: Alignment.topCenter, |
|||
child: Padding( |
|||
padding: const EdgeInsets.only(top: 80, left: 24, right: 24), |
|||
child: Material( |
|||
borderRadius: BorderRadius.circular(12), |
|||
color: Colors.white, |
|||
child: Padding( |
|||
padding: const EdgeInsets.all(16), |
|||
child: Column( |
|||
mainAxisSize: MainAxisSize.min, |
|||
crossAxisAlignment: CrossAxisAlignment.start, |
|||
children: [ |
|||
Text(title, |
|||
style: TextStyle( |
|||
fontSize: 18, fontWeight: FontWeight.bold)), |
|||
const SizedBox(height: 12), |
|||
...messages.map((msg) => Padding( |
|||
padding: const EdgeInsets.only(bottom: 8), |
|||
child: Text("• $msg"), |
|||
)), |
|||
const SizedBox(height: 20), |
|||
Row( |
|||
mainAxisAlignment: MainAxisAlignment.end, |
|||
children: [ |
|||
TextButton( |
|||
onPressed: () => Get.back(), |
|||
child: const Text('取消'), |
|||
), |
|||
const SizedBox(width: 8), |
|||
TextButton( |
|||
onPressed: () { |
|||
Get.back(); |
|||
onConfirm(); |
|||
}, |
|||
child: Text(isPermanent ? '前往设置' : '授权'), |
|||
), |
|||
], |
|||
), |
|||
], |
|||
_hideExplanationOverlay(); // 确保移除旧叠加层 |
|||
|
|||
_explanationOverlayEntry = OverlayEntry( |
|||
builder: (context) => Positioned( |
|||
top: MediaQuery.of(context).padding.top + 10, |
|||
left: 16, |
|||
right: 16, |
|||
child: Material( |
|||
borderRadius: BorderRadius.circular(12), |
|||
elevation: 4, |
|||
color: Colors.white, |
|||
child: Padding( |
|||
padding: const EdgeInsets.all(16), |
|||
child: Column( |
|||
mainAxisSize: MainAxisSize.min, |
|||
crossAxisAlignment: CrossAxisAlignment.start, |
|||
children: [ |
|||
Text( |
|||
'需要$permissionName权限', |
|||
style: const TextStyle( |
|||
fontWeight: FontWeight.bold, |
|||
fontSize: 16, |
|||
), |
|||
), |
|||
), |
|||
const SizedBox(height: 8), |
|||
Text( |
|||
explanationText, |
|||
style: const TextStyle(fontSize: 14), |
|||
), |
|||
], |
|||
), |
|||
), |
|||
); |
|||
}, |
|||
), |
|||
), |
|||
); |
|||
|
|||
Future.delayed(Duration.zero, () { |
|||
if (_explanationOverlayEntry != null) { |
|||
Overlay.of(context).insert(_explanationOverlayEntry!); |
|||
log('权限说明叠加层已显示。'); |
|||
} |
|||
}); |
|||
} |
|||
|
|||
void _hideExplanationOverlay() { |
|||
_explanationOverlayEntry?.remove(); |
|||
_explanationOverlayEntry = null; |
|||
} |
|||
|
|||
Future<void> _showPermanentDenialDialog({ |
|||
required BuildContext context, |
|||
required String permissionName, |
|||
required String permanentDenialText, |
|||
required String settingsButtonText, |
|||
required String cancelButtonText, |
|||
}) async { |
|||
await showDialog<void>( |
|||
context: context, |
|||
barrierDismissible: false, |
|||
builder: (context) => AlertDialog( |
|||
title: Text('需要$permissionName权限'), |
|||
content: Text(permanentDenialText), |
|||
actions: [ |
|||
TextButton( |
|||
onPressed: () => Navigator.pop(context), |
|||
child: Text(cancelButtonText), |
|||
), |
|||
ElevatedButton( |
|||
onPressed: () { |
|||
Navigator.pop(context); |
|||
AppSettings.openAppSettings(); |
|||
}, |
|||
child: Text(settingsButtonText), |
|||
), |
|||
], |
|||
), |
|||
); |
|||
} |
|||
|
|||
Future<void> _showRestrictedPermissionDialog( |
|||
BuildContext context, String permissionName) async { |
|||
await showDialog( |
|||
context: context, |
|||
builder: (context) => AlertDialog( |
|||
title: Text('$permissionName权限受限'), |
|||
content: const Text('此功能在当前设备上不可用或受限,请检查设备设置或系统限制'), |
|||
actions: [ |
|||
TextButton( |
|||
onPressed: () => Navigator.pop(context), |
|||
child: const Text('确定'), |
|||
), |
|||
], |
|||
), |
|||
); |
|||
} |
|||
} |
|||
|
|||
Loading…
Reference in new issue