asset_picker.dart 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. /*
  2. * @Author: XianKaiQun
  3. * @Date: 2020-08-31 11:45:11
  4. * @LastEditors : WuWei
  5. * @LastEditTime : 2023-07-25 11:39:50
  6. * @Description:
  7. */
  8. import 'dart:async';
  9. import 'dart:io' as IOPlatfrom;
  10. import 'package:flutter/material.dart';
  11. import 'package:image_picker/image_picker.dart';
  12. import 'package:image_picker_platform_interface/src/types/image_source.dart'
  13. as Isource;
  14. import 'package:permission_handler/permission_handler.dart';
  15. import 'package:wisdom_cli/wisdom_cli.dart';
  16. import '../views/album_view.dart';
  17. export '../views/album_view.dart' show FileEntity;
  18. ///图片选择来源
  19. ///
  20. ///[camera]相机,
  21. ///[gallery]相册,
  22. ///[cameraAndGallery]和相册
  23. enum WImageSource { camera, gallery, cameraAndGallery }
  24. ///静态资源选择器
  25. class WAssetPickerUtil {
  26. WAssetPickerUtil._();
  27. ///获取相册权限
  28. static Future<bool> getPhotoPermission() async {
  29. if (IOPlatfrom.Platform.isAndroid) {
  30. bool permission = await WPermissionUtil.getPhotoPermission();
  31. if (permission) {
  32. return true;
  33. } else {
  34. final confirm = await (WModalUtil.showSync(
  35. '无法获取相机权限,是否前往设置',
  36. title: '权限申请失败',
  37. confirmText: '前往设置',
  38. ) as Future<bool>);
  39. if (confirm) {
  40. await openAppSettings();
  41. bool permission = await WPermissionUtil.getPhotoPermission();
  42. return permission;
  43. }
  44. }
  45. return false;
  46. } else {
  47. bool isSuccess = await Permission.photos.request().isGranted;
  48. if (isSuccess) {
  49. return true;
  50. } else {
  51. final confirm = await (WModalUtil.showSync(
  52. '无法获取相机权限,是否前往设置',
  53. title: '权限申请失败',
  54. confirmText: '前往设置',
  55. ) as Future<bool>);
  56. if (confirm) {
  57. await openAppSettings();
  58. bool permission = await Permission.photos.request().isGranted;
  59. return permission;
  60. }
  61. }
  62. }
  63. return false;
  64. }
  65. /// 拍照或者从相机中选择
  66. /// [maxCount]最大选择数目,
  67. /// [source] 图片来源
  68. /// [sourceCallBack] 回调图片来源(相机或相册)
  69. static Future<List<FileEntity>> image({
  70. int maxCount = 9,
  71. WImageSource source = WImageSource.cameraAndGallery,
  72. void Function(WImageSource source)? sourceCallBack,
  73. }) async {
  74. final completer = Completer<List<FileEntity>>();
  75. ///相机
  76. Future<List<FileEntity>?> _camera() async {
  77. if (!await WPermissionUtil.getCameraPermission()) return null;
  78. final res = await ImagePicker().pickImage(
  79. source: Isource.ImageSource.camera,
  80. imageQuality: 50,
  81. );
  82. if (res == null) return null;
  83. if (sourceCallBack != null) sourceCallBack(WImageSource.camera);
  84. return [FileEntity(file: IOPlatfrom.File(res.path))];
  85. }
  86. ///相册
  87. Future<List<FileEntity>?> _gallery() async {
  88. bool isOk = await WPermissionUtil.getPhotoPermission();
  89. if (!isOk) return null;
  90. if (sourceCallBack != null)
  91. sourceCallBack(
  92. WImageSource.gallery,
  93. );
  94. return WAlbumView.pushRoute(maxCount: maxCount);
  95. }
  96. switch (source) {
  97. case WImageSource.camera:
  98. completer.complete(await _camera());
  99. break;
  100. case WImageSource.gallery:
  101. completer.complete(await _gallery());
  102. break;
  103. default:
  104. ///默认两个都可以(弹出action选择)
  105. _ImagePickerActionSheet.show(
  106. onCameraTap: () async => completer.complete(await _camera() ?? []),
  107. onGalleryTap: () async => completer.complete(await _gallery() ?? []),
  108. onCancelTap: () => completer.complete([]),
  109. );
  110. }
  111. return completer.future;
  112. }
  113. }
  114. ///弹出层样式
  115. ///
  116. ///
  117. ///
  118. ///
  119. class _ImagePickerActionSheet extends StatelessWidget {
  120. const _ImagePickerActionSheet({
  121. Key? key,
  122. this.onCameraTap,
  123. this.onGalleryTap,
  124. this.onCancelTap,
  125. }) : super(key: key);
  126. final Function()? onCameraTap;
  127. final Function()? onGalleryTap;
  128. final Function()? onCancelTap;
  129. static Future<void> show({
  130. Function()? onCameraTap,
  131. Function()? onGalleryTap,
  132. Function()? onCancelTap,
  133. }) {
  134. return WNavUtil.instance!.showModalBottomSheet(
  135. backgroundColor: Color(0xfff7f7f7),
  136. builder: (_) => _ImagePickerActionSheet(
  137. onCameraTap: onCameraTap,
  138. onGalleryTap: onGalleryTap,
  139. onCancelTap: onCancelTap,
  140. ),
  141. );
  142. }
  143. @override
  144. Widget build(BuildContext context) {
  145. return SafeArea(
  146. child: Column(
  147. crossAxisAlignment: CrossAxisAlignment.stretch,
  148. mainAxisSize: MainAxisSize.min,
  149. children: ListTile.divideTiles(
  150. context: context,
  151. tiles: [
  152. Wisdom(
  153. onTap: () {
  154. Navigator.pop(context);
  155. onCameraTap!();
  156. },
  157. alignment: Alignment.center,
  158. color: Colors.white,
  159. height: 44.pt,
  160. child: Text('拍照'),
  161. ),
  162. Wisdom(
  163. onTap: () {
  164. Navigator.pop(context);
  165. onGalleryTap!();
  166. },
  167. alignment: Alignment.center,
  168. color: Colors.white,
  169. height: 44.pt,
  170. child: Text('从相册选择'),
  171. ),
  172. Wisdom(
  173. onTap: () {
  174. Navigator.pop(context);
  175. onCancelTap!();
  176. },
  177. margin: EdgeInsets.only(top: 15.pt),
  178. alignment: Alignment.center,
  179. color: Colors.white,
  180. height: 44.pt,
  181. child: Text('取消'),
  182. ),
  183. ],
  184. ).toList(),
  185. ),
  186. );
  187. }
  188. }