permission_utils.dart 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. import 'package:flutter/material.dart';
  2. import 'package:permission_handler/permission_handler.dart';
  3. import '../router/my_navigator.dart';
  4. class PermissionHandler {
  5. static Future<bool> handleWith(Permission permission,
  6. {bool showTip = true}) async {
  7. if (await permission.request().isGranted ||
  8. await permission.request().isLimited) {
  9. return true;
  10. }
  11. if (await permission.isPermanentlyDenied ||
  12. await permission.isRestricted ||
  13. await permission.isDenied) {
  14. if (showTip) {
  15. MyNavigator.showDialog(
  16. builder: (_) => PermissionAlertDialog(permission: permission),
  17. );
  18. }
  19. }
  20. return false;
  21. }
  22. }
  23. class PermissionAlertDialog extends StatelessWidget {
  24. final Permission permission;
  25. const PermissionAlertDialog({Key? key, required this.permission}) : super(key: key);
  26. @override
  27. Widget build(BuildContext context) {
  28. String content = '';
  29. if (this.permission is PermissionWithService) {
  30. if (this.permission == Permission.location) {
  31. content = '请前往系统设置打开定位权限';
  32. }
  33. }
  34. if (this.permission is Permission) {
  35. if (this.permission == Permission.photos) {
  36. content = '请前往系统设置打开相册权限';
  37. } else if (this.permission == Permission.camera) {
  38. content = '请前往系统设置打开相机权限';
  39. } else if (this.permission == Permission.microphone) {
  40. content = '请前往系统设置打开麦克风权限';
  41. } else if (this.permission == Permission.storage) {
  42. content = '请前往系统设置打开存储权限';
  43. } else if (this.permission == Permission.bluetoothScan) {
  44. content = '请前往系统设置打开蓝牙权限';
  45. } else {
  46. content = '请前往系统设置打开该权限';
  47. }
  48. }
  49. return AlertDialog(
  50. title: Text('提示'),
  51. content: Text(content),
  52. backgroundColor: Colors.white,
  53. elevation: 24,
  54. shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(8)),
  55. actions: <Widget>[
  56. TextButton(
  57. child: Text('知道了'),
  58. onPressed: () {
  59. // XRouter.pop(context);
  60. },
  61. ),
  62. ],
  63. );
  64. }
  65. }