import '../../../../ui/adaptive/segmented_bar.dart'; import 'dart:typed_data'; import 'package:flutter/cupertino.dart'; import 'package:flutter/material.dart'; /// The accessible name and visible label of the viewer's close control. const String chatImageViewerClose = 'Close'; /// One attached image full screen: pinch or scroll to zoom, drag to pan once /// zoomed, and the host's own close control in the navigation bar. class ChatImageViewer extends StatelessWidget { const ChatImageViewer({ super.key, required this.name, required this.bytes, required this.cupertino, }); final String name; final Uint8List bytes; final bool cupertino; @override Widget build(BuildContext context) { final body = InteractiveViewer( key: const Key('chat-image-viewer-zoom'), maxScale: 8, child: Center( child: Semantics( image: true, label: name, child: Image.memory( bytes, key: const Key('chat-image-viewer-image'), fit: BoxFit.contain, gaplessPlayback: true, errorBuilder: (context, _, _) => Icon( cupertino ? CupertinoIcons.photo : Icons.broken_image_outlined, ), ), ), ), ); void close() => Navigator.of(context).maybePop(); if (cupertino) { return CupertinoPageScaffold( backgroundColor: CupertinoColors.black, navigationBar: SegmentedBar( cupertino: true, automaticallyImplyLeading: false, title: name, backgroundColor: CupertinoColors.black, pillColor: CupertinoColors.darkBackgroundGray, foregroundColor: CupertinoColors.white, leading: CupertinoButton( key: const Key('chat-image-viewer-close'), padding: const EdgeInsets.symmetric(horizontal: 16), onPressed: close, child: const Text(chatImageViewerClose), ), ), child: body, ); } return Scaffold( extendBodyBehindAppBar: true, backgroundColor: Colors.black, appBar: SegmentedBar( cupertino: false, automaticallyImplyLeading: false, leading: IconButton( key: const Key('chat-image-viewer-close'), tooltip: chatImageViewerClose, onPressed: close, icon: const Icon(Icons.close), ), title: name, backgroundColor: Colors.black, pillColor: Colors.white24, foregroundColor: Colors.white, ), body: body, ); } } /// Pushes [ChatImageViewer] with the host's full-screen dialog transition. Future openChatImageViewer( BuildContext context, { required String name, required Uint8List bytes, required bool cupertino, }) { final page = ChatImageViewer(name: name, bytes: bytes, cupertino: cupertino); final route = cupertino ? CupertinoPageRoute(fullscreenDialog: true, builder: (_) => page) : MaterialPageRoute(fullscreenDialog: true, builder: (_) => page); return Navigator.of(context).push(route); }