123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257 |
- library ct_assets_picker;
-
- import 'dart:developer';
- import 'dart:io';
- import 'dart:math' as math;
- import 'dart:typed_data';
- import 'dart:ui' as ui;
-
- import 'package:extended_image/extended_image.dart';
- import 'package:flutter/foundation.dart';
- import 'package:flutter/material.dart';
- import 'package:flutter/rendering.dart';
- import 'package:flutter/services.dart';
- import 'package:intl/intl.dart';
- import 'package:notus/notus.dart';
- import 'package:provider/provider.dart';
- import 'package:wechat_assets_picker/wechat_assets_picker.dart';
- import 'package:image_picker/image_picker.dart';
- import 'package:zefyr/src/widgets/iconfont.dart';
- import 'package:zefyr/src/widgets/theme.dart';
- import 'package:zefyr/src/widgets/toolbar.dart';
-
- export 'package:wechat_assets_picker/wechat_assets_picker.dart'
- show RequestType, AssetEntity, AssetType, PhotoManager;
- export 'package:image_picker/image_picker.dart' show CameraDevice;
-
- part 'src/constants/constants.dart';
- part 'src/constants/screens.dart';
- part 'src/delegate/asset_picker_builder_delegate.dart';
- part 'src/delegate/asset_picker_quick_builder_delegate.dart';
- part 'src/delegate/asset_picker_viewer_builder_delegate.dart';
- part 'src/delegate/asset_picker_sort_delegate.dart';
- part 'src/delegate/asset_picker_text_delegate.dart';
- part 'src/widget/slide_page_transition_builder.dart';
- part 'src/widget/asset_picker_viewer.dart';
- part 'src/widget/rounded_check_box.dart';
- part 'src/provider/asset_picker_provider.dart';
- part 'src/entity/asset_picker_entity.dart';
-
- class ExtendedAssetPicker {
- static Future<AssetPickerEntity> pickAssets(
- BuildContext context, {
- int maxAssets = 9,
- int pageSize = 320,
- int pathThumbSize = 200,
- int gridCount = 4,
- List<int> previewThumbSize,
- RequestType requestType,
- SpecialPickerType specialPickerType,
- List<AssetEntity> selectedAssets,
- Color themeColor,
- ThemeData pickerTheme,
- SortPathDelegate sortPathDelegate,
- AssetsPickerTextDelegate textDelegate,
- FilterOptionGroup filterOptions,
- WidgetBuilder specialItemBuilder,
- SpecialItemPosition specialItemPosition = SpecialItemPosition.none,
- bool allowSpecialItemWhenEmpty = false,
- Curve routeCurve = Curves.easeIn,
- Duration routeDuration = const Duration(milliseconds: 300),
- }) async {
- if (maxAssets == null || maxAssets < 1) {
- throw ArgumentError(
- 'maxAssets must be greater than 1.',
- );
- }
- if (pageSize != null && pageSize % gridCount != 0) {
- throw ArgumentError(
- 'pageSize must be a multiple of gridCount.',
- );
- }
- if (pickerTheme != null && themeColor != null) {
- throw ArgumentError(
- 'Theme and theme color cannot be set at the same time.',
- );
- }
- if (specialPickerType != null && requestType != null) {
- throw ArgumentError(
- 'specialPickerType and requestType cannot be set at the same time.',
- );
- } else {
- if (specialPickerType == SpecialPickerType.wechatMoment) {
- requestType = RequestType.common;
- } else {
- requestType ??= RequestType.image;
- }
- }
- if ((specialItemBuilder == null &&
- specialItemPosition != SpecialItemPosition.none) ||
- (specialItemBuilder != null &&
- specialItemPosition == SpecialItemPosition.none)) {
- throw ArgumentError('Custom item didn\'t set properly.');
- }
-
- final theme = Theme.of(context);
-
- try {
- final isPermissionGranted = await PhotoManager.requestPermission();
- if (isPermissionGranted) {
- final provider = ExtendedAssetPickerProvider(
- maxAssets: maxAssets,
- pageSize: pageSize,
- pathThumbSize: pathThumbSize,
- selectedAssets: selectedAssets,
- requestType: requestType,
- sortPathDelegate: sortPathDelegate ?? CustomSortPathDelegate(),
- filterOptions: filterOptions,
- routeDuration: routeDuration,
- );
- final Widget picker =
- ChangeNotifierProvider<DefaultAssetPickerProvider>.value(
- value: provider,
- child: AssetPicker<AssetEntity, AssetPathEntity>(
- key: Constants.pickerKey,
- builder: ExtendedAssetPickerBuilderDelegate(
- provider: provider,
- gridCount: gridCount,
- textDelegate: textDelegate ??
- (Intl.getCurrentLocale() == 'zh_CN'
- ? DefaultAssetsPickerTextDelegate()
- : EnglishTextDelegate()),
- themeColor: themeColor,
- pickerTheme: theme,
- previewThumbSize: previewThumbSize,
- specialPickerType: specialPickerType,
- specialItemPosition: specialItemPosition,
- specialItemBuilder: specialItemBuilder,
- allowSpecialItemWhenEmpty: allowSpecialItemWhenEmpty,
- ),
- ),
- );
- final result = await Navigator.of(
- context,
- rootNavigator: true,
- ).push<AssetPickerEntity>(
- SlidePageTransitionBuilder<AssetPickerEntity>(
- builder: picker,
- transitionCurve: routeCurve,
- transitionDuration: routeDuration,
- ),
- );
- return result;
- } else {
- return null;
- }
- } catch (e) {
- realDebugPrint('Error when calling assets picker: $e');
- return null;
- }
- }
-
- static Future<AssetEntity> pickSingleAsset(
- BuildContext context, {
- int pageSize = 320,
- int pathThumbSize = 200,
- int gridCount = 4,
- List<int> previewThumbSize,
- RequestType requestType,
- SpecialPickerType specialPickerType,
- List<AssetEntity> selectedAssets,
- Color themeColor,
- ThemeData pickerTheme,
- SortPathDelegate sortPathDelegate,
- AssetsPickerTextDelegate textDelegate,
- FilterOptionGroup filterOptions,
- WidgetBuilder specialItemBuilder,
- SpecialItemPosition specialItemPosition = SpecialItemPosition.none,
- bool allowSpecialItemWhenEmpty = false,
- Curve routeCurve = Curves.easeIn,
- Duration routeDuration = const Duration(milliseconds: 300),
- }) async {
- final result = await ExtendedAssetPicker.pickAssets(
- context,
- maxAssets: 1,
- pageSize: pageSize,
- pathThumbSize: pathThumbSize,
- gridCount: gridCount,
- previewThumbSize: previewThumbSize,
- requestType: requestType,
- specialPickerType: specialPickerType,
- themeColor: themeColor,
- pickerTheme: pickerTheme,
- sortPathDelegate: sortPathDelegate,
- textDelegate: textDelegate,
- filterOptions: filterOptions,
- specialItemBuilder: specialItemBuilder,
- specialItemPosition: specialItemPosition,
- allowSpecialItemWhenEmpty: allowSpecialItemWhenEmpty,
- routeCurve: routeCurve,
- routeDuration: routeDuration,
- );
- if (result != null) {
- return result.assets?.first;
- }
- return null;
- }
-
- static Future<File> getImageFromCamera({
- ImageSource source = ImageSource.camera,
- double maxWidth,
- double maxHeight,
- int imageQuality = 100,
- CameraDevice preferredCameraDevice = CameraDevice.rear,
- }) {
- return ImagePicker()
- .getImage(
- source: source,
- maxWidth: maxWidth,
- maxHeight: maxHeight,
- imageQuality: imageQuality,
- preferredCameraDevice: preferredCameraDevice)
- .then((result) {
- if (result != null) {
- return File(result.path);
- }
- return null;
- });
- }
-
- static Widget buildQuickPicker(
- BuildContext context, {
- AssetPickerProvider provider,
- int gridCount = 4,
- List<int> previewThumbSize,
- SpecialPickerType specialPickerType,
- Color themeColor,
- ThemeData pickerTheme,
- AssetsPickerTextDelegate textDelegate,
- WidgetBuilder specialItemBuilder,
- SpecialItemPosition specialItemPosition = SpecialItemPosition.none,
- bool allowSpecialItemWhenEmpty = false,
- }) {
- final theme = Theme.of(context);
-
- return ChangeNotifierProvider<DefaultAssetPickerProvider>.value(
- value: provider,
- child: AssetPicker<AssetEntity, AssetPathEntity>(
- key: Constants.pickerKey,
- builder: ExtendedAssetPickerQuickBuilderDelegate(
- provider: provider,
- textDelegate: textDelegate ??
- (Intl.getCurrentLocale() == 'zh_CN'
- ? ExtendedChineseTextDelegate()
- : ExtendedEnglishTextDelegate()),
- themeColor: themeColor,
- pickerTheme: theme,
- previewThumbSize: previewThumbSize,
- specialPickerType: specialPickerType,
- specialItemPosition: specialItemPosition,
- specialItemBuilder: specialItemBuilder,
- allowSpecialItemWhenEmpty: allowSpecialItemWhenEmpty,
- ),
- ),
- );
- }
- }
|