Skip to content

Commit aabc243

Browse files
committed
✨ Add findChildIndexBuilder to indicates grid find reusable RenderObject
1 parent 18db400 commit aabc243

File tree

2 files changed

+29
-4
lines changed

2 files changed

+29
-4
lines changed

example/lib/customs/pickers/directory_file_asset_picker.dart

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -557,6 +557,7 @@ class FileAssetPickerBuilder
557557
asset,
558558
);
559559
return Stack(
560+
key: ValueKey<String>(asset.path),
560561
fit: StackFit.expand,
561562
children: <Widget>[
562563
Positioned.fill(child: builder),
@@ -1044,6 +1045,11 @@ class FileAssetPickerBuilder
10441045
Widget videoIndicator(BuildContext context, File asset) {
10451046
return const SizedBox.shrink();
10461047
}
1048+
1049+
@override
1050+
int findChildIndexBuilder(String id, List<File> currentAssets) {
1051+
return currentAssets.indexWhere((File file) => file.path == id);
1052+
}
10471053
}
10481054

10491055
class FileAssetPickerViewerProvider extends AssetPickerViewerProvider<File> {

lib/src/delegates/asset_picker_builder_delegate.dart

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -252,7 +252,6 @@ abstract class AssetPickerBuilderDelegate<A, P> {
252252
/// The main grid view builder for assets.
253253
/// 主要的资源查看网格部件
254254
Widget assetsGridBuilder(BuildContext context) {
255-
final int hashCode = provider.currentPathEntity?.hashCode ?? 0;
256255
return ColoredBox(
257256
color: theme.canvasColor,
258257
child: Selector<AssetPickerProvider<A, P>, List<A>>(
@@ -270,16 +269,22 @@ abstract class AssetPickerBuilderDelegate<A, P> {
270269
SliverGrid(
271270
delegate: SliverChildBuilderDelegate(
272271
(_, int index) => Builder(
273-
key: ValueKey<int>(index + hashCode),
274272
builder: (BuildContext c) => assetGridItemBuilder(
275273
c,
276274
index,
277275
currentAssets,
278276
),
279277
),
280278
childCount: assetsGridItemCount(_, currentAssets),
281-
findChildIndexCallback: (Key key) =>
282-
(key as ValueKey<int>).value - hashCode,
279+
findChildIndexCallback: (Key? key) {
280+
if (key is ValueKey<String>) {
281+
return findChildIndexBuilder(
282+
key.value,
283+
currentAssets,
284+
);
285+
}
286+
return null;
287+
},
283288
),
284289
gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
285290
crossAxisCount: gridCount,
@@ -299,6 +304,10 @@ abstract class AssetPickerBuilderDelegate<A, P> {
299304
);
300305
}
301306

307+
/// Indicates how would the grid found a reusable [RenderObject] through [id].
308+
/// 为 Grid 布局指示如何找到可复用的 [RenderObject]
309+
int findChildIndexBuilder(String id, List<A> currentAssets);
310+
302311
/// The function which return items count for the assets' grid.
303312
/// 为资源列表提供内容数量计算的方法
304313
int assetsGridItemCount(BuildContext context, List<A> currentAssets);
@@ -697,6 +706,7 @@ class DefaultAssetPickerBuilderDelegate
697706
break;
698707
}
699708
return Stack(
709+
key: ValueKey<String>(asset.id),
700710
children: <Widget>[
701711
builder,
702712
if (!isWeChatMoment || asset.type != AssetType.video)
@@ -705,6 +715,15 @@ class DefaultAssetPickerBuilderDelegate
705715
);
706716
}
707717

718+
@override
719+
int findChildIndexBuilder(String id, List<AssetEntity> currentAssets) {
720+
int index = currentAssets.indexWhere((AssetEntity e) => e.id == id);
721+
if (specialItemPosition == SpecialItemPosition.prepend) {
722+
index += 1;
723+
}
724+
return index;
725+
}
726+
708727
@override
709728
int assetsGridItemCount(
710729
BuildContext context,

0 commit comments

Comments
 (0)