permission.dart 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. /*
  2. * @Author: XianKaiQun
  3. * @Date: 2020-07-30 18:08:53
  4. * @LastEditors : WuWei
  5. * @LastEditTime : 2023-07-21 11:41:02
  6. * @Description:
  7. */
  8. import 'dart:async';
  9. import 'dart:io';
  10. import 'package:flutter/material.dart';
  11. import 'package:permission_handler/permission_handler.dart';
  12. import 'package:wisdom_cli/new/utils/modal.dart';
  13. import 'package:wisdom_cli/new/utils/nav.dart';
  14. ///手机权限工具类
  15. class WPermissionUtil {
  16. WPermissionUtil._();
  17. ///申请权限,如果选择拒绝,那么会出现去设置的引导框
  18. static Future<bool> request({
  19. ///传入Permission.xxxxx
  20. required Permission service,
  21. ///引导词
  22. String deniedText = '无法获取相对应权限,是否前往设置?',
  23. String? cancelText = '取消',
  24. ///如果用户拒绝,需不需要引导去App设置中修改
  25. bool guidance = true,
  26. String? title = '',
  27. }) async {
  28. if ((await service.request()).isGranted) {
  29. return true;
  30. } else {
  31. ///打开设置
  32. if (guidance) {
  33. final confirm = await (WModalUtil.showSync(
  34. deniedText,
  35. title: '申请您的${title}权限设置',
  36. confirmText: '前往设置',
  37. cancelText: cancelText ?? '取消',
  38. )) ??
  39. false;
  40. if (confirm) {
  41. await openAppSettings();
  42. if ((await service.request()).isGranted) {
  43. return true;
  44. }
  45. }
  46. }
  47. }
  48. return false;
  49. }
  50. ///获取相册权限
  51. static Future<bool> getPhotoPermission({String? deniedText}) async {
  52. if (Platform.isAndroid) {
  53. // 检查多种可能的权限
  54. bool hasStoragePermission = await Permission.storage.isGranted;
  55. bool hasPhotosPermission = await Permission.photos.isGranted;
  56. bool hasMediaLibraryPermission = await Permission.mediaLibrary.isGranted;
  57. print(hasStoragePermission);
  58. print(hasPhotosPermission);
  59. print(hasMediaLibraryPermission);
  60. // 如果任何一个权限已授予,则认为有相册访问权限
  61. if (hasPhotosPermission ||
  62. (hasStoragePermission && hasMediaLibraryPermission)) {
  63. return true;
  64. } else {
  65. // 显示权限用途说明对话框
  66. bool shouldProceed = await showDialog(
  67. context: WNavUtil.instance!.navigatorKey.currentContext!,
  68. builder: (context) => AlertDialog(
  69. title: Text('申请您的相册访问权限'),
  70. content: Text('我们需要访问您的相册以便您可以上传头像、任务图片、提交问题反馈等。'),
  71. actions: [
  72. TextButton(
  73. onPressed: () => Navigator.of(context).pop(false),
  74. child: Text('拒绝'),
  75. ),
  76. TextButton(
  77. onPressed: () => Navigator.of(context).pop(true),
  78. child: Text('允许'),
  79. ),
  80. ],
  81. ),
  82. ) ??
  83. false;
  84. if (shouldProceed == false) return false;
  85. bool permission = await Permission.photos.request().isGranted;
  86. bool permission1 = await Permission.storage.request().isGranted;
  87. bool permission2 = await Permission.mediaLibrary.request().isGranted;
  88. if (permission || (permission1 && permission2)) {
  89. return true;
  90. } else {
  91. bool confirm = await WModalUtil.showSync(
  92. '${deniedText != null ? deniedText : '无法获取相册权限,是否前往设置'}',
  93. title: '权限申请失败',
  94. confirmText: '前往设置',
  95. ) ??
  96. false;
  97. if (confirm) {
  98. await openAppSettings();
  99. return await Permission.photos.isGranted ||
  100. (await Permission.storage.isGranted &&
  101. await Permission.mediaLibrary.isGranted);
  102. }
  103. }
  104. }
  105. return false;
  106. } else {
  107. bool isSuccess = await Permission.photos.request().isGranted;
  108. if (isSuccess) {
  109. return true;
  110. } else {
  111. final confirm = await (WModalUtil.showSync(
  112. '${deniedText != null ? deniedText : '无法获取相册权限,是否前往设置'}',
  113. title: '权限申请失败',
  114. confirmText: '前往设置',
  115. ) as Future<bool>);
  116. if (confirm) {
  117. await openAppSettings();
  118. bool permission = await Permission.photos.request().isGranted;
  119. return permission;
  120. }
  121. }
  122. return false;
  123. }
  124. }
  125. static Future<bool> getCameraPermission({String? deniedText}) async {
  126. if (Platform.isAndroid) {
  127. bool cameraPermission = await Permission.camera.isGranted;
  128. if (cameraPermission) {
  129. return true;
  130. }
  131. // 显示权限用途说明对话框
  132. bool shouldProceed = await showDialog(
  133. context: WNavUtil.instance!.navigatorKey.currentContext!,
  134. builder: (context) => AlertDialog(
  135. title: Text('申请您的相机权限'),
  136. content: Text('我们需要访问您的相机以便您可以拍摄照片上传头像、任务图片、提交问题反馈等。'),
  137. actions: [
  138. TextButton(
  139. onPressed: () => Navigator.of(context).pop(false),
  140. child: Text('拒绝'),
  141. ),
  142. TextButton(
  143. onPressed: () => Navigator.of(context).pop(true),
  144. child: Text('允许'),
  145. ),
  146. ],
  147. ),
  148. ) ??
  149. false;
  150. if (shouldProceed) {
  151. bool permission = await Permission.camera.request().isGranted;
  152. if (permission) {
  153. return true;
  154. } else {
  155. final confirm = await (WModalUtil.showSync(
  156. '${deniedText != null ? deniedText : '无法获取相机权限,是否前往设置'}',
  157. title: '权限申请失败',
  158. confirmText: '前往设置',
  159. ) as Future<bool>);
  160. if (confirm) {
  161. await openAppSettings();
  162. bool permission = await getAndroidPermission();
  163. return permission;
  164. }
  165. }
  166. return false;
  167. }
  168. return false;
  169. } else {
  170. bool isSuccess = await Permission.camera.request().isGranted;
  171. if (isSuccess) {
  172. return true;
  173. } else {
  174. final confirm = await (WModalUtil.showSync(
  175. '${deniedText != null ? deniedText : '无法获取相机权限,是否前往设置'}',
  176. title: '权限申请失败',
  177. confirmText: '前往设置',
  178. ) as Future<bool>);
  179. if (confirm) {
  180. await openAppSettings();
  181. bool permission = await Permission.camera.request().isGranted;
  182. return permission;
  183. }
  184. }
  185. }
  186. return false;
  187. }
  188. static Future<bool> getAndroidPermission() async {
  189. // final androidInfo = await DeviceInfoPlugin().androidInfo;
  190. // if (androidInfo.version.sdkInt <= 32) {
  191. // /// use [Permissions.storage.status]
  192. // bool isSuccess = await Permission.storage.request().isGranted;
  193. // if (isSuccess) {
  194. // return true;
  195. // }
  196. // } else {
  197. bool isSuccess = await Permission.photos.request().isGranted;
  198. if (isSuccess) {
  199. return true;
  200. }
  201. // }
  202. return false;
  203. }
  204. }