permission_engine.dart 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. import 'package:flutter/material.dart';
  2. import 'package:permission_handler/permission_handler.dart';
  3. import 'package:photo_manager/photo_manager.dart';
  4. import 'package:shared/utils/device_utils.dart';
  5. import 'package:shared/utils/log_utils.dart';
  6. import 'package:widgets/dialog/app_default_dialog.dart';
  7. import 'package:widgets/dialog/permission_desc_dialog.dart';
  8. import '../dialog/dialog_engine.dart';
  9. /*
  10. * 动态权限的申请与校验
  11. */
  12. class PermissionEngine {
  13. // 私有构造函数
  14. PermissionEngine._privateConstructor();
  15. // 单例实例
  16. static final PermissionEngine _instance = PermissionEngine._privateConstructor();
  17. // 获取单例实例的访问点
  18. factory PermissionEngine() {
  19. return _instance;
  20. }
  21. /// 申请多媒体相册权限
  22. void requestPhotosPermission(void Function() success) async {
  23. //相册的选项
  24. if (DeviceUtils.isIOS) {
  25. //申请授权
  26. final value = await PhotoManager.requestPermissionExtend();
  27. if (value.hasAccess) {
  28. //已授权
  29. Log.d("相册已授权");
  30. success();
  31. } else if (value == PermissionState.limited) {
  32. Log.d("相册访问受限,去设置受限");
  33. PhotoManager.presentLimited();
  34. } else {
  35. Log.d("相册无授权,去设置");
  36. DialogEngine.show(
  37. widget: AppDefaultDialog(
  38. title: "提示",
  39. message: '无相册权限,前往设置',
  40. confirmAction: () {
  41. PhotoManager.openSetting();
  42. },
  43. ),
  44. );
  45. }
  46. } else {
  47. //Android是否有SD卡权限
  48. var status = await Permission.storage.status;
  49. late PermissionState ps;
  50. if (status.isGranted) {
  51. // 已经授权
  52. success();
  53. } else {
  54. // 未授权,则准备发起一次申请
  55. var permissionRequestFuture = PhotoManager.requestPermissionExtend();
  56. // 延迟500毫秒的Future
  57. var delayFuture = Future.delayed(Duration(milliseconds: 500), () => 'delay');
  58. // 使用Future.any等待上述两个Future中的任何一个完成
  59. var firstCompleted = await Future.any([permissionRequestFuture, delayFuture]);
  60. // 判断响应结果
  61. if (firstCompleted == 'delay') {
  62. Log.d("判断响应结果:1");
  63. // 如果是延迟Future完成了,表示500毫秒内没有获得权限响应,显示对话框
  64. _showPermissionDialog("“YYBusiness”想访问你的多媒体相册 用于图片上传,图片保存等功能,请允许我获取您的权限");
  65. // 再次等待权限请求结果
  66. ps = await permissionRequestFuture;
  67. DialogEngine.dismiss(tag: "permission");
  68. } else {
  69. Log.d("判断响应结果:2");
  70. // 权限请求已完成,立刻取消对话框展示(如果已经展示的话)
  71. DialogEngine.dismiss(tag: "permission");
  72. ps = firstCompleted as PermissionState;
  73. }
  74. if (ps.isAuth) {
  75. // 用户授权
  76. success();
  77. } else {
  78. // 权限被拒绝
  79. await DialogEngine.show(
  80. widget: AppDefaultDialog(
  81. message: "请到您的手机设置打开相册的权限",
  82. title: "提醒",
  83. confirmAction: () {
  84. openAppSettings();
  85. }),
  86. );
  87. }
  88. }
  89. }
  90. }
  91. /// 申请相机权限
  92. void requestCameraPermission(void Function() success) async {
  93. // 获取当前的权限
  94. var status = await Permission.camera.status;
  95. if (status.isGranted) {
  96. // 已经授权
  97. success();
  98. } else {
  99. // 未授权,则准备发起一次申请
  100. var permissionRequestFuture = Permission.camera.request();
  101. // 延迟500毫秒的Future
  102. var delayFuture = Future.delayed(Duration(milliseconds: 500), () => 'delay');
  103. // 使用Future.any等待上述两个Future中的任何一个完成
  104. var firstCompleted = await Future.any([permissionRequestFuture, delayFuture]);
  105. // 判断响应结果
  106. if (firstCompleted == 'delay') {
  107. // 如果是延迟Future完成了,表示500毫秒内没有获得权限响应,显示对话框
  108. _showPermissionDialog("“YYBusiness”申请调用您的相机权限 用于使用拍摄头像,图片上传保存等功能,请允许我获取您的权限");
  109. // 再次等待权限请求结果
  110. status = await permissionRequestFuture;
  111. DialogEngine.dismiss(tag: "permission");
  112. } else {
  113. // 权限请求已完成,立刻取消对话框展示(如果已经展示的话)
  114. DialogEngine.dismiss(tag: "permission");
  115. status = firstCompleted as PermissionStatus;
  116. }
  117. if (status.isGranted) {
  118. // 用户授权
  119. success();
  120. } else {
  121. // 权限被拒绝
  122. await DialogEngine.show(
  123. widget: AppDefaultDialog(
  124. message: "请到您的手机设置打开相机的权限",
  125. title: "提醒",
  126. confirmAction: () {
  127. openAppSettings();
  128. }),
  129. );
  130. }
  131. }
  132. }
  133. /// 校验并申请定位权限
  134. Future<bool> requestLocationPermission() async {
  135. // 获取当前的权限
  136. var status = await Permission.location.status;
  137. if (status.isGranted) {
  138. // 已经授权
  139. return true;
  140. } else {
  141. // 未授权,则准备发起一次申请
  142. var permissionRequestFuture = Permission.location.request();
  143. // 延迟500毫秒的Future
  144. var delayFuture = Future.delayed(Duration(milliseconds: 500), () => 'delay');
  145. // 使用Future.any等待上述两个Future中的任何一个完成
  146. var firstCompleted = await Future.any([permissionRequestFuture, delayFuture]);
  147. // 判断响应结果
  148. if (firstCompleted == 'delay') {
  149. // 如果是延迟Future完成了,表示500毫秒内没有获得权限响应,显示对话框
  150. _showPermissionDialog("“YYBusiness”想访问您的定位权限获取您的位置来推荐附近的工作");
  151. // 再次等待权限请求结果
  152. status = await permissionRequestFuture;
  153. DialogEngine.dismiss(tag: "permission");
  154. } else {
  155. Log.d("权限请求已完成,立刻取消对话框展示");
  156. // 权限请求已完成,立刻取消对话框展示(如果已经展示的话)
  157. DialogEngine.dismiss(tag: "permission");
  158. status = firstCompleted as PermissionStatus;
  159. }
  160. if (status.isGranted) {
  161. // 用户授权
  162. return true;
  163. } else {
  164. // 权限被拒绝
  165. await DialogEngine.show(
  166. widget: AppDefaultDialog(
  167. message: "请到您的手机设置打开定位的权限",
  168. title: "提醒",
  169. confirmAction: () {
  170. openAppSettings();
  171. }),
  172. );
  173. return false;
  174. }
  175. }
  176. }
  177. //顶部展示权限声明详情弹窗
  178. void _showPermissionDialog(String desc) {
  179. DialogEngine.show(
  180. clickMaskDismiss: false,
  181. backDismiss: true,
  182. tag: "permission",
  183. maskColor: Colors.transparent,
  184. widget: PermissionDescDialog(desc),
  185. );
  186. }
  187. }