import 'package:cached_network_image/cached_network_image.dart'; import 'package:dismissible_page/dismissible_page.dart'; import 'package:eatvio_app/core/app_config.dart'; import 'package:eatvio_app/core/locator_service.dart'; import 'package:eatvio_app/core/theme/app_decoration.dart'; import 'package:flutter/material.dart'; import 'package:go_router/go_router.dart'; import 'package:photo_view/photo_view.dart'; import 'package:photo_view/photo_view_gallery.dart'; import 'package:url_launcher/url_launcher.dart'; class PhotoViewScreen extends StatefulWidget { final List imageUrls; final int index; final bool isLocal; /// When set, used instead of the image URL so source/destination tags stay unique. final Object? heroTag; const PhotoViewScreen({ super.key, required this.imageUrls, required this.index, this.isLocal = false, this.heroTag, }); @override State createState() => _PhotoViewScreenState(); } class _PhotoViewScreenState extends State { late final PageController pageController; int imageIndex = 0; @override void initState() { imageIndex = widget.index; pageController = PageController(initialPage: imageIndex); super.initState(); } PhotoViewHeroAttributes? _heroAttributesFor(int index) { if (widget.heroTag != null) { if (index != widget.index) { return null; } return PhotoViewHeroAttributes( tag: widget.heroTag!, transitionOnUserGestures: true, ); } return PhotoViewHeroAttributes( tag: widget.imageUrls[index], transitionOnUserGestures: true, ); } @override Widget build(BuildContext context) { return DismissiblePage( minScale: 1, onDismissed: () => GoRouter.of(context).pop(), direction: DismissiblePageDismissDirection.vertical, isFullScreen: true, child: PhotoViewGallery.builder( backgroundDecoration: const BoxDecoration(color: Colors.transparent), scrollPhysics: const BouncingScrollPhysics(), builder: (BuildContext context, int index) { final imageProvider = widget.isLocal ? FileImage(widget.imageUrls[index]) as ImageProvider : CachedNetworkImageProvider(widget.imageUrls[index]) as ImageProvider; return PhotoViewGalleryPageOptions( imageProvider: imageProvider, initialScale: PhotoViewComputedScale.contained, minScale: PhotoViewComputedScale.contained, heroAttributes: _heroAttributesFor(index), errorBuilder: (context, object, stackTrace) { return Scaffold( backgroundColor: Colors.black, body: SafeArea( child: Column( children: [ const SizedBox(height: 32), const Text('Файл'), const SizedBox(height: 8), ListTile( shape: RoundedRectangleBorder( borderRadius: BorderRadiusStyle.circleBorder17, ), contentPadding: const EdgeInsets.all(16), onTap: () async { if (!await launchUrl( Uri.parse(widget.imageUrls[index]), )) { throw Exception( 'Could not launch ${widget.imageUrls[index]}', ); } }, leading: const Icon( Icons.file_copy_outlined, color: Colors.white, ), trailing: const Icon( Icons.download_outlined, color: Colors.white, ), title: Text( widget.imageUrls[index].replaceAll( '${sl().baseUrl}/storage/chat_files/', '', ), style: const TextStyle( color: Colors.white, fontSize: 12, ), ), ), ], ), ), ); }, ); }, itemCount: widget.imageUrls.length, loadingBuilder: (context, event) => Center( child: SizedBox( width: 20.0, height: 20.0, child: CircularProgressIndicator( value: event == null ? 0 : event.cumulativeBytesLoaded / event.expectedTotalBytes!, ), ), ), pageController: pageController, onPageChanged: (page) { setState(() { imageIndex = page; }); }, ), ); } }