No Description

photo.dart 4.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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' show I18NCustomProvider, I18nProvider, CNProvider, ENProvider;
  17. export 'package:photo/src/entity/options.dart' show PickType;
  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. /// 没有授予权限的时候,会开启一个dialog去帮助用户去应用设置页面开启权限
  28. /// 确定开启设置页面,取消关闭弹窗,无论选择什么,返回值都是null
  29. ///
  30. ///
  31. /// 当用户给予权限后
  32. ///
  33. /// 当用户确定时,返回一个图片[AssetEntity]列表
  34. ///
  35. /// 当用户取消时返回一个空数组
  36. ///
  37. /// 关于参数可以查看readme文档介绍
  38. ///
  39. /// if user not grand permission, then return null and show a dialog to help user open setting.
  40. /// sure is open setting cancel ,cancel to dismiss dialog, return null
  41. ///
  42. /// when user give permission.
  43. ///
  44. /// when user sure , return a [AssetEntity] of [List]
  45. ///
  46. /// when user cancel selected,result is empty list
  47. ///
  48. /// params see readme.md
  49. static Future<List<AssetEntity>> pickAsset({
  50. @required BuildContext context,
  51. int rowCount = 4,
  52. int maxSelected = 9,
  53. double padding = 0.5,
  54. double itemRadio = 1.0,
  55. Color themeColor,
  56. Color dividerColor,
  57. Color textColor,
  58. Color disableColor,
  59. int thumbSize = 64,
  60. I18nProvider provider = I18nProvider.chinese,
  61. SortDelegate sortDelegate,
  62. CheckBoxBuilderDelegate checkBoxBuilderDelegate,
  63. LoadingDelegate loadingDelegate,
  64. PickType pickType = PickType.all,
  65. BadgeDelegate badgeDelegate = const DefaultBadgeDelegate(),
  66. }) {
  67. assert(provider != null, "provider must be not null");
  68. assert(context != null, "context must be not null");
  69. assert(pickType != null, "pickType must be not null");
  70. themeColor ??= Theme.of(context)?.primaryColor ?? Colors.black;
  71. dividerColor ??= Theme.of(context)?.dividerColor ?? Colors.grey;
  72. disableColor ??= Theme.of(context)?.disabledColor ?? Colors.grey;
  73. textColor ??= Colors.white;
  74. sortDelegate ??= SortDelegate.common;
  75. checkBoxBuilderDelegate ??= DefaultCheckBoxBuilderDelegate();
  76. loadingDelegate ??= DefaultLoadingDelegate();
  77. var options = Options(
  78. rowCount: rowCount,
  79. dividerColor: dividerColor,
  80. maxSelected: maxSelected,
  81. itemRadio: itemRadio,
  82. padding: padding,
  83. disableColor: disableColor,
  84. textColor: textColor,
  85. themeColor: themeColor,
  86. thumbSize: thumbSize,
  87. sortDelegate: sortDelegate,
  88. checkBoxBuilderDelegate: checkBoxBuilderDelegate,
  89. loadingDelegate: loadingDelegate,
  90. badgeDelegate: badgeDelegate,
  91. pickType: pickType,
  92. );
  93. return PhotoPicker()._pickAsset(
  94. context,
  95. options,
  96. provider,
  97. );
  98. }
  99. Future<List<AssetEntity>> _pickAsset(
  100. BuildContext context,
  101. Options options,
  102. I18nProvider provider,
  103. ) async {
  104. var requestPermission = await PhotoManager.requestPermission();
  105. if (requestPermission != true) {
  106. var result = await showDialog(
  107. context: context,
  108. builder: (ctx) => NotPermissionDialog(
  109. provider.getNotPermissionText(options),
  110. ),
  111. );
  112. if (result == true) {
  113. PhotoManager.openSetting();
  114. }
  115. return null;
  116. }
  117. return _openGalleryContentPage(context, options, provider);
  118. }
  119. Future<List<AssetEntity>> _openGalleryContentPage(BuildContext context, Options options, I18nProvider provider) async {
  120. return Navigator.of(context).push(
  121. MaterialPageRoute(
  122. builder: (ctx) => PhotoApp(
  123. options: options,
  124. provider: provider,
  125. ),
  126. ),
  127. );
  128. }
  129. }