123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217 |
- library photo;
-
- import 'dart:async';
-
- import 'package:flutter/material.dart';
- import 'package:photo/src/entity/_theme.dart';
- import 'package:photo/src/ui/page/photo_list_page.dart';
-
- import 'package:photo_manager/photo_manager.dart';
-
- import 'package:photo/src/delegate/badge_delegate.dart';
- import 'package:photo/src/delegate/checkbox_builder_delegate.dart';
- import 'package:photo/src/delegate/loading_delegate.dart';
- import 'package:photo/src/delegate/sort_delegate.dart';
- import 'package:photo/src/entity/options.dart';
- import 'package:photo/src/provider/i18n_provider.dart';
- import 'package:photo/src/ui/dialog/not_permission_dialog.dart';
- import 'package:photo/src/ui/photo_app.dart';
-
- import 'src/provider/config_provider.dart';
- export 'package:photo/src/delegate/checkbox_builder_delegate.dart';
- export 'package:photo/src/delegate/loading_delegate.dart';
- export 'package:photo/src/delegate/sort_delegate.dart';
- export 'package:photo/src/provider/i18n_provider.dart'
- show I18NCustomProvider, I18nProvider, CNProvider, ENProvider;
- export 'package:photo/src/entity/options.dart' show PickType;
- export 'package:photo/src/delegate/badge_delegate.dart';
-
- class PhotoPicker {
- static PhotoPicker _instance;
-
- PhotoPicker._();
-
- factory PhotoPicker() {
- _instance ??= PhotoPicker._();
- return _instance;
- }
-
- static const String rootRouteName = "photo_picker_image";
-
- /// 没有授予权限的时候,会开启一个dialog去帮助用户去应用设置页面开启权限
- /// 确定开启设置页面,取消关闭弹窗,无论选择什么,返回值都是null
- ///
- ///
- /// 当用户给予权限后
- ///
- /// 当用户确定时,返回一个图片[AssetEntity]列表
- ///
- /// 当用户取消时返回一个空数组
- ///
- /// [photoPathList] 一旦设置 则 [pickType]参数无效
- ///
- /// [pickedAssetList] 已选择的asset
- ///
- /// 关于参数可以查看readme文档介绍
- ///
- /// if user not grand permission, then return null and show a dialog to help user open setting.
- /// sure is open setting cancel ,cancel to dismiss dialog, return null
- ///
- /// when user give permission.
- ///
- /// when user sure , return a [AssetEntity] of [List]
- ///
- /// when user cancel selected,result is empty list
- ///
- /// when [photoPathList] is not null , [pickType] invalid
- ///
- /// [pickedAssetList]: The results of the last selection can be passed in for easy secondary selection.
- ///
- /// params see readme.md
- static Future<List<AssetEntity>> pickAsset({
- @required BuildContext context,
- int rowCount = 4,
- int maxSelected = 9,
- double padding = 1,
- double itemRadio = 1.0,
- int thumbSize = 64,
- I18nProvider provider = I18nProvider.chinese,
- SortDelegate sortDelegate,
- LoadingDelegate loadingDelegate,
- PickType pickType = PickType.all,
- List<AssetPathEntity> photoPathList,
- List<AssetEntity> pickedAssetList,
- }) {
- assert(provider != null, "provider must be not null");
- assert(context != null, "context must be not null");
- assert(pickType != null, "pickType must be not null");
-
- sortDelegate ??= SortDelegate.common;
-
- loadingDelegate ??= DefaultLoadingDelegate();
-
- var options = Options(
- rowCount: rowCount,
- maxSelected: maxSelected,
- itemRadio: itemRadio,
- padding: padding,
- theme: PhotoTheme.fallback(context),
- thumbSize: thumbSize,
- sortDelegate: sortDelegate,
- checkBoxBuilderDelegate: DefaultCheckBoxBuilderDelegate(),
- loadingDelegate: loadingDelegate,
- badgeDelegate: DefaultBadgeDelegate(),
- pickType: pickType,
- );
-
- return PhotoPicker()._pickAsset(
- context,
- options,
- provider,
- photoPathList,
- pickedAssetList,
- );
- }
-
- Future<List<AssetEntity>> _pickAsset(
- BuildContext context,
- Options options,
- I18nProvider provider,
- List<AssetPathEntity> photoList,
- List<AssetEntity> pickedAssetList,
- ) async {
- await requestPermission(context, provider: provider);
-
- return _openGalleryContentPage(
- context,
- options,
- provider,
- photoList,
- pickedAssetList,
- );
- }
-
- static Widget buildGallery({
- @required BuildContext context,
- double padding = 1,
- @required double itemHeight,
- @required double itemWidth,
- int thumbSize = 64,
- I18nProvider provider = I18nProvider.chinese,
- SortDelegate sortDelegate,
- LoadingDelegate loadingDelegate,
- PickType pickType = PickType.all,
- List<AssetPathEntity> photoPathList,
- List<AssetEntity> pickedAssetList,
- void Function(List<AssetEntity> value) onSelected,
- }) {
- sortDelegate ??= SortDelegate.common;
-
- loadingDelegate ??= DefaultLoadingDelegate();
-
- var options = ListOptions(
- padding: padding,
- itemHeight: itemHeight,
- itemWidth: itemWidth,
- theme: PhotoTheme.fallback(context),
- thumbSize: thumbSize,
- sortDelegate: sortDelegate,
- checkBoxBuilderDelegate: DefaultCheckBoxBuilderDelegate(),
- loadingDelegate: loadingDelegate,
- badgeDelegate: DefaultBadgeDelegate(),
- pickType: pickType,
- );
- assert(provider != null, "provider must be not null");
- assert(context != null, "context must be not null");
- assert(pickType != null, "pickType must be not null");
-
- final pickerProvider = PhotoPickerProvider(
- provider: provider,
- options: options,
- pickedAssetList: pickedAssetList,
- child: PhotoListPage(
- onSelected: onSelected,
- options: options,
- photoList: photoPathList,
- ),
- );
-
- return pickerProvider;
- }
-
- Future<void> requestPermission(BuildContext context, {@required I18nProvider provider}) async {
- var requestPermission = await PhotoManager.requestPermission();
- if (requestPermission != true) {
- var result = await showDialog(
- context: context,
- builder: (ctx) => NotPermissionDialog(
- provider.getNotPermissionText(),
- ),
- );
- if (result == true) {
- PhotoManager.openSetting();
- }
- return null;
- }
- }
-
- Future<List<AssetEntity>> _openGalleryContentPage(
- BuildContext context,
- Options options,
- I18nProvider provider,
- List<AssetPathEntity> photoList,
- List<AssetEntity> pickedAssetList,
- ) async {
- return Navigator.of(context, rootNavigator: true).push(
- MaterialPageRoute(
- builder: (ctx) => PhotoApp(
- options: options,
- provider: provider,
- photoList: photoList,
- pickedAssetList: pickedAssetList,
- ),
- ),
- );
- }
- }
|