暫無描述

photo.dart 3.0KB

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