2
2
/// [Author] Alex (https://github.com/AlexV525)
3
3
/// [Date] 2020-10-29 21:50
4
4
///
5
+ import 'dart:async' ;
5
6
import 'dart:io' ;
6
7
import 'dart:math' as math;
7
8
import 'dart:typed_data' ;
@@ -22,6 +23,12 @@ typedef IndicatorBuilder = Widget Function(
22
23
bool isAssetsEmpty,
23
24
);
24
25
26
+ typedef AssetSelectPredicate <Asset > = FutureOr <bool > Function (
27
+ BuildContext context,
28
+ Asset asset,
29
+ bool isSelected,
30
+ );
31
+
25
32
/// The delegate to build the whole picker's components.
26
33
///
27
34
/// By extending the delegate, you can customize every components on you own.
@@ -41,6 +48,7 @@ abstract class AssetPickerBuilderDelegate<Asset, Path> {
41
48
this .loadingIndicatorBuilder,
42
49
this .allowSpecialItemWhenEmpty = false ,
43
50
this .keepScrollOffset = false ,
51
+ this .selectPredicate,
44
52
}) : assert (
45
53
pickerTheme == null || themeColor == null ,
46
54
'Theme and theme color cannot be set at the same time.' ,
@@ -100,6 +108,10 @@ abstract class AssetPickerBuilderDelegate<Asset, Path> {
100
108
/// 选择器是否可以从同样的位置开始选择
101
109
final bool keepScrollOffset;
102
110
111
+ /// Predicate whether an asset can be selected or unselected.
112
+ /// 判断资源可否被选择
113
+ final AssetSelectPredicate <Asset >? selectPredicate;
114
+
103
115
/// The [ScrollController] for the preview grid.
104
116
final ScrollController gridScrollController = ScrollController ();
105
117
@@ -629,6 +641,7 @@ class DefaultAssetPickerBuilderDelegate
629
641
IndicatorBuilder ? loadingIndicatorBuilder,
630
642
bool allowSpecialItemWhenEmpty = false ,
631
643
bool keepScrollOffset = false ,
644
+ AssetSelectPredicate <AssetEntity >? selectPredicate,
632
645
this .gridThumbSize = Constants .defaultGridThumbSize,
633
646
this .previewThumbSize,
634
647
this .specialPickerType,
@@ -648,6 +661,7 @@ class DefaultAssetPickerBuilderDelegate
648
661
loadingIndicatorBuilder: loadingIndicatorBuilder,
649
662
allowSpecialItemWhenEmpty: allowSpecialItemWhenEmpty,
650
663
keepScrollOffset: keepScrollOffset,
664
+ selectPredicate: selectPredicate,
651
665
);
652
666
653
667
/// Thumbnail size in the grid.
@@ -1642,7 +1656,15 @@ class DefaultAssetPickerBuilderDelegate
1642
1656
);
1643
1657
final GestureDetector selectorWidget = GestureDetector (
1644
1658
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
+ }
1646
1668
if (selected) {
1647
1669
provider.unSelectAsset (asset);
1648
1670
return ;
0 commit comments