assetGridItemBuilder method
Widget
assetGridItemBuilder(
- BuildContext context,
- int index,
- List<
AssetEntity> currentAssets, { - Widget? specialItem,
override
There are several conditions within this builder:
- Return item builder according to the asset's type.
- Load more assets when the index reached at third line counting backwards.
资源构建有几个条件:
- 根据资源类型返回对应类型的构建:
- AssetType.audio -> audioItemBuilder 音频类型
- AssetType.image, AssetType.video -> imageAndVideoItemBuilder 图片和视频类型
- 在索引到达倒数第三列的时候加载更多资源。
Implementation
@override
Widget assetGridItemBuilder(
BuildContext context,
int index,
List<AssetEntity> currentAssets, {
Widget? specialItem,
}) {
final DefaultAssetPickerProvider p =
context.read<DefaultAssetPickerProvider>();
final int length = currentAssets.length;
final PathWrapper<AssetPathEntity>? currentWrapper = p.currentPath;
final AssetPathEntity? currentPathEntity = currentWrapper?.path;
if (specialItem != null) {
if ((index == 0 && specialItemPosition == SpecialItemPosition.prepend) ||
(index == length &&
specialItemPosition == SpecialItemPosition.append)) {
return specialItem;
}
}
final int currentIndex;
if (specialItem != null &&
specialItemPosition == SpecialItemPosition.prepend) {
currentIndex = index - 1;
} else {
currentIndex = index;
}
if (currentPathEntity == null) {
return const SizedBox.shrink();
}
if (p.hasMoreToLoad) {
if ((p.pageSize <= gridCount * 3 && index == length - 1) ||
index == length - gridCount * 3) {
p.loadMoreAssets();
}
}
final AssetEntity asset = currentAssets.elementAt(currentIndex);
final Widget builder;
switch (asset.type) {
case AssetType.audio:
builder = audioItemBuilder(context, currentIndex, asset);
break;
case AssetType.image:
case AssetType.video:
builder = imageAndVideoItemBuilder(context, currentIndex, asset);
break;
case AssetType.other:
builder = const SizedBox.shrink();
break;
}
final Widget content = Stack(
key: ValueKey<String>(asset.id),
children: <Widget>[
builder,
selectedBackdrop(context, currentIndex, asset),
if (!isWeChatMoment || asset.type != AssetType.video)
selectIndicator(context, index, asset),
itemBannedIndicator(context, asset),
],
);
return assetGridItemSemanticsBuilder(context, index, asset, content);
}