Brak opisu

photo.dart 5.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. library photo;
  2. import 'dart:async';
  3. import 'package:flutter/material.dart';
  4. import 'package:photo_manager/photo_manager.dart';
  5. import 'package:photo/src/delegate/badge_delegate.dart';
  6. import 'package:photo/src/delegate/checkbox_builder_delegate.dart';
  7. import 'package:photo/src/delegate/loading_delegate.dart';
  8. import 'package:photo/src/delegate/sort_delegate.dart';
  9. import 'package:photo/src/entity/options.dart';
  10. import 'package:photo/src/provider/i18n_provider.dart';
  11. import 'package:photo/src/ui/dialog/not_permission_dialog.dart';
  12. import 'package:photo/src/ui/photo_app.dart';
  13. export 'package:photo/src/delegate/checkbox_builder_delegate.dart';
  14. export 'package:photo/src/delegate/loading_delegate.dart';
  15. export 'package:photo/src/delegate/sort_delegate.dart';
  16. export 'package:photo/src/provider/i18n_provider.dart'
  17. show I18NCustomProvider, I18nProvider, CNProvider, ENProvider;
  18. export 'package:photo/src/entity/options.dart' show PickType;
  19. export 'package:photo/src/delegate/badge_delegate.dart';
  20. class PhotoPicker {
  21. static PhotoPicker _instance;
  22. PhotoPicker._();
  23. factory PhotoPicker() {
  24. _instance ??= PhotoPicker._();
  25. return _instance;
  26. }
  27. static const String rootRouteName = "photo_picker_image";
  28. /// 没有授予权限的时候,会开启一个dialog去帮助用户去应用设置页面开启权限
  29. /// 确定开启设置页面,取消关闭弹窗,无论选择什么,返回值都是null
  30. ///
  31. ///
  32. /// 当用户给予权限后
  33. ///
  34. /// 当用户确定时,返回一个图片[AssetEntity]列表
  35. ///
  36. /// 当用户取消时返回一个空数组
  37. ///
  38. /// [photoPathList] 一旦设置 则 [pickType]参数无效
  39. ///
  40. /// [pickedAssetList] 已选择的asset
  41. ///
  42. /// 关于参数可以查看readme文档介绍
  43. ///
  44. /// if user not grand permission, then return null and show a dialog to help user open setting.
  45. /// sure is open setting cancel ,cancel to dismiss dialog, return null
  46. ///
  47. /// when user give permission.
  48. ///
  49. /// when user sure , return a [AssetEntity] of [List]
  50. ///
  51. /// when user cancel selected,result is empty list
  52. ///
  53. /// when [photoPathList] is not null , [pickType] invalid
  54. ///
  55. /// [pickedAssetList]: The results of the last selection can be passed in for easy secondary selection.
  56. ///
  57. /// params see readme.md
  58. static Future<List<AssetEntity>> pickAsset({
  59. @required BuildContext context,
  60. int rowCount = 4,
  61. int maxSelected = 9,
  62. double padding = 0.5,
  63. double itemRadio = 1.0,
  64. Color themeColor,
  65. Color dividerColor,
  66. Color textColor,
  67. Color disableColor,
  68. int thumbSize = 64,
  69. I18nProvider provider = I18nProvider.chinese,
  70. SortDelegate sortDelegate,
  71. CheckBoxBuilderDelegate checkBoxBuilderDelegate,
  72. LoadingDelegate loadingDelegate,
  73. PickType pickType = PickType.all,
  74. BadgeDelegate badgeDelegate = const DefaultBadgeDelegate(),
  75. List<AssetPathEntity> photoPathList,
  76. List<AssetEntity> pickedAssetList,
  77. }) {
  78. assert(provider != null, "provider must be not null");
  79. assert(context != null, "context must be not null");
  80. assert(pickType != null, "pickType must be not null");
  81. themeColor ??= Theme.of(context)?.primaryColor ?? Colors.black;
  82. dividerColor ??= Theme.of(context)?.dividerColor ?? Colors.grey;
  83. disableColor ??= Theme.of(context)?.disabledColor ?? Colors.grey;
  84. textColor ??= Colors.white;
  85. sortDelegate ??= SortDelegate.common;
  86. checkBoxBuilderDelegate ??= DefaultCheckBoxBuilderDelegate();
  87. loadingDelegate ??= DefaultLoadingDelegate();
  88. var options = Options(
  89. rowCount: rowCount,
  90. dividerColor: dividerColor,
  91. maxSelected: maxSelected,
  92. itemRadio: itemRadio,
  93. padding: padding,
  94. disableColor: disableColor,
  95. textColor: textColor,
  96. themeColor: themeColor,
  97. thumbSize: thumbSize,
  98. sortDelegate: sortDelegate,
  99. checkBoxBuilderDelegate: checkBoxBuilderDelegate,
  100. loadingDelegate: loadingDelegate,
  101. badgeDelegate: badgeDelegate,
  102. pickType: pickType,
  103. );
  104. return PhotoPicker()._pickAsset(
  105. context,
  106. options,
  107. provider,
  108. photoPathList,
  109. pickedAssetList,
  110. );
  111. }
  112. Future<List<AssetEntity>> _pickAsset(
  113. BuildContext context,
  114. Options options,
  115. I18nProvider provider,
  116. List<AssetPathEntity> photoList,
  117. List<AssetEntity> pickedAssetList,
  118. ) async {
  119. var requestPermission = await PhotoManager.requestPermission();
  120. if (requestPermission != true) {
  121. var result = await showDialog(
  122. context: context,
  123. builder: (ctx) => NotPermissionDialog(
  124. provider.getNotPermissionText(options),
  125. ),
  126. );
  127. if (result == true) {
  128. PhotoManager.openSetting();
  129. }
  130. return null;
  131. }
  132. return _openGalleryContentPage(
  133. context,
  134. options,
  135. provider,
  136. photoList,
  137. pickedAssetList,
  138. );
  139. }
  140. Future<List<AssetEntity>> _openGalleryContentPage(
  141. BuildContext context,
  142. Options options,
  143. I18nProvider provider,
  144. List<AssetPathEntity> photoList,
  145. List<AssetEntity> pickedAssetList,
  146. ) async {
  147. return Navigator.of(context, rootNavigator: true).push(
  148. MaterialPageRoute(
  149. builder: (ctx) => PhotoApp(
  150. options: options,
  151. provider: provider,
  152. photoList: photoList,
  153. pickedAssetList: pickedAssetList,
  154. ),
  155. ),
  156. );
  157. }
  158. }