showReplitAttachmentPreview function

Future<void> showReplitAttachmentPreview({
  1. required BuildContext context,
  2. required List<ReplitAttachment> attachments,
  3. String? initialId,
  4. int? initialIndex,
  5. String? closeTooltip,
})

Opens ReplitAttachmentPreview over the current route.

showReplitAttachmentPreview(
  context: context,
  attachments: attachments,
  initialId: tappedId,
)

This is what a ReplitAttachmentGroup tile does on tap when the group has no onTap of its own. Call it directly to open the preview from a host's own handler. An empty attachments list opens nothing.

The list is read once, at push time: the preview pages through a snapshot and is unaffected by later changes to the list the host passed.

Implementation

Future<void> showReplitAttachmentPreview({
  required BuildContext context,
  required List<ReplitAttachment> attachments,
  String? initialId,
  int? initialIndex,
  String? closeTooltip,
}) {
  if (attachments.isEmpty) return Future<void>.value();

  // Copied, not aliased: the route outlives the host's list, which it is free
  // to mutate — or empty — while the preview is open.
  final snapshot = List<ReplitAttachment>.unmodifiable(attachments);

  // An explicit index wins: ids only have to be unique within a group, so a
  // caller that already knows which tile was tapped must not be resolved by
  // an id lookup that would land on the first duplicate.
  final resolved =
      initialIndex ?? snapshot.indexWhere((a) => a.id == initialId);
  assert(
    initialIndex != null || initialId == null || resolved >= 0,
    'initialId "$initialId" does not match any attachment in the list',
  );

  return Navigator.of(context).push<void>(
    PageRouteBuilder<void>(
      // The preview frosts the page rather than covering it, so the route
      // underneath has to keep painting.
      opaque: false,
      fullscreenDialog: true,
      transitionDuration: _transition,
      reverseTransitionDuration: _transition,
      pageBuilder: (context, animation, secondaryAnimation) =>
          ReplitAttachmentPreview(
            attachments: snapshot,
            initialIndex: resolved.clamp(0, snapshot.length - 1),
            closeTooltip: closeTooltip,
          ),
      // No FadeTransition around the page: an opacity layer over a
      // BackdropFilter stops the filter sampling the route beneath, which
      // costs the blur and, on web, the filtered child's paint as well. The
      // preview frosts itself in instead, driven by this same animation so
      // the fade reverses on the way out.
      transitionsBuilder: (context, animation, secondaryAnimation, child) =>
          child,
    ),
  );
}