@@ -27,6 +27,7 @@ abstract class AssetPickerBuilderDelegate<A, P> {
27
27
this .pickerTheme,
28
28
this .specialItemPosition = SpecialItemPosition .none,
29
29
this .specialItemBuilder,
30
+ this .allowSpecialItemWhenEmpty = false ,
30
31
}) : assert (
31
32
pickerTheme == null || themeColor == null ,
32
33
'Theme and theme color cannot be set at the same time.' ,
@@ -55,17 +56,22 @@ abstract class AssetPickerBuilderDelegate<A, P> {
55
56
/// Usually the WeChat uses the dark version (dark background color)
56
57
/// for the picker. However, some others want a light or a custom version.
57
58
///
58
- /// 通常情况下微信选择器使用的是暗色(暗色背景)的主题,但某些情况下开发者需要亮色或自定义主题。
59
+ /// 通常情况下微信选择器使用的是暗色(暗色背景)的主题,
60
+ /// 但某些情况下开发者需要亮色或自定义主题。
59
61
final ThemeData pickerTheme;
60
62
61
63
/// Allow users set a special item in the picker with several positions.
62
- /// 允许用户在选择器中添加一个自定义item,并指定位置。
64
+ /// 允许用户在选择器中添加一个自定义item,并指定位置
63
65
final SpecialItemPosition specialItemPosition;
64
66
65
67
/// The widget builder for the the special item.
66
68
/// 自定义item的构造方法
67
69
final WidgetBuilder specialItemBuilder;
68
70
71
+ /// Whether the special item will display or not when assets is empty.
72
+ /// 当没有资源时是否显示自定义item
73
+ final bool allowSpecialItemWhenEmpty;
74
+
69
75
/// [ThemeData] for the picker.
70
76
/// 选择器使用的主题
71
77
ThemeData get theme => pickerTheme ?? AssetPicker .themeData (themeColor);
@@ -405,6 +411,7 @@ class DefaultAssetPickerBuilderDelegate
405
411
ThemeData pickerTheme,
406
412
SpecialItemPosition specialItemPosition = SpecialItemPosition .none,
407
413
WidgetBuilder specialItemBuilder,
414
+ bool allowSpecialItemWhenEmpty = false ,
408
415
this .previewThumbSize,
409
416
this .specialPickerType,
410
417
}) : assert (
@@ -423,6 +430,7 @@ class DefaultAssetPickerBuilderDelegate
423
430
pickerTheme: pickerTheme,
424
431
specialItemPosition: specialItemPosition,
425
432
specialItemBuilder: specialItemBuilder,
433
+ allowSpecialItemWhenEmpty: allowSpecialItemWhenEmpty,
426
434
);
427
435
428
436
/// Thumb size for the preview of images in the viewer.
@@ -472,9 +480,12 @@ class DefaultAssetPickerBuilderDelegate
472
480
bool hasAssetsToDisplay,
473
481
Widget __,
474
482
) {
483
+ final bool shouldDisplayAssets = hasAssetsToDisplay ||
484
+ (allowSpecialItemWhenEmpty &&
485
+ specialItemPosition != SpecialItemPosition .none);
475
486
return AnimatedSwitcher (
476
487
duration: switchingPathDuration,
477
- child: hasAssetsToDisplay
488
+ child: shouldDisplayAssets
478
489
? Stack (
479
490
children: < Widget > [
480
491
RepaintBoundary (
@@ -596,6 +607,20 @@ class DefaultAssetPickerBuilderDelegate
596
607
currentIndex = index - 1 ;
597
608
break ;
598
609
}
610
+
611
+ // Directly return the special item when it's empty.
612
+ if (currentPathEntity == null &&
613
+ allowSpecialItemWhenEmpty &&
614
+ specialItemPosition != SpecialItemPosition .none) {
615
+ return specialItemBuilder (context);
616
+ }
617
+
618
+ if (currentPathEntity.isAll &&
619
+ specialItemPosition != SpecialItemPosition .none &&
620
+ (index == 0 || index == currentAssets.length)) {
621
+ return specialItemBuilder (context);
622
+ }
623
+
599
624
if (! currentPathEntity.isAll) {
600
625
currentIndex = index;
601
626
}
@@ -605,14 +630,6 @@ class DefaultAssetPickerBuilderDelegate
605
630
provider.loadMoreAssets ();
606
631
}
607
632
608
- if (currentPathEntity.isAll) {
609
- if ((index == currentAssets.length &&
610
- specialItemPosition == SpecialItemPosition .append) ||
611
- (index == 0 && specialItemPosition == SpecialItemPosition .prepend)) {
612
- return specialItemBuilder (context);
613
- }
614
- }
615
-
616
633
final AssetEntity asset = currentAssets.elementAt (currentIndex);
617
634
Widget builder;
618
635
switch (asset.type) {
@@ -648,6 +665,11 @@ class DefaultAssetPickerBuilderDelegate
648
665
listen: false ,
649
666
).currentPathEntity;
650
667
668
+ if (currentPathEntity == null &&
669
+ specialItemPosition != SpecialItemPosition .none) {
670
+ return 1 ;
671
+ }
672
+
651
673
/// Return actual length if current path is all.
652
674
/// 如果当前目录是全部内容,则返回实际的内容数量。
653
675
if (! currentPathEntity.isAll) {
0 commit comments