| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071 |
- import 'dart:io';
- import 'package:permission_handler/permission_handler.dart';
- enum PermissionResult {
- granted,
- denied,
- permanentlyDenied,
- }
- enum PermissionType {
- camera,
- gallery,
- microphone,
- bluetooth,
- }
- class PermissionService {
- /// 对外统一入口
- static Future<PermissionResult> request(
- PermissionType type,
- ) async {
- final permission = _mapPermission(type);
- final status = await permission.status;
- if (status.isGranted) {
- return PermissionResult.granted;
- }
- final result = await permission.request();
- if (result.isGranted) {
- return PermissionResult.granted;
- }
- if (result.isPermanentlyDenied) {
- return PermissionResult.permanentlyDenied;
- }
- return PermissionResult.denied;
- }
- /// 跳转系统设置
- static Future<void> openSetting() async {
- await openAppSettings();
- }
- /// 权限映射(平台差异处理)
- static Permission _mapPermission(PermissionType type) {
- switch (type) {
- case PermissionType.camera:
- return Permission.camera;
- case PermissionType.microphone:
- return Permission.microphone;
- case PermissionType.gallery:
- if (Platform.isAndroid) {
- return Permission.photos;
- }
- return Permission.photos;
- case PermissionType.bluetooth:
- if (Platform.isAndroid) {
- return Permission.bluetoothConnect;
- }
- return Permission.bluetooth;
- }
- }
- }
|