暫無描述

photo.dart 4.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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. /// [photoList] 一旦设置 则 [pickType]参数无效
  39. ///
  40. /// 关于参数可以查看readme文档介绍
  41. ///
  42. /// if user not grand permission, then return null and show a dialog to help user open setting.
  43. /// sure is open setting cancel ,cancel to dismiss dialog, return null
  44. ///
  45. /// when user give permission.
  46. ///
  47. /// when user sure , return a [AssetEntity] of [List]
  48. ///
  49. /// when user cancel selected,result is empty list
  50. ///
  51. /// when [photoList] is not null , [pickType] invalid
  52. ///
  53. /// params see readme.md
  54. static Future<List<AssetEntity>> pickAsset({
  55. @required BuildContext context,
  56. int rowCount = 4,
  57. int maxSelected = 9,
  58. double padding = 0.5,
  59. double itemRadio = 1.0,
  60. Color themeColor,
  61. Color dividerColor,
  62. Color textColor,
  63. Color disableColor,
  64. int thumbSize = 64,
  65. I18nProvider provider = I18nProvider.chinese,
  66. SortDelegate sortDelegate,
  67. CheckBoxBuilderDelegate checkBoxBuilderDelegate,
  68. LoadingDelegate loadingDelegate,
  69. PickType pickType = PickType.all,
  70. BadgeDelegate badgeDelegate = const DefaultBadgeDelegate(),
  71. List<AssetPathEntity> photoList,
  72. }) {
  73. assert(provider != null, "provider must be not null");
  74. assert(context != null, "context must be not null");
  75. assert(pickType != null, "pickType must be not null");
  76. themeColor ??= Theme.of(context)?.primaryColor ?? Colors.black;
  77. dividerColor ??= Theme.of(context)?.dividerColor ?? Colors.grey;
  78. disableColor ??= Theme.of(context)?.disabledColor ?? Colors.grey;
  79. textColor ??= Colors.white;
  80. sortDelegate ??= SortDelegate.common;
  81. checkBoxBuilderDelegate ??= DefaultCheckBoxBuilderDelegate();
  82. loadingDelegate ??= DefaultLoadingDelegate();
  83. var options = Options(
  84. rowCount: rowCount,
  85. dividerColor: dividerColor,
  86. maxSelected: maxSelected,
  87. itemRadio: itemRadio,
  88. padding: padding,
  89. disableColor: disableColor,
  90. textColor: textColor,
  91. themeColor: themeColor,
  92. thumbSize: thumbSize,
  93. sortDelegate: sortDelegate,
  94. checkBoxBuilderDelegate: checkBoxBuilderDelegate,
  95. loadingDelegate: loadingDelegate,
  96. badgeDelegate: badgeDelegate,
  97. pickType: pickType,
  98. );
  99. return PhotoPicker()._pickAsset(
  100. context,
  101. options,
  102. provider,
  103. photoList,
  104. );
  105. }
  106. Future<List<AssetEntity>> _pickAsset(
  107. BuildContext context,
  108. Options options,
  109. I18nProvider provider,
  110. List<AssetPathEntity> photoList,
  111. ) async {
  112. var requestPermission = await PhotoManager.requestPermission();
  113. if (requestPermission != true) {
  114. var result = await showDialog(
  115. context: context,
  116. builder: (ctx) => NotPermissionDialog(
  117. provider.getNotPermissionText(options),
  118. ),
  119. );
  120. if (result == true) {
  121. PhotoManager.openSetting();
  122. }
  123. return null;
  124. }
  125. return _openGalleryContentPage(context, options, provider, photoList);
  126. }
  127. Future<List<AssetEntity>> _openGalleryContentPage(
  128. BuildContext context,
  129. Options options,
  130. I18nProvider provider,
  131. List<AssetPathEntity> photoList,
  132. ) async {
  133. return Navigator.of(context).push(
  134. MaterialPageRoute(
  135. builder: (ctx) => PhotoApp(
  136. options: options,
  137. provider: provider,
  138. photoList: photoList,
  139. ),
  140. ),
  141. );
  142. }
  143. }