assetGridItemBuilder method

  1. @override
Widget assetGridItemBuilder(
  1. BuildContext context,
  2. int index,
  3. List<AssetEntity> currentAssets, {
  4. Widget? specialItem,
})
override

There are several conditions within this builder:

资源构建有几个条件:

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) {
    AssetType.image ||
    AssetType.video =>
      imageAndVideoItemBuilder(context, currentIndex, asset),
    AssetType.audio => audioItemBuilder(context, currentIndex, asset),
    AssetType.other => const SizedBox.shrink(),
  };
  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);
}