Skip to content

Commit 84f5af0

Browse files
committed
🚀 Abstract and expose shouldRevertGrid parameter from delegate
1 parent a86c937 commit 84f5af0

File tree

4 files changed

+32
-17
lines changed

4 files changed

+32
-17
lines changed

README-ZH.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,7 @@ platform :ios, '9.0'
184184
| loadingIndicatorBuilder | `IndicatorBuilder?` | 加载器的实现 | `null` |
185185
| allowSpecialItemWhenEmpty | `bool` | 在资源为空时是否允许显示自定义item | `false` |
186186
| selectPredicate | `AssetSelectPredicate` | 判断资源可否被选择 | `null` |
187+
| shouldRevertGrid | `bool?` | 判断资源网格是否需要倒序排列 | `null` |
187188
| routeCurve | `Curve` | 选择构造路由动画的曲线 | `Curves.easeIn` |
188189
| routeDuration | `Duration` | 选择构造路由动画的时间 | `const Duration(milliseconds: 500)` |
189190

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,7 @@ platform :osx, '10.15'
177177
| loadingIndicatorBuilder | `IndicatorBuilder?` | Indicates the loading status for the builder. | `null` |
178178
| allowSpecialItemWhenEmpty | `bool` | Whether the special item will display or not when assets is empty. | `false` |
179179
| selectPredicate | `AssetSelectPredicate` | Predicate whether an asset can be selected or unselected. | `null` |
180+
| shouldRevertGrid | `bool?` | Whether the assets grid should revert. | `null` |
180181
| routeCurve | `Curve` | The curve which the picker use to build page route transition. | `Curves.easeIn` |
181182
| routeDuration | `Duration` | The duration which the picker use to build page route transition. | `const Duration(milliseconds: 500)` |
182183

lib/src/delegates/asset_picker_builder_delegate.dart

