Skip to content

Commit cd20f2f

Browse files
authored
🐛 Fix index reverting in viewAsset (#570)
Fixes #569
1 parent 717488e commit cd20f2f

File tree

4 files changed

+32
-22
lines changed

4 files changed

+32
-22
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,12 @@ that can be found in the LICENSE file. -->
77
> [!IMPORTANT]
88
> See the [Migration Guide](guides/migration_guide.md) for the details of breaking changes between versions.
99
10+
## 9.0.3
11+
12+
### Fixes
13+
14+
- Fix index reverting in `viewAsset`.
15+
1016
## 9.0.2
1117

1218
### Fixes

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: 9.0.2+51
3+
version: 9.0.3+52
44
publish_to: none
55

66
environment:

lib/src/delegates/asset_picker_builder_delegate.dart

Lines changed: 24 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -869,18 +869,16 @@ class DefaultAssetPickerBuilderDelegate
869869
int? index,
870870
AssetEntity currentAsset,
871871
) async {
872-
final DefaultAssetPickerProvider provider =
873-
context.read<DefaultAssetPickerProvider>();
872+
final p = context.read<DefaultAssetPickerProvider>();
874873
// - When we reached the maximum select count and the asset is not selected,
875874
// do nothing.
876875
// - When the special type is WeChat Moment, pictures and videos cannot
877876
// be selected at the same time. Video select should be banned if any
878877
// pictures are selected.
879-
if ((!provider.selectedAssets.contains(currentAsset) &&
880-
provider.selectedMaximumAssets) ||
878+
if ((!p.selectedAssets.contains(currentAsset) && p.selectedMaximumAssets) ||
881879
(isWeChatMoment &&
882880
currentAsset.type == AssetType.video &&
883-
provider.selectedAssets.isNotEmpty)) {
881+
p.selectedAssets.isNotEmpty)) {
884882
return;
885883
}
886884
final revert = effectiveShouldRevertGrid(context);
@@ -893,23 +891,29 @@ class DefaultAssetPickerBuilderDelegate
893891
selected = null;
894892
effectiveIndex = 0;
895893
} else {
896-
current = provider.currentAssets
894+
if (index == null) {
895+
current = p.selectedAssets;
896+
current = current.reversed.toList(growable: false);
897+
} else {
898+
current = p.currentAssets;
899+
}
900+
current = current
897901
.where((AssetEntity e) => e.type == AssetType.image)
898902
.toList();
899-
selected = provider.selectedAssets;
900-
effectiveIndex = current.indexOf(currentAsset);
903+
selected = p.selectedAssets;
904+
final i = current.indexOf(currentAsset);
905+
effectiveIndex = revert ? current.length - i - 1 : i;
901906
}
902-
} else if (index == null) {
903-
current = provider.selectedAssets;
904-
selected = provider.selectedAssets;
905-
effectiveIndex = selected.indexOf(currentAsset);
906907
} else {
907-
current = provider.currentAssets;
908-
selected = provider.selectedAssets;
909-
effectiveIndex = revert ? current.length - index - 1 : index;
910-
}
911-
if (revert && index == null) {
912-
current = current.reversed.toList(growable: false);
908+
selected = p.selectedAssets;
909+
if (index == null) {
910+
current = p.selectedAssets;
911+
current = current.reversed.toList(growable: false);
912+
effectiveIndex = selected.indexOf(currentAsset);
913+
} else {
914+
current = p.currentAssets;
915+
effectiveIndex = revert ? current.length - index - 1 : index;
916+
}
913917
}
914918
final List<AssetEntity>? result = await AssetPickerViewer.pushToViewer(
915919
context,
@@ -919,9 +923,9 @@ class DefaultAssetPickerBuilderDelegate
919923
previewThumbnailSize: previewThumbnailSize,
920924
selectPredicate: selectPredicate,
921925
selectedAssets: selected,
922-
selectorProvider: provider,
926+
selectorProvider: p,
923927
specialPickerType: specialPickerType,
924-
maxAssets: provider.maxAssets,
928+
maxAssets: p.maxAssets,
925929
shouldReversePreview: revert,
926930
);
927931
if (result != null) {

pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name: wechat_assets_picker
2-
version: 9.0.2
2+
version: 9.0.3
33
description: |
44
An image picker (also with videos and audio)
55
for Flutter projects based on WeChat's UI,

0 commit comments

Comments
 (0)