Нет описания

photo.dart 5.4KB

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