Lines changed: 27 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ abstract class AssetPickerBuilderDelegate<Asset, Path> {
4949
this.allowSpecialItemWhenEmpty = false,
5050
this.keepScrollOffset = false,
5151
this.selectPredicate,
52+
this.shouldRevertGrid,
5253
}) : assert(
5354
pickerTheme == null || themeColor == null,
5455
'Theme and theme color cannot be set at the same time.',
@@ -119,6 +120,13 @@ abstract class AssetPickerBuilderDelegate<Asset, Path> {
119120
/// [assetsGridBuilder] 用于定位 [ScrollView.center][GlobalKey]
120121
final GlobalKey gridRevertKey = GlobalKey();
121122

123+
/// Whether the assets grid should revert.
124+
/// 判断资源网格是否需要倒序排列
125+
///
126+
/// [Null] means judging by [isAppleOS].
127+
/// 使用 [Null] 即使用 [isAppleOS] 进行判断。
128+
final bool? shouldRevertGrid;
129+
122130
/// [ThemeData] for the picker.
123131
/// 选择器使用的主题
124132
ThemeData get theme => pickerTheme ?? AssetPicker.themeData(themeColor);
@@ -186,6 +194,8 @@ abstract class AssetPickerBuilderDelegate<Asset, Path> {
186194
/// 当前的权限是否为受限
187195
bool get isPermissionLimited => permission.value == PermissionState.limited;
188196

197+
bool get effectiveShouldRevertGrid => shouldRevertGrid ?? isAppleOS;
198+
189199
/// The listener to track the scroll position of the [gridScrollController]
190200
/// if [keepScrollOffset] is true.
191201
/// 当 [keepScrollOffset] 为 true 时,跟踪 [gridScrollController] 位置的监听。
@@ -357,7 +367,7 @@ abstract class AssetPickerBuilderDelegate<Asset, Path> {
357367
/// 默认情况下,在 iOS/macOS 上方向会反向。
358368
TextDirection effectiveGridDirection(BuildContext context) {
359369
final TextDirection _od = Directionality.of(context);
360-
if (isAppleOS) {
370+
if (effectiveShouldRevertGrid) {
361371
if (_od == TextDirection.ltr) {
362372
return TextDirection.rtl;
363373
}
@@ -642,6 +652,7 @@ class DefaultAssetPickerBuilderDelegate
642652
bool allowSpecialItemWhenEmpty = false,
643653
bool keepScrollOffset = false,
644654
AssetSelectPredicate<AssetEntity>? selectPredicate,
655+
bool? shouldRevertGrid,
645656
this.gridThumbSize = Constants.defaultGridThumbSize,
646657
this.previewThumbSize,
647658
this.specialPickerType,
@@ -662,6 +673,7 @@ class DefaultAssetPickerBuilderDelegate
662673
allowSpecialItemWhenEmpty: allowSpecialItemWhenEmpty,
663674
keepScrollOffset: keepScrollOffset,
664675
selectPredicate: selectPredicate,
676+
shouldRevertGrid: shouldRevertGrid,
665677
);
666678

667679
/// Thumbnail size in the grid.
@@ -840,11 +852,11 @@ class DefaultAssetPickerBuilderDelegate
840852
path?.isAll == true) {
841853
totalCount += 1;
842854
}
843-
// Then we use the [totalCount] to calculate how many placeholders we need.
855+
// Then we use the [totalCount] to calculate placeholders we need.
844856
final int placeholderCount;
845-
if (isAppleOS && totalCount % gridCount != 0) {
846-
// When there are left items that not filled into one row, filled the row
847-
// with placeholders.
857+
if (effectiveShouldRevertGrid && totalCount % gridCount != 0) {
858+
// When there are left items that not filled into one row,
859+
// filled the row with placeholders.
848860
placeholderCount = gridCount - totalCount % gridCount;
849861
} else {
850862
// Otherwise, we don't need placeholders.
@@ -863,7 +875,7 @@ class DefaultAssetPickerBuilderDelegate
863875
delegate: SliverChildBuilderDelegate(
864876
(_, int index) => Builder(
865877
builder: (BuildContext c) {
866-
if (isAppleOS) {
878+
if (effectiveShouldRevertGrid) {
867879
if (index < placeholderCount) {
868880
return const SizedBox.shrink();
869881
}
@@ -933,25 +945,27 @@ class DefaultAssetPickerBuilderDelegate
933945
selector: (_, DefaultAssetPickerProvider p) =>
934946
p.currentAssets,
935947
builder: (_, List<AssetEntity> assets, __) {
948+
final SliverGap _bottomGap = SliverGap.v(
949+
context.bottomPadding + bottomSectionHeight,
950+
);
936951
return CustomScrollView(
937952
physics: const AlwaysScrollableScrollPhysics(),
938953
controller: gridScrollController,
939-
anchor: isAppleOS ? anchor : 0,
940-
center: isAppleOS ? gridRevertKey : null,
954+
anchor: effectiveShouldRevertGrid ? anchor : 0,
955+
center: effectiveShouldRevertGrid ? gridRevertKey : null,
941956
slivers: <Widget>[
942957
if (isAppleOS)
943958
SliverGap.v(context.topPadding + kToolbarHeight),
944959
_sliverGrid(_, assets),
945960
// Ignore the gap when the [anchor] is not equal to 1.
946-
if (isAppleOS && anchor == 1)
947-
SliverGap.v(
948-
context.bottomPadding + bottomSectionHeight,
949-
),
950-
if (isAppleOS)
961+
if (effectiveShouldRevertGrid && anchor == 1)
962+
_bottomGap,
963+
if (effectiveShouldRevertGrid)
951964
SliverToBoxAdapter(
952965
key: gridRevertKey,
953966
child: const SizedBox.shrink(),
954967
),
968+
if (isAppleOS && !effectiveShouldRevertGrid) _bottomGap,
955969
],
956970
);
957971
},

lib/src/widget/asset_picker.dart

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,7 @@ import 'package:flutter/services.dart';
99
import '../constants/constants.dart';
1010

1111
class AssetPicker<Asset, Path> extends StatefulWidget {
12-
const AssetPicker({
13-
Key? key,
14-
required this.builder,
15-
}) : super(key: key);
12+
const AssetPicker({Key? key, required this.builder}) : super(key: key);
1613

1714
final AssetPickerBuilderDelegate<Asset, Path> builder;
1815

@@ -47,6 +44,7 @@ class AssetPicker<Asset, Path> extends StatefulWidget {
4744
SpecialItemPosition specialItemPosition = SpecialItemPosition.none,
4845
bool allowSpecialItemWhenEmpty = false,
4946
AssetSelectPredicate<AssetEntity>? selectPredicate,
47+
bool? shouldRevertGrid,
5048
bool useRootNavigator = true,
5149
Curve routeCurve = Curves.easeIn,
5250
Duration routeDuration = const Duration(milliseconds: 300),
@@ -113,6 +111,7 @@ class AssetPicker<Asset, Path> extends StatefulWidget {
113111
loadingIndicatorBuilder: loadingIndicatorBuilder,
114112
allowSpecialItemWhenEmpty: allowSpecialItemWhenEmpty,
115113
selectPredicate: selectPredicate,
114+
shouldRevertGrid: shouldRevertGrid,
116115
),
117116
),
118117
);

0 commit comments

Comments
 (0)