Skip to content

Commit daff0d1

Browse files
committed
✨ Add ability to show the special item when the device has no assets.
1 parent 30474e4 commit daff0d1

File tree

2 files changed

+35
-11
lines changed

2 files changed

+35
-11
lines changed

lib/src/delegates/asset_picker_builder_delegate.dart

Lines changed: 33 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ abstract class AssetPickerBuilderDelegate<A, P> {
2727
this.pickerTheme,
2828
this.specialItemPosition = SpecialItemPosition.none,
2929
this.specialItemBuilder,
30+
this.allowSpecialItemWhenEmpty = false,
3031
}) : assert(
3132
pickerTheme == null || themeColor == null,
3233
'Theme and theme color cannot be set at the same time.',
@@ -55,17 +56,22 @@ abstract class AssetPickerBuilderDelegate<A, P> {
5556
/// Usually the WeChat uses the dark version (dark background color)
5657
/// for the picker. However, some others want a light or a custom version.
5758
///
58-
/// 通常情况下微信选择器使用的是暗色(暗色背景)的主题,但某些情况下开发者需要亮色或自定义主题。
59+
/// 通常情况下微信选择器使用的是暗色(暗色背景)的主题,
60+
/// 但某些情况下开发者需要亮色或自定义主题。
5961
final ThemeData pickerTheme;
6062

6163
/// Allow users set a special item in the picker with several positions.
62-
/// 允许用户在选择器中添加一个自定义item,并指定位置
64+
/// 允许用户在选择器中添加一个自定义item,并指定位置
6365
final SpecialItemPosition specialItemPosition;
6466

6567
/// The widget builder for the the special item.
6668
/// 自定义item的构造方法
6769
final WidgetBuilder specialItemBuilder;
6870

71+
/// Whether the special item will display or not when assets is empty.
72+
/// 当没有资源时是否显示自定义item
73+
final bool allowSpecialItemWhenEmpty;
74+
6975
/// [ThemeData] for the picker.
7076
/// 选择器使用的主题
7177
ThemeData get theme => pickerTheme ?? AssetPicker.themeData(themeColor);
@@ -405,6 +411,7 @@ class DefaultAssetPickerBuilderDelegate
405411
ThemeData pickerTheme,
406412
SpecialItemPosition specialItemPosition = SpecialItemPosition.none,
407413
WidgetBuilder specialItemBuilder,
414+
bool allowSpecialItemWhenEmpty = false,
408415
this.previewThumbSize,
409416
this.specialPickerType,
410417
}) : assert(
@@ -423,6 +430,7 @@ class DefaultAssetPickerBuilderDelegate
423430
pickerTheme: pickerTheme,
424431
specialItemPosition: specialItemPosition,
425432
specialItemBuilder: specialItemBuilder,
433+
allowSpecialItemWhenEmpty: allowSpecialItemWhenEmpty,
426434
);
427435

428436
/// Thumb size for the preview of images in the viewer.
@@ -472,9 +480,12 @@ class DefaultAssetPickerBuilderDelegate
472480
bool hasAssetsToDisplay,
473481
Widget __,
474482
) {
483+
final bool shouldDisplayAssets = hasAssetsToDisplay ||
484+
(allowSpecialItemWhenEmpty &&
485+
specialItemPosition != SpecialItemPosition.none);
475486
return AnimatedSwitcher(
476487
duration: switchingPathDuration,
477-
child: hasAssetsToDisplay
488+
child: shouldDisplayAssets
478489
? Stack(
479490
children: <Widget>[
480491
RepaintBoundary(
@@ -596,6 +607,20 @@ class DefaultAssetPickerBuilderDelegate
596607
currentIndex = index - 1;
597608
break;
598609
}
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+
599624
if (!currentPathEntity.isAll) {
600625
currentIndex = index;
601626
}
@@ -605,14 +630,6 @@ class DefaultAssetPickerBuilderDelegate
605630
provider.loadMoreAssets();
606631
}
607632

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-
616633
final AssetEntity asset = currentAssets.elementAt(currentIndex);
617634
Widget builder;
618635
switch (asset.type) {
@@ -648,6 +665,11 @@ class DefaultAssetPickerBuilderDelegate
648665
listen: false,
649666
).currentPathEntity;
650667

668+
if (currentPathEntity == null &&
669+
specialItemPosition != SpecialItemPosition.none) {
670+
return 1;
671+
}
672+
651673
/// Return actual length if current path is all.
652674
/// 如果当前目录是全部内容,则返回实际的内容数量。
653675
if (!currentPathEntity.isAll) {

lib/src/widget/asset_picker.dart

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ class AssetPicker<A, P> extends StatelessWidget {
4141
FilterOptionGroup filterOptions,
4242
WidgetBuilder specialItemBuilder,
4343
SpecialItemPosition specialItemPosition = SpecialItemPosition.none,
44+
bool allowSpecialItemWhenEmpty = false,
4445
Curve routeCurve = Curves.easeIn,
4546
Duration routeDuration = const Duration(milliseconds: 300),
4647
}) async {
@@ -105,6 +106,7 @@ class AssetPicker<A, P> extends StatelessWidget {
105106
specialPickerType: specialPickerType,
106107
specialItemPosition: specialItemPosition,
107108
specialItemBuilder: specialItemBuilder,
109+
allowSpecialItemWhenEmpty: allowSpecialItemWhenEmpty,
108110
),
109111
),
110112
);

0 commit comments

Comments
 (0)