previewButton method

  1. @override
Widget previewButton(
  1. BuildContext context
)
override

Preview button to preview selected assets. 预览已选资源的按钮

Implementation

@override
Widget previewButton(BuildContext context) {
  Future<void> _onTap() async {
    final DefaultAssetPickerProvider p =
        context.read<DefaultAssetPickerProvider>();
    final List<AssetEntity> selectedAssets = p.selectedAssets;
    final List<AssetEntity> selected;
    if (isWeChatMoment) {
      selected = selectedAssets
          .where((AssetEntity e) => e.type == AssetType.image)
          .toList();
    } else {
      selected = selectedAssets;
    }
    final List<AssetEntity>? result = await AssetPickerViewer.pushToViewer(
      context,
      previewAssets: selected,
      previewThumbnailSize: previewThumbnailSize,
      selectPredicate: selectPredicate,
      selectedAssets: selected,
      selectorProvider: provider,
      themeData: theme,
      maxAssets: p.maxAssets,
    );
    if (result != null) {
      Navigator.of(context).maybePop(result);
    }
  }

  return Consumer<DefaultAssetPickerProvider>(
    builder: (_, DefaultAssetPickerProvider p, Widget? child) {
      return ValueListenableBuilder<bool>(
        valueListenable: isSwitchingPath,
        builder: (_, bool isSwitchingPath, __) => Semantics(
          enabled: p.isSelectedNotEmpty,
          focusable: !isSwitchingPath,
          hidden: isSwitchingPath,
          onTapHint: semanticsTextDelegate.sActionPreviewHint,
          child: child,
        ),
      );
    },
    child: Consumer<DefaultAssetPickerProvider>(
      builder: (_, DefaultAssetPickerProvider p, __) => GestureDetector(
        onTap: p.isSelectedNotEmpty ? _onTap : null,
        child: Selector<DefaultAssetPickerProvider, String>(
          selector: (_, DefaultAssetPickerProvider p) =>
              p.selectedDescriptions,
          builder: (BuildContext c, __, ___) => Padding(
            padding: const EdgeInsets.symmetric(vertical: 12),
            child: ScaleText(
              '${textDelegate.preview}'
              '${p.isSelectedNotEmpty ? ' (${p.selectedAssets.length})' : ''}',
              style: TextStyle(
                color: p.isSelectedNotEmpty
                    ? null
                    : c.themeData.textTheme.bodySmall?.color,
                fontSize: 17,
              ),
              maxScaleFactor: 1.2,
              semanticsLabel: '${semanticsTextDelegate.preview}'
                  '${p.isSelectedNotEmpty ? ' (${p.selectedAssets.length})' : ''}',
            ),
          ),
        ),
      ),
    ),
  );
}