viewAsset method

  1. @override
Future<void> viewAsset(
  1. BuildContext context,
  2. int index,
  3. AssetEntity currentAsset
)
override

Determine how to browse assets in the viewer. 定义如何在查看器中浏览资源

Implementation

@override
Future<void> viewAsset(
  BuildContext context,
  int index,
  AssetEntity currentAsset,
) async {
  final DefaultAssetPickerProvider provider =
      context.read<DefaultAssetPickerProvider>();
  bool selectedAllAndNotSelected() =>
      !provider.selectedAssets.contains(currentAsset) &&
      provider.selectedMaximumAssets;
  bool selectedPhotosAndIsVideo() =>
      isWeChatMoment &&
      currentAsset.type == AssetType.video &&
      provider.selectedAssets.isNotEmpty;
  // When we reached the maximum select count and the asset
  // is not selected, do nothing.
  // When the special type is WeChat Moment, pictures and videos cannot
  // be selected at the same time. Video select should be banned if any
  // pictures are selected.
  if (selectedAllAndNotSelected() || selectedPhotosAndIsVideo()) {
    return;
  }
  final List<AssetEntity> current;
  final List<AssetEntity>? selected;
  final int effectiveIndex;
  if (isWeChatMoment) {
    if (currentAsset.type == AssetType.video) {
      current = <AssetEntity>[currentAsset];
      selected = null;
      effectiveIndex = 0;
    } else {
      current = provider.currentAssets
          .where((AssetEntity e) => e.type == AssetType.image)
          .toList();
      selected = provider.selectedAssets;
      effectiveIndex = current.indexOf(currentAsset);
    }
  } else {
    current = provider.currentAssets;
    selected = provider.selectedAssets;
    effectiveIndex = index;
  }
  final List<AssetEntity>? result = await AssetPickerViewer.pushToViewer(
    context,
    currentIndex: effectiveIndex,
    previewAssets: current,
    themeData: theme,
    previewThumbnailSize: previewThumbnailSize,
    selectPredicate: selectPredicate,
    selectedAssets: selected,
    selectorProvider: provider,
    specialPickerType: specialPickerType,
    maxAssets: provider.maxAssets,
    shouldReversePreview: isAppleOS,
  );
  if (result != null) {
    Navigator.of(context).maybePop(result);
  }
}