library photo; import 'dart:async'; import 'package:flutter/material.dart'; import 'package:image_picker/image_picker.dart'; import 'package:photo/src/entity/_theme.dart'; import 'package:photo/src/permission_utils.dart'; import 'package:photo/src/ui/page/photo_list_page.dart'; import 'package:photo/src/ui/page/photo_main_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:provider/provider.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, PickedEntity; export 'package:photo/src/delegate/badge_delegate.dart'; export 'package:photo/src/entity/_theme.dart'; export 'package:image_picker/image_picker.dart' show CameraDevice, PickedFile; export 'package:photo_manager/photo_manager.dart' show AssetEntity, AssetType; class PhotoPicker { static PhotoPicker _instance; PhotoPicker._(); factory PhotoPicker() { _instance ??= PhotoPicker._(); return _instance; } static const String rootRouteName = "photo_picker_image"; /// 没有授予权限的时候,会开启一个dialog去帮助用户去应用设置页面开启权限 /// 确定开启设置页面,取消关闭弹窗,无论选择什么,返回值都是null /// 当用户给予权限后 /// /// 返回一个[PickedEntity] 内有[AssetEntity]以及[isFullImage] /// /// 当用户取消时[AssetEntity]返回一个空数组 /// /// [photoPathList] 一旦设置 则 [pickType]参数无效 /// /// [pickedAssetList] 已选择的asset /// /// 关于参数可以查看readme文档介绍 static Future pickAsset({ @required BuildContext context, int rowCount = 4, int maxSelected = 9, double padding = 0.5, double itemRadio = 1.0, int thumbSize = 180, PhotoTheme theme, I18nProvider provider = I18nProvider.chinese, SortDelegate sortDelegate, LoadingDelegate loadingDelegate, PickType pickType = PickType.all, List photoPathList, List pickedAssetList, }) { assert(maxSelected != 0, "maxSelected must be not 0"); 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: theme ?? PhotoTheme.fallback(context), thumbSize: thumbSize, sortDelegate: sortDelegate, checkBoxBuilderDelegate: DefaultCheckBoxBuilderDelegate(), loadingDelegate: loadingDelegate, badgeDelegate: DurationBadgeDelegate(), pickType: pickType, ); return PhotoPicker()._pickAsset( context, options, provider, photoPathList, pickedAssetList, ); } static Future pickSingleAsset({ @required BuildContext context, int rowCount = 4, double padding = 0.5, double itemRadio = 1.0, int thumbSize = 180, PhotoTheme theme, I18nProvider provider = I18nProvider.chinese, SortDelegate sortDelegate, LoadingDelegate loadingDelegate, PickType pickType = PickType.all, List photoPathList, }) async { final PickedEntity result = await PhotoPicker.pickAsset( context: context, rowCount: rowCount, maxSelected: 1, padding: padding, itemRadio: itemRadio, thumbSize: thumbSize, theme: theme, provider: provider, sortDelegate: sortDelegate, loadingDelegate: loadingDelegate, pickType: pickType, photoPathList: photoPathList, pickedAssetList: [], ); if (result.asset.isNotEmpty) { return result.asset[0]; } return null; } static Future pickCamera({ double maxWidth, double maxHeight, int imageQuality, CameraDevice preferredCameraDevice = CameraDevice.rear, }) async { final bool status = await PermissionUtils.canPermission(Permission.camera); if (!status) { throw 'permission denied'; } else { return ImagePicker().getImage( source: ImageSource.camera, maxWidth: maxWidth, maxHeight: maxHeight, imageQuality: imageQuality, preferredCameraDevice: preferredCameraDevice, ); } } Future _pickAsset( BuildContext context, Options options, I18nProvider provider, List photoList, List pickedAssetList, ) async { final bool status = await PermissionUtils.canMultiplePermission([Permission.photos, Permission.storage]); if (!status) { throw 'permission denied'; } else { return _openGalleryContentPage( context, options, provider, photoList, pickedAssetList, ); } } static Widget buildGallery({ @required BuildContext context, double padding = 1, int maxSelected = 9, int thumbSize = 180, double itemRadio = 1.0, PhotoTheme theme, I18nProvider provider = I18nProvider.chinese, SortDelegate sortDelegate, LoadingDelegate loadingDelegate, PickType pickType = PickType.all, List photoPathList, List pickedAssetList, bool isFullImage = false, void Function(PickedEntity value) onSelected, }) { sortDelegate ??= SortDelegate.common; loadingDelegate ??= DefaultLoadingDelegate(); var options = ListOptions( padding: padding, maxSelected: maxSelected, itemRadio: itemRadio, theme: theme ?? PhotoTheme.fallback(context), thumbSize: thumbSize, sortDelegate: sortDelegate, checkBoxBuilderDelegate: DefaultCheckBoxBuilderDelegate(), loadingDelegate: loadingDelegate, badgeDelegate: DurationBadgeDelegate(), 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"); return ChangeNotifierProvider( create: (_) => PhotoProvider( provider: provider, options: options, pickedAssetList: pickedAssetList, ), child: PhotoListPage( onSelected: onSelected, options: options, isFullImage: isFullImage, ), ); } Future requestPermission({@required I18nProvider provider, @required List permissions}) async { final bool status = await PermissionUtils.canMultiplePermission(permissions); // if (!status) { // var result = await showDialog( // context: context, // builder: (ctx) => NotPermissionDialog( // provider.getNotPermissionText(), // ), // ); // if (result == true) { // PhotoManager.openSetting(); // } // return false; // } return status; } Future _openGalleryContentPage( BuildContext context, Options options, I18nProvider provider, List photoList, List pickedAssetList, ) async { return Navigator.of(context, rootNavigator: true).push( MaterialPageRoute( builder: (ctx) => ChangeNotifierProvider( create: (_) => PhotoProvider( provider: provider, options: options, pickedAssetList: pickedAssetList, ), child: PhotoMainPage( onClose: (PickedEntity value) { Navigator.pop(ctx, value); }, options: options, photoList: photoList, ), ), ), ); } }