説明なし

photo.dart 3.0KB

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