Skip to content

Commit 69523ae

Browse files
committed
✨ Introduce AssetSelectPredicate
1 parent 8e28234 commit 69523ae

File tree

2 files changed

+25
-1
lines changed

2 files changed

+25
-1
lines changed

lib/src/delegates/asset_picker_builder_delegate.dart

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
/// [Author] Alex (https://github.com/AlexV525)
33
/// [Date] 2020-10-29 21:50
44
///
5+
import 'dart:async';
56
import 'dart:io';
67
import 'dart:math' as math;
78
import 'dart:typed_data';
@@ -22,6 +23,12 @@ typedef IndicatorBuilder = Widget Function(
2223
bool isAssetsEmpty,
2324
);
2425

26+
typedef AssetSelectPredicate<Asset> = FutureOr<bool> Function(
27+
BuildContext context,
28+
Asset asset,
29+
bool isSelected,
30+
);
31+
2532
/// The delegate to build the whole picker's components.
2633
///
2734
/// By extending the delegate, you can customize every components on you own.
@@ -41,6 +48,7 @@ abstract class AssetPickerBuilderDelegate<Asset, Path> {
4148
this.loadingIndicatorBuilder,
4249
this.allowSpecialItemWhenEmpty = false,
4350
this.keepScrollOffset = false,
51+
this.selectPredicate,
4452
}) : assert(
4553
pickerTheme == null || themeColor == null,
4654
'Theme and theme color cannot be set at the same time.',
@@ -100,6 +108,10 @@ abstract class AssetPickerBuilderDelegate<Asset, Path> {
100108
/// 选择器是否可以从同样的位置开始选择
101109
final bool keepScrollOffset;
102110

111+
/// Predicate whether an asset can be selected or unselected.
112+
/// 判断资源可否被选择
113+
final AssetSelectPredicate<Asset>? selectPredicate;
114+
103115
/// The [ScrollController] for the preview grid.
104116
final ScrollController gridScrollController = ScrollController();
105117

@@ -629,6 +641,7 @@ class DefaultAssetPickerBuilderDelegate
629641
IndicatorBuilder? loadingIndicatorBuilder,
630642
bool allowSpecialItemWhenEmpty = false,
631643
bool keepScrollOffset = false,
644+
AssetSelectPredicate<AssetEntity>? selectPredicate,
632645
this.gridThumbSize = Constants.defaultGridThumbSize,
633646
this.previewThumbSize,
634647
this.specialPickerType,
@@ -648,6 +661,7 @@ class DefaultAssetPickerBuilderDelegate
648661
loadingIndicatorBuilder: loadingIndicatorBuilder,
649662
allowSpecialItemWhenEmpty: allowSpecialItemWhenEmpty,
650663
keepScrollOffset: keepScrollOffset,
664+
selectPredicate: selectPredicate,
651665
);
652666

653667
/// Thumbnail size in the grid.
@@ -1642,7 +1656,15 @@ class DefaultAssetPickerBuilderDelegate
16421656
);
16431657
final GestureDetector selectorWidget = GestureDetector(
16441658
behavior: HitTestBehavior.opaque,
1645-
onTap: () {
1659+
onTap: () async {
1660+
final bool? selectPredicateResult = await selectPredicate?.call(
1661+
context,
1662+
asset,
1663+
selected,
1664+
);
1665+
if (selectPredicateResult == false) {
1666+
return;
1667+
}
16461668
if (selected) {
16471669
provider.unSelectAsset(asset);
16481670
return;

lib/src/widget/asset_picker.dart

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ class AssetPicker<Asset, Path> extends StatefulWidget {
4646
IndicatorBuilder? loadingIndicatorBuilder,
4747
SpecialItemPosition specialItemPosition = SpecialItemPosition.none,
4848
bool allowSpecialItemWhenEmpty = false,
49+
AssetSelectPredicate<AssetEntity>? selectPredicate,
4950
bool useRootNavigator = true,
5051
Curve routeCurve = Curves.easeIn,
5152
Duration routeDuration = const Duration(milliseconds: 300),
@@ -111,6 +112,7 @@ class AssetPicker<Asset, Path> extends StatefulWidget {
111112
specialItemBuilder: specialItemBuilder,
112113
loadingIndicatorBuilder: loadingIndicatorBuilder,
113114
allowSpecialItemWhenEmpty: allowSpecialItemWhenEmpty,
115+
selectPredicate: selectPredicate,
114116
),
115117
),
116118
);

0 commit comments

Comments
 (0)