Files
eatvio_app/lib/features/chat/presentation/pages/photo_view_screen.dart
2024-06-19 16:00:18 +03:00

125 lines
4.3 KiB
Dart

import 'package:eatvio_app/core/app_export.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_dotenv/flutter_dotenv.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<String> imageUrls;
final int index;
const PhotoViewScreen({
super.key,
required this.imageUrls,
required this.index,
});
@override
State<PhotoViewScreen> createState() => _PhotoViewScreenState();
}
class _PhotoViewScreenState extends State<PhotoViewScreen> {
late final pageController;
int imageIndex = 0;
@override
void initState() {
imageIndex = widget.index;
pageController = PageController(initialPage: imageIndex);
super.initState();
}
@override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: Colors.black,
appBar: AppBar(
centerTitle: true,
systemOverlayStyle: SystemUiOverlayStyle(
statusBarColor: Colors.black,
statusBarBrightness: Brightness.dark,
statusBarIconBrightness: Brightness.light,
),
backgroundColor: Colors.black,
foregroundColor: Colors.white,
title: Text(
(imageIndex + 1).toString() + ' из ' + widget.imageUrls.length.toString(),
),
),
body: Dismissible(
direction: DismissDirection.vertical,
key: const Key('key'),
onDismissed: (_) => GoRouter.of(context).pop(),
resizeDuration: Duration(microseconds: 1),
child: Container(
child: PhotoViewGallery.builder(
scrollPhysics: const BouncingScrollPhysics(),
builder: (BuildContext context, int index) {
return PhotoViewGalleryPageOptions(
imageProvider: NetworkImage(widget.imageUrls[index]),
initialScale: PhotoViewComputedScale.contained,
minScale: PhotoViewComputedScale.contained,
heroAttributes: PhotoViewHeroAttributes(tag: widget.imageUrls[index]),
errorBuilder: (context, object, stackTrace) {
return Column(
children: [
SizedBox(height: 32),
Text('Файл'),
SizedBox(height: 8),
ListTile(
shape: RoundedRectangleBorder(
borderRadius: BorderRadiusStyle.circleBorder17,
),
contentPadding: EdgeInsets.all(16),
onTap: () async {
if (!await launchUrl(Uri.parse(widget.imageUrls[index]))) {
throw Exception('Could not launch ${widget.imageUrls[index]}');
}
},
leading: Icon(
Icons.file_copy_outlined,
color: Colors.white,
),
trailing: Icon(
Icons.download_outlined,
color: Colors.white,
),
title: Text(
widget.imageUrls[index].replaceAll(dotenv.env['STORAGE_URL']! + 'chat_files/', ''),
style: TextStyle(
color: Colors.white,
fontSize: 12,
),
),
),
],
);
},
);
},
itemCount: widget.imageUrls.length,
loadingBuilder: (context, event) => Center(
child: Container(
width: 20.0,
height: 20.0,
child: CircularProgressIndicator(
value: event == null ? 0 : event.cumulativeBytesLoaded / event.expectedTotalBytes!,
),
),
),
pageController: pageController,
onPageChanged: (page) {
setState(() {
imageIndex = page;
});
},
),
),
),
);
}
}