Skip to content

Commit 91ddee6

Browse files
authored
🚀 Customizable AssetPickerPageRoute (#248)
1 parent 34f133f commit 91ddee6

File tree

5 files changed

+54
-62
lines changed

5 files changed

+54
-62
lines changed

README-ZH.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -190,8 +190,7 @@ platform :ios, '9.0'
190190
| allowSpecialItemWhenEmpty | `bool` | 在资源为空时是否允许显示自定义item | `false` |
191191
| selectPredicate | `AssetSelectPredicate` | 判断资源可否被选择 | `null` |
192192
| shouldRevertGrid | `bool?` | 判断资源网格是否需要倒序排列 | `null` |
193-
| routeCurve | `Curve` | 选择构造路由动画的曲线 | `Curves.easeIn` |
194-
| routeDuration | `Duration` | 选择构造路由动画的时间 | `const Duration(milliseconds: 500)` |
193+
| pageRouteBuilder | `AssetPickerPageRouteBuilder` | 构建 `AssetPickerPageRoute` | `null` |
195194

196195
### 简单的使用方法
197196

README.md

Lines changed: 23 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -162,30 +162,29 @@ platform :osx, '10.15'
162162

163163
## Usage 📖
164164

165-
| Name | Type | Description | Default |
166-
|---------------------------|-----------------------------|---------------------------------------------------------------------------------------------------------------------|-------------------------------------|
167-
| selectedAssets | `List<AssetEntity>?` | Selected assets. Prevent duplicate selection. If you don't need to prevent duplicate selection, just don't pass it. | `null` |
168-
| maxAssets | `int` | Maximum asset that the picker can pick. | 9 |
169-
| pageSize | `int?` | Number of assets per page. **Must be a multiple of `gridCount`**. | 320 (80 * 4) |
170-
| gridThumbSize | `int` | Thumbnail size for the grid's item. | 200 |
171-
| pathThumbSize | `int` | Thumbnail size for the path selector. | 80 |
172-
| previewThumbSize | `List<int>?` | Preview thumbnail size in the viewer. | `null` |
173-
| gridCount | `int` | Grid count in picker. | 4 |
174-
| requestType | `RequestType` | Request type for picker. | `RequestType.image` |
175-
| specialPickerType | `SpacialPickerType?` | Provides the option to integrate a custom picker type. | `null` |
176-
| themeColor | `Color?` | Main theme color for the picker. | `Color(0xff00bc56)` |
177-
| pickerTheme | `ThemeData?` | Theme data provider for the picker and the viewer. | `null` |
178-
| sortPathDelegate | `SortPathDeleage?` | Path entities sort delegate for the picker, sort paths as you want. | `CommonSortPathDelegate` |
179-
| textDelegate | `AssetsPickerTextDelegate?` | Text delegate for the picker, for customize the texts. | `DefaultAssetsPickerTextDelegate()` |
180-
| filterOptions | `FilterOptionGroup?` | Allow users to customize assets filter options. | `null` |
181-
| specialItemBuilder | `WidgetBuilder?` | The widget builder for the special item. | `null` |
182-
| specialItemPosition | `SpecialItemPosition` | Allow users set a special item in the picker with several positions. | `SpecialItemPosition.none` |
183-
| loadingIndicatorBuilder | `IndicatorBuilder?` | Indicates the loading status for the builder. | `null` |
184-
| allowSpecialItemWhenEmpty | `bool` | Whether the special item will display or not when assets is empty. | `false` |
185-
| selectPredicate | `AssetSelectPredicate` | Predicate whether an asset can be selected or unselected. | `null` |
186-
| shouldRevertGrid | `bool?` | Whether the assets grid should revert. | `null` |
187-
| routeCurve | `Curve` | The curve which the picker use to build page route transition. | `Curves.easeIn` |
188-
| routeDuration | `Duration` | The duration which the picker use to build page route transition. | `const Duration(milliseconds: 500)` |
165+
| Name | Type | Description | Default |
166+
|---------------------------|-------------------------------|---------------------------------------------------------------------------------------------------------------------|-------------------------------------|
167+
| selectedAssets | `List<AssetEntity>?` | Selected assets. Prevent duplicate selection. If you don't need to prevent duplicate selection, just don't pass it. | `null` |
168+
| maxAssets | `int` | Maximum asset that the picker can pick. | 9 |
169+
| pageSize | `int?` | Number of assets per page. **Must be a multiple of `gridCount`**. | 320 (80 * 4) |
170+
| gridThumbSize | `int` | Thumbnail size for the grid's item. | 200 |
171+
| pathThumbSize | `int` | Thumbnail size for the path selector. | 80 |
172+
| previewThumbSize | `List<int>?` | Preview thumbnail size in the viewer. | `null` |
173+
| gridCount | `int` | Grid count in picker. | 4 |
174+
| requestType | `RequestType` | Request type for picker. | `RequestType.image` |
175+
| specialPickerType | `SpacialPickerType?` | Provides the option to integrate a custom picker type. | `null` |
176+
| themeColor | `Color?` | Main theme color for the picker. | `Color(0xff00bc56)` |
177+
| pickerTheme | `ThemeData?` | Theme data provider for the picker and the viewer. | `null` |
178+
| sortPathDelegate | `SortPathDeleage?` | Path entities sort delegate for the picker, sort paths as you want. | `CommonSortPathDelegate` |
179+
| textDelegate | `AssetsPickerTextDelegate?` | Text delegate for the picker, for customize the texts. | `DefaultAssetsPickerTextDelegate()` |
180+
| filterOptions | `FilterOptionGroup?` | Allow users to customize assets filter options. | `null` |
181+
| specialItemBuilder | `WidgetBuilder?` | The widget builder for the special item. | `null` |
182+
| specialItemPosition | `SpecialItemPosition` | Allow users set a special item in the picker with several positions. | `SpecialItemPosition.none` |
183+
| loadingIndicatorBuilder | `IndicatorBuilder?` | Indicates the loading status for the builder. | `null` |
184+
| allowSpecialItemWhenEmpty | `bool` | Whether the special item will display or not when assets is empty. | `false` |
185+
| selectPredicate | `AssetSelectPredicate` | Predicate whether an asset can be selected or unselected. | `null` |
186+
| shouldRevertGrid | `bool?` | Whether the assets grid should revert. | `null` |
187+
| pageRouteBuilder | `AssetPickerPageRouteBuilder` | Build `AssetPickerPageRoute` with the given generic type. | `null` |
189188

190189
### Simple usage
191190

lib/src/provider/asset_picker_provider.dart

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,6 @@ class DefaultAssetPickerProvider
232232
int maxAssets = 9,
233233
int pageSize = 80,
234234
int pathThumbSize = 80,
235-
Duration routeDuration = const Duration(milliseconds: 300),
236235
}) : super(
237236
maxAssets: maxAssets,
238237
pageSize: pageSize,
@@ -241,12 +240,10 @@ class DefaultAssetPickerProvider
241240
) {
242241
Singleton.sortPathDelegate = sortPathDelegate ?? SortPathDelegate.common;
243242
// Call [getAssetList] with route duration when constructing.
244-
Future<void>.delayed(routeDuration).then(
245-
(dynamic _) async {
246-
await getPaths();
247-
await getAssetsFromCurrentPath();
248-
},
249-
);
243+
Future<void>(() async {
244+
await getPaths();
245+
await getAssetsFromCurrentPath();
246+
});
250247
}
251248

