@@ -19,6 +19,7 @@ import 'package:provider/provider.dart';
19
19
import '../constants/constants.dart' ;
20
20
import '../constants/enums.dart' ;
21
21
import '../constants/extensions.dart' ;
22
+ import '../constants/typedefs.dart' ;
22
23
import '../delegates/asset_picker_text_delegate.dart' ;
23
24
import '../internal/singleton.dart' ;
24
25
import '../provider/asset_picker_provider.dart' ;
@@ -33,21 +34,6 @@ import '../widget/scale_text.dart';
33
34
34
35
const String _ordinalNamePermissionOverlay = 'permissionOverlay' ;
35
36
36
- typedef IndicatorBuilder = Widget Function (
37
- BuildContext context,
38
- bool isAssetsEmpty,
39
- );
40
-
41
- /// {@template wechat_assets_picker.AssetSelectPredicate}
42
- /// Predicate whether an asset can be selected or unselected.
43
- /// 判断资源可否被选择
44
- /// {@endtemplate}
45
- typedef AssetSelectPredicate <Asset > = FutureOr <bool > Function (
46
- BuildContext context,
47
- Asset asset,
48
- bool isSelected,
49
- );
50
-
51
37
/// The delegate to build the whole picker's components.
52
38
///
53
39
/// By extending the delegate, you can customize every components on you own.
@@ -62,7 +48,6 @@ abstract class AssetPickerBuilderDelegate<Asset, Path> {
62
48
this .specialItemPosition = SpecialItemPosition .none,
63
49
this .specialItemBuilder,
64
50
this .loadingIndicatorBuilder,
65
- this .allowSpecialItemWhenEmpty = false ,
66
51
this .selectPredicate,
67
52
this .shouldRevertGrid,
68
53
Color ? themeColor,
@@ -107,15 +92,11 @@ abstract class AssetPickerBuilderDelegate<Asset, Path> {
107
92
108
93
/// The widget builder for the the special item.
109
94
/// 自定义item的构造方法
110
- final WidgetBuilder ? specialItemBuilder;
95
+ final SpecialItemBuilder < Path > ? specialItemBuilder;
111
96
112
97
/// Indicates the loading status for the builder.
113
98
/// 指示目前加载的状态
114
- final IndicatorBuilder ? loadingIndicatorBuilder;
115
-
116
- /// Whether the special item will display or not when assets is empty.
117
- /// 当没有资源时是否显示自定义item
118
- final bool allowSpecialItemWhenEmpty;
99
+ final LoadingIndicatorBuilder ? loadingIndicatorBuilder;
119
100
120
101
/// {@macro wechat_assets_picker.AssetSelectPredicate}
121
102
final AssetSelectPredicate <Asset >? selectPredicate;
@@ -668,9 +649,8 @@ class DefaultAssetPickerBuilderDelegate
668
649
int gridCount = 4 ,
669
650
ThemeData ? pickerTheme,
670
651
SpecialItemPosition specialItemPosition = SpecialItemPosition .none,
671
- WidgetBuilder ? specialItemBuilder,
672
- IndicatorBuilder ? loadingIndicatorBuilder,
673
- bool allowSpecialItemWhenEmpty = false ,
652
+ SpecialItemBuilder <AssetPathEntity >? specialItemBuilder,
653
+ LoadingIndicatorBuilder ? loadingIndicatorBuilder,
674
654
AssetSelectPredicate <AssetEntity >? selectPredicate,
675
655
bool ? shouldRevertGrid,
676
656
this .gridThumbnailSize = defaultAssetGridPreviewSize,
@@ -691,7 +671,6 @@ class DefaultAssetPickerBuilderDelegate
691
671
specialItemPosition: specialItemPosition,
692
672
specialItemBuilder: specialItemBuilder,
693
673
loadingIndicatorBuilder: loadingIndicatorBuilder,
694
- allowSpecialItemWhenEmpty: allowSpecialItemWhenEmpty,
695
674
selectPredicate: selectPredicate,
696
675
shouldRevertGrid: shouldRevertGrid,
697
676
themeColor: themeColor,
@@ -922,11 +901,15 @@ class DefaultAssetPickerBuilderDelegate
922
901
Widget androidLayout (BuildContext context) {
923
902
return AssetPickerAppBarWrapper (
924
903
appBar: appBar (context),
925
- body: Selector <DefaultAssetPickerProvider , bool >(
926
- selector: (_, DefaultAssetPickerProvider p) => p.hasAssetsToDisplay,
927
- builder: (_, bool hasAssetsToDisplay, __) {
928
- final bool shouldDisplayAssets = hasAssetsToDisplay ||
929
- (allowSpecialItemWhenEmpty &&
904
+ body: Consumer <DefaultAssetPickerProvider >(
905
+ builder: (BuildContext context, DefaultAssetPickerProvider p, __) {
906
+ final Widget ? _specialItem = specialItemBuilder? .call (
907
+ context,
908
+ p.currentPath,
909
+ p.currentAssets.length,
910
+ );
911
+ final bool shouldDisplayAssets = p.hasAssetsToDisplay ||
912
+ (_specialItem != null &&
930
913
specialItemPosition != SpecialItemPosition .none);
931
914
return AnimatedSwitcher (
932
915
duration: switchingPathDuration,
@@ -980,13 +963,16 @@ class DefaultAssetPickerBuilderDelegate
980
963
return Stack (
981
964
children: < Widget > [
982
965
Positioned .fill (
983
- child: Selector <DefaultAssetPickerProvider , bool >(
984
- selector: (_, DefaultAssetPickerProvider p) =>
985
- p.hasAssetsToDisplay,
986
- builder: (_, bool hasAssetsToDisplay, __) {
966
+ child: Consumer <DefaultAssetPickerProvider >(
967
+ builder: (_, DefaultAssetPickerProvider p, __) {
987
968
final Widget _child;
988
- final bool shouldDisplayAssets = hasAssetsToDisplay ||
989
- (allowSpecialItemWhenEmpty &&
969
+ final Widget ? _specialItem = specialItemBuilder? .call (
970
+ context,
971
+ p.currentPath,
972
+ p.currentAssets.length,
973
+ );
974
+ final bool shouldDisplayAssets = p.hasAssetsToDisplay ||
975
+ (_specialItem != null &&
990
976
specialItemPosition != SpecialItemPosition .none);
991
977
if (shouldDisplayAssets) {
992
978
_child = Stack (
@@ -1192,45 +1178,52 @@ class DefaultAssetPickerBuilderDelegate
1192
1178
int index,
1193
1179
List <AssetEntity > currentAssets,
1194
1180
) {
1181
+ final int _length = currentAssets.length;
1195
1182
final AssetPathEntity ? currentPathEntity =
1196
1183
context.select <DefaultAssetPickerProvider , AssetPathEntity ?>(
1197
1184
(DefaultAssetPickerProvider p) => p.currentPath,
1198
1185
);
1199
1186
1200
- int currentIndex;
1201
- switch (specialItemPosition) {
1202
- case SpecialItemPosition .none:
1203
- case SpecialItemPosition .append:
1204
- currentIndex = index;
1205
- break ;
1206
- case SpecialItemPosition .prepend:
1207
- currentIndex = index - 1 ;
1208
- break ;
1209
- }
1210
-
1211
- // Directly return the special item when it's empty.
1212
- if (currentPathEntity == null ) {
1213
- if (allowSpecialItemWhenEmpty &&
1214
- specialItemPosition != SpecialItemPosition .none) {
1215
- return specialItemBuilder !(context);
1216
- }
1217
- return const SizedBox .shrink ();
1187
+ final Widget ? specialItem;
1188
+ if (specialItemPosition == SpecialItemPosition .none) {
1189
+ specialItem = null ;
1190
+ } else {
1191
+ specialItem = specialItemBuilder? .call (
1192
+ context,
1193
+ currentPathEntity,
1194
+ _length,
1195
+ );
1218
1196
}
1219
1197
1220
- final int _length = currentAssets.length;
1221
- if (currentPathEntity.isAll &&
1222
- specialItemPosition != SpecialItemPosition .none) {
1198
+ if (specialItem != null ) {
1223
1199
if ((index == 0 && specialItemPosition == SpecialItemPosition .prepend) ||
1224
1200
(index == _length &&
1225
1201
specialItemPosition == SpecialItemPosition .append)) {
1226
- return specialItemBuilder !(context) ;
1202
+ return specialItem ;
1227
1203
}
1228
1204
}
1229
1205
1230
- if (! currentPathEntity.isAll) {
1206
+ final int currentIndex;
1207
+ if (specialItem != null &&
1208
+ specialItemPosition == SpecialItemPosition .prepend) {
1209
+ currentIndex = index - 1 ;
1210
+ } else {
1231
1211
currentIndex = index;
1232
1212
}
1233
1213
1214
+ if (specialItemPosition != SpecialItemPosition .none) {
1215
+ final Widget ? specialItem = specialItemBuilder? .call (
1216
+ context,
1217
+ currentPathEntity,
1218
+ _length,
1219
+ );
1220
+ if (specialItem != null ) {}
1221
+ }
1222
+
1223
+ if (currentPathEntity == null ) {
1224
+ return const SizedBox .shrink ();
1225
+ }
1226
+
1234
1227
if (index == _length - gridCount * 3 &&
1235
1228
context.select <DefaultAssetPickerProvider , bool >(
1236
1229
(DefaultAssetPickerProvider p) => p.hasMoreToLoad,
0 commit comments