Hibok
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.
 
 
 
 
 
 

147 rader
3.8 KiB

  1. library photo;
  2. import 'dart:async';
  3. import 'package:chat/utils/CustomUI.dart';
  4. import 'package:flutter/material.dart';
  5. import 'package:photo_manager/photo_manager.dart';
  6. import 'delegate/loading_delegate.dart';
  7. import 'delegate/sort_delegate.dart';
  8. import 'entity/options.dart';
  9. import 'ui/photo_app.dart';
  10. class PhotoPicker {
  11. static PhotoPicker _instance;
  12. PhotoPicker._();
  13. factory PhotoPicker() {
  14. _instance ??= PhotoPicker._();
  15. return _instance;
  16. }
  17. static const String rootRouteName = "photo_picker_image";
  18. /// 没有授予权限的时候,会开启一个dialog去帮助用户去应用设置页面开启权限
  19. /// 确定开启设置页面,取消关闭弹窗,无论选择什么,返回值都是null
  20. ///
  21. ///
  22. /// 当用户给予权限后
  23. ///
  24. /// 当用户确定时,返回一个图片[AssetEntity]列表
  25. ///
  26. /// 当用户取消时返回一个空数组
  27. ///
  28. /// [photoPathList] 一旦设置 则 [pickType]参数无效
  29. ///
  30. /// 关于参数可以查看readme文档介绍
  31. ///
  32. /// if user not grand permission, then return null and show a dialog to help user open setting.
  33. /// sure is open setting cancel ,cancel to dismiss dialog, return null
  34. ///
  35. /// when user give permission.
  36. ///
  37. /// when user sure , return a [AssetEntity] of [List]
  38. ///
  39. /// when user cancel selected,result is empty list
  40. ///
  41. /// when [photoPathList] is not null , [pickType] invalid
  42. ///
  43. /// params see readme.md
  44. static Future<List<AssetEntity>> pickAsset({
  45. @required BuildContext context,
  46. int rowCount = 4,
  47. int maxSelected = 9,
  48. double padding = 0.5,
  49. double itemRadio = 1.0,
  50. Color themeColor,
  51. Color dividerColor,
  52. Color textColor,
  53. Color disableColor,
  54. int thumbSize = 64,
  55. SortDelegate sortDelegate,
  56. LoadingDelegate loadingDelegate,
  57. PickType pickType = PickType.onlyImage,
  58. List<AssetPathEntity> photoPathList,
  59. }) {
  60. assert(context != null, "context must be not null");
  61. assert(pickType != null, "pickType must be not null");
  62. themeColor ??= Theme.of(context)?.primaryColor ?? Colors.white;
  63. dividerColor ??= Theme.of(context)?.dividerColor ?? Colors.grey;
  64. disableColor ??= Theme.of(context)?.disabledColor ?? Colors.grey;
  65. textColor ??= Colors.white;
  66. sortDelegate ??= SortDelegate.common;
  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. loadingDelegate: loadingDelegate,
  80. pickType: pickType,
  81. );
  82. return PhotoPicker()._pickAsset(
  83. context,
  84. options,
  85. photoPathList,
  86. );
  87. }
  88. Future<List<AssetEntity>> _pickAsset(
  89. BuildContext context,
  90. Options options,
  91. List<AssetPathEntity> photoList,
  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. // );
  99. // if (result == true) {
  100. // ;
  101. // }
  102. // return null;
  103. // }
  104. if(await CustomUI.showPhotoPermissionSetting(context)){
  105. return _openGalleryContentPage(context, options, photoList);
  106. }else{
  107. return null;
  108. }
  109. // return _openGalleryContentPage(context, options, photoList);
  110. }
  111. Future<List<AssetEntity>> _openGalleryContentPage(
  112. BuildContext context,
  113. Options options,
  114. List<AssetPathEntity> photoList,
  115. ) async {
  116. return Navigator.of(context, rootNavigator: true).push(
  117. MaterialPageRoute(
  118. builder: (ctx) => PhotoApp(
  119. options: options,
  120. photoList: photoList,
  121. ),
  122. ),
  123. );
  124. }
  125. }