Skip to content

Commit d39d74f

Browse files
authored
🚀 Abstract viewAsset in AssetPickerBuilderDelegate (#391)
* 🚀 Abstract `viewAsset` in `AssetPickerBuilderDelegate` * 🚀 Adopt in the example * 🔖 8.2.0
1 parent c495bb1 commit d39d74f

File tree

6 files changed

+81
-14
lines changed

6 files changed

+81
-14
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,12 @@ that can be found in the LICENSE file. -->
44

55
# Changelog
66

7+
## 8.2.0
8+
9+
### New features
10+
11+
- Allow overrides `viewAsset` in the `AssetPickerBuilderDelegate`. (#391)
12+
713
## 8.1.4
814

915
### Fixes

example/lib/customs/pickers/directory_file_asset_picker.dart

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -388,6 +388,37 @@ class FileAssetPickerBuilder
388388
@override
389389
bool get isSingleAssetMode => provider.maxAssets == 1;
390390

391+
@override
392+
Future<void> viewAsset(
393+
BuildContext context,
394+
int index,
395+
AssetEntity currentAsset,
396+
) async {
397+
final List<File>? result = await Navigator.of(context).push<List<File>?>(
398+
PageRouteBuilder<List<File>>(
399+
pageBuilder: (
400+
BuildContext context,
401+
Animation<double> animation,
402+
Animation<double> secondaryAnimation,
403+
) {
404+
return AssetPickerViewer<File, Directory>(
405+
builder: FileAssetPickerViewerBuilderDelegate(
406+
currentIndex: index,
407+
previewAssets: provider.selectedAssets,
408+
provider: FileAssetPickerViewerProvider(provider.selectedAssets),
409+
themeData: AssetPicker.themeData(themeColor),
410+
selectedAssets: provider.selectedAssets,
411+
selectorProvider: provider,
412+
),
413+
);
414+
},
415+
),
416+
);
417+
if (result != null) {
418+
Navigator.of(context).maybePop(result);
419+
}
420+
}
421+
391422
Future<List<File>?> pushToPicker(
392423
BuildContext context, {
393424
required int index,

example/pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name: wechat_assets_picker_demo
22
description: The demo project for the wechat_assets_picker package.
3-
version: 8.1.4+29
3+
version: 8.2.0+30
44
publish_to: none
55

66
environment:

guides/migration_guide.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,34 @@ This document gathered all breaking changes and migrations requirement between m
88

99
## Major versions
1010

11+
- [8.2.0](#820)
1112
- [8.0.0](#800)
1213
- [7.0.0](#700)
1314
- [6.0.0](#600)
1415
- [5.0.0](#500)
1516

17+
## 8.2.0
18+
19+
### Summary
20+
21+
Delegates that extend `AssetPickerBuilderDelegate` should now implement `viewAsset`.
22+
Delegates that extend `DefaultAssetPickerBuilderDelegate` are not required to do so.
23+
24+
### Details
25+
26+
`viewAsset` is abstracted in the `AssetPickerBuilderDelegate`:
27+
28+
```dart
29+
Future<void> viewAsset(
30+
BuildContext context,
31+
int index,
32+
AssetEntity currentAsset,
33+
);
34+
```
35+
36+
The new method is implemented in the `DefaultAssetPickerBuilderDelegate`.
37+
It's a private method previously which not allow to modify.
38+
1639
## 8.0.0
1740

1841
### Summary

lib/src/delegates/asset_picker_builder_delegate.dart

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,14 @@ abstract class AssetPickerBuilderDelegate<Asset, Path> {
232232
/// 系统发出资源变更的通知时调用的方法
233233
Future<void> onAssetsChanged(MethodCall call, StateSetter setState) async {}
234234

235+
/// Determine how to browse assets in the viewer.
236+
/// 定义如何在查看器中浏览资源
237+
Future<void> viewAsset(
238+
BuildContext context,
239+
int index,
240+
AssetEntity currentAsset,
241+
);
242+
235243
/// Yes, the build method.
236244
/// 没错,是它是它就是它,我们亲爱的 build 方法~
237245
Widget build(BuildContext context);
@@ -824,19 +832,20 @@ class DefaultAssetPickerBuilderDelegate
824832
}
825833
}
826834

827-
Future<void> _pushAssetToViewer(
835+
@override
836+
Future<void> viewAsset(
828837
BuildContext context,
829838
int index,
830-
AssetEntity asset,
839+
AssetEntity currentAsset,
831840
) async {
832841
final DefaultAssetPickerProvider provider =
833842
context.read<DefaultAssetPickerProvider>();
834843
bool selectedAllAndNotSelected() =>
835-
!provider.selectedAssets.contains(asset) &&
844+
!provider.selectedAssets.contains(currentAsset) &&
836845
provider.selectedMaximumAssets;
837846
bool selectedPhotosAndIsVideo() =>
838847
isWeChatMoment &&
839-
asset.type == AssetType.video &&
848+
currentAsset.type == AssetType.video &&
840849
provider.selectedAssets.isNotEmpty;
841850
// When we reached the maximum select count and the asset
842851
// is not selected, do nothing.
@@ -850,16 +859,16 @@ class DefaultAssetPickerBuilderDelegate
850859
final List<AssetEntity>? selected;
851860
final int effectiveIndex;
852861
if (isWeChatMoment) {
853-
if (asset.type == AssetType.video) {
854-
current = <AssetEntity>[asset];
862+
if (currentAsset.type == AssetType.video) {
863+
current = <AssetEntity>[currentAsset];
855864
selected = null;
856865
effectiveIndex = 0;
857866
} else {
858867
current = provider.currentAssets
859868
.where((AssetEntity e) => e.type == AssetType.image)
860869
.toList();
861870
selected = provider.selectedAssets;
862-
effectiveIndex = current.indexOf(asset);
871+
effectiveIndex = current.indexOf(currentAsset);
863872
}
864873
} else {
865874
current = provider.currentAssets;
@@ -1323,7 +1332,7 @@ class DefaultAssetPickerBuilderDelegate
13231332
onTap: () => selectAsset(context, asset, isSelected),
13241333
onTapHint: semanticsTextDelegate.sActionSelectHint,
13251334
onLongPress: isPreviewEnabled
1326-
? () => _pushAssetToViewer(context, index, asset)
1335+
? () => viewAsset(context, index, asset)
13271336
: null,
13281337
onLongPressHint: semanticsTextDelegate.sActionPreviewHint,
13291338
selected: isSelected,
@@ -1336,7 +1345,7 @@ class DefaultAssetPickerBuilderDelegate
13361345
// Regression https://github.com/flutter/flutter/issues/35112.
13371346
onLongPress:
13381347
isPreviewEnabled && context.mediaQuery.accessibleNavigation
1339-
? () => _pushAssetToViewer(context, index, asset)
1348+
? () => viewAsset(context, index, asset)
13401349
: null,
13411350
child: IndexedSemantics(
13421351
index: semanticIndex(index),
@@ -2041,9 +2050,7 @@ class DefaultAssetPickerBuilderDelegate
20412050
final double indicatorSize = context.mediaQuery.size.width / gridCount / 3;
20422051
return Positioned.fill(
20432052
child: GestureDetector(
2044-
onTap: isPreviewEnabled
2045-
? () => _pushAssetToViewer(context, index, asset)
2046-
: null,
2053+
onTap: isPreviewEnabled ? () => viewAsset(context, index, asset) : null,
20472054
child: Consumer<DefaultAssetPickerProvider>(
20482055
builder: (_, DefaultAssetPickerProvider p, __) {
20492056
final int index = p.selectedAssets.indexOf(asset);

pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ description: |
33
An audio/video/image picker in pure Dart which is the same with WeChat,
44
support multi picking, custom item and delegates override.
55
repository: https://github.com/fluttercandies/flutter_wechat_assets_picker
6-
version: 8.1.4
6+
version: 8.2.0
77

88
environment:
99
sdk: '>=2.17.0 <3.0.0'

0 commit comments

Comments
 (0)