No Description

photo.dart 4.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. library photo;
  2. import 'dart:async';
  3. import 'package:flutter/material.dart';
  4. import 'package:photo/src/delegate/checkbox_builder_delegate.dart';
  5. import 'package:photo/src/delegate/loading_delegate.dart';
  6. import 'package:photo/src/delegate/sort_delegate.dart';
  7. import 'package:photo/src/entity/options.dart';
  8. import 'package:photo/src/provider/i18n_provider.dart';
  9. import 'package:photo/src/ui/dialog/not_permission_dialog.dart';
  10. import 'package:photo/src/ui/photo_app.dart';
  11. import 'package:photo_manager/photo_manager.dart';
  12. export 'package:photo/src/delegate/checkbox_builder_delegate.dart';
  13. export 'package:photo/src/delegate/loading_delegate.dart';
  14. export 'package:photo/src/delegate/sort_delegate.dart';
  15. export 'package:photo/src/provider/i18n_provider.dart'
  16. show I18NCustomProvider, I18nProvider, CNProvider, ENProvider;
  17. class PhotoPicker {
  18. static PhotoPicker _instance;
  19. PhotoPicker._();
  20. factory PhotoPicker() {
  21. _instance ??= PhotoPicker._();
  22. return _instance;
  23. }
  24. static const String rootRouteName = "photo_picker_image";
  25. /// 没有授予权限的时候,会开启一个dialog去帮助用户去应用设置页面开启权限
  26. /// 确定开启设置页面,取消关闭弹窗
  27. ///
  28. ///
  29. /// 当用户给予权限后
  30. ///
  31. /// 当用户确定时,返回一个图片[AssetEntity]列表
  32. ///
  33. /// 当用户取消时
  34. ///
  35. /// if user not grand permission, then return null and show a dialog to help user open setting.
  36. /// sure is open setting cancel ,cancel to dismiss dialog
  37. ///
  38. /// when user give permission.
  39. ///
  40. /// when user sure , return a [AssetEntity] of [List]
  41. ///
  42. /// when user cancel selected,result is empty list
  43. static Future<List<AssetEntity>> pickImage({
  44. @required BuildContext context,
  45. int rowCount = 4,
  46. int maxSelected = 9,
  47. double padding = 0.5,
  48. double itemRadio = 1.0,
  49. Color themeColor,
  50. Color dividerColor,
  51. Color textColor,
  52. Color disableColor,
  53. int thumbSize = 64,
  54. I18nProvider provider = I18nProvider.chinese,
  55. SortDelegate sortDelegate,
  56. CheckBoxBuilderDelegate checkBoxBuilderDelegate,
  57. LoadingDelegate loadingDelegate,
  58. }) {
  59. assert(provider != null, "provider must be not null");
  60. assert(context != null, "context must be not null");
  61. themeColor ??= Theme.of(context)?.primaryColor ?? Colors.black;
  62. dividerColor ??= Theme.of(context)?.dividerColor ?? Colors.grey;
  63. disableColor ??= Theme.of(context)?.disabledColor ?? Colors.grey;
  64. textColor ??= Colors.white;
  65. sortDelegate ??= SortDelegate.common;
  66. checkBoxBuilderDelegate ??= DefaultCheckBoxBuilderDelegate();
  67. loadingDelegate ??= DefaultLoadingDelegate();
  68. var options = Options(
  69. rowCount: rowCount,
  70. dividerColor: dividerColor,
  71. maxSelected: maxSelected,
  72. itemRadio: itemRadio,
  73. padding: padding,
  74. disableColor: disableColor,
  75. textColor: textColor,
  76. themeColor: themeColor,
  77. thumbSize: thumbSize,
  78. sortDelegate: sortDelegate,
  79. checkBoxBuilderDelegate: checkBoxBuilderDelegate,
  80. loadingDelegate: loadingDelegate,
  81. );
  82. return PhotoPicker()._pickImage(
  83. context,
  84. options,
  85. provider,
  86. );
  87. }
  88. Future<List<AssetEntity>> _pickImage(
  89. BuildContext context,
  90. Options options,
  91. I18nProvider provider,
  92. ) async {
  93. var requestPermission = await PhotoManager.requestPermission();
  94. if (requestPermission != true) {
  95. var result = await showDialog(
  96. context: context,
  97. builder: (ctx) => NotPermissionDialog(
  98. provider.getNotPermissionText(options),
  99. ),
  100. );
  101. if (result == true) {
  102. PhotoManager.openSetting();
  103. }
  104. return null;
  105. }
  106. return _openGalleryContentPage(context, options, provider);
  107. }
  108. Future<List<AssetEntity>> _openGalleryContentPage(
  109. BuildContext context, Options options, I18nProvider provider) async {
  110. return Navigator.of(context).push(
  111. MaterialPageRoute(
  112. builder: (ctx) => PhotoApp(
  113. options: options,
  114. provider: provider,
  115. ),
  116. ),
  117. );
  118. }
  119. }