252249
/// Request assets type.

lib/src/widget/asset_picker.dart

Lines changed: 8 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,7 @@ class AssetPicker<Asset, Path> extends StatefulWidget {
5656
AssetSelectPredicate<AssetEntity>? selectPredicate,
5757
bool? shouldRevertGrid,
5858
bool useRootNavigator = true,
59-
Curve routeCurve = Curves.easeIn,
60-
Duration routeDuration = const Duration(milliseconds: 300),
59+
AssetPickerPageRouteBuilder<List<AssetEntity>>? pageRouteBuilder,
6160
}) async {
6261
if (maxAssets < 1) {
6362
throw ArgumentError(
@@ -99,7 +98,6 @@ class AssetPicker<Asset, Path> extends StatefulWidget {
9998
requestType: requestType,
10099
sortPathDelegate: sortPathDelegate,
101100
filterOptions: filterOptions,
102-
routeDuration: routeDuration,
103101
);
104102
final Widget picker = CNP<DefaultAssetPickerProvider>.value(
105103
value: provider,
@@ -129,24 +127,20 @@ class AssetPicker<Asset, Path> extends StatefulWidget {
129127
context,
130128
rootNavigator: useRootNavigator,
131129
).push<List<AssetEntity>>(
132-
AssetPickerPageRoute<List<AssetEntity>>(
133-
builder: picker,
134-
transitionCurve: routeCurve,
135-
transitionDuration: routeDuration,
136-
),
130+
pageRouteBuilder?.call(picker) ??
131+
AssetPickerPageRoute<List<AssetEntity>>(builder: (_) => picker),
137132
);
138133
return result;
139134
}
140135

141-
/// Call the picker with provided [delegate] and [provider].
142-
/// 通过指定的 [delegate] [provider] 调用选择器
136+
/// Call the picker with provided [delegate].
137+
/// 通过指定的 [delegate] 调用选择器
143138
static Future<List<Asset>?> pickAssetsWithDelegate<Asset, Path,
144139
PickerProvider extends AssetPickerProvider<Asset, Path>>(
145140
BuildContext context, {
146141
required AssetPickerBuilderDelegate<Asset, Path> delegate,
147142
bool useRootNavigator = true,
148-
Curve routeCurve = Curves.easeIn,
149-
Duration routeDuration = const Duration(milliseconds: 300),
143+
AssetPickerPageRouteBuilder<List<Asset>>? pageRouteBuilder,
150144
}) async {
151145
await permissionCheck();
152146

@@ -158,11 +152,8 @@ class AssetPicker<Asset, Path> extends StatefulWidget {
158152
context,
159153
rootNavigator: useRootNavigator,
160154
).push<List<Asset>>(
161-
AssetPickerPageRoute<List<Asset>>(
162-
builder: picker,
163-
transitionCurve: routeCurve,
164-
transitionDuration: routeDuration,
165-
),
155+
pageRouteBuilder?.call(picker) ??
156+
AssetPickerPageRoute<List<Asset>>(builder: (_) => picker),
166157
);
167158
return result;
168159
}

lib/src/widget/asset_picker_page_route.dart

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,36 +4,42 @@
44
///
55
import 'package:flutter/material.dart';
66

7+
/// Build [AssetPickerPageRoute] with the given generic type.
8+
/// 构建匹配泛型的 [AssetPickerPageRoute]
9+
typedef AssetPickerPageRouteBuilder<T> = AssetPickerPageRoute<T> Function(
10+
Widget picker,
11+
);
12+
713
/// Built a slide page transition for the picker.
814
/// 为选择器构造一个上下进出的页面过渡动画
915
class AssetPickerPageRoute<T> extends PageRoute<T> {
1016
AssetPickerPageRoute({
1117
required this.builder,
1218
this.transitionCurve = Curves.easeIn,
1319
this.transitionDuration = const Duration(milliseconds: 500),
20+
this.barrierColor,
21+
this.barrierDismissible = false,
22+
this.barrierLabel,
23+
this.maintainState = true,
24+
this.opaque = true,
1425
});
1526

16-
final Widget builder;
27+
final WidgetBuilder builder;
1728

1829
final Curve transitionCurve;
19-
2030
@override
2131
final Duration transitionDuration;
2232

2333
@override
24-
final bool opaque = true;
25-
34+
final Color? barrierColor;
2635
@override
27-
final bool barrierDismissible = false;
28-
36+
final bool barrierDismissible;
2937
@override
30-
final bool maintainState = true;
31-
38+
final String? barrierLabel;
3239
@override
33-
Color? get barrierColor => null;
34-
40+
final bool opaque;
3541
@override
36-
String? get barrierLabel => null;
42+
final bool maintainState;
3743

3844
@override
3945
bool canTransitionFrom(TransitionRoute<dynamic> previousRoute) => false;
@@ -44,7 +50,7 @@ class AssetPickerPageRoute<T> extends PageRoute<T> {
4450
Animation<double> animation,
4551
Animation<double> secondaryAnimation,
4652
) {
47-
return builder;
53+
return builder(context);
4854
}
4955

5056
@override

0 commit comments

Comments
 (0)