|
| 1 | +import 'package:flutter/material.dart'; |
| 2 | +import 'package:stream_feeds/stream_feeds.dart'; |
| 3 | + |
| 4 | +import '../../theme/theme.dart'; |
| 5 | +import 'attachment_gallery_chrome.dart'; |
| 6 | +import 'attachment_gallery_item.dart'; |
| 7 | +import 'attachment_metadata.dart'; |
| 8 | + |
| 9 | +class AttachmentGallery extends StatefulWidget { |
| 10 | + const AttachmentGallery({ |
| 11 | + super.key, |
| 12 | + this.initialIndex = 0, |
| 13 | + required this.attachments, |
| 14 | + required this.metadata, |
| 15 | + }) : assert(initialIndex >= 0, 'Initial index must be non-negative'); |
| 16 | + |
| 17 | + final int initialIndex; |
| 18 | + final List<Attachment> attachments; |
| 19 | + final AttachmentMetadata metadata; |
| 20 | + |
| 21 | + @override |
| 22 | + State<AttachmentGallery> createState() => _AttachmentGalleryState(); |
| 23 | +} |
| 24 | + |
| 25 | +class _AttachmentGalleryState extends State<AttachmentGallery> { |
| 26 | + late final PageController _pageController; |
| 27 | + late final _currentPage = ValueNotifier(widget.initialIndex); |
| 28 | + |
| 29 | + late final _isDisplayingDetail = ValueNotifier<bool>(true); |
| 30 | + void switchDisplayingDetail() { |
| 31 | + _isDisplayingDetail.value = !_isDisplayingDetail.value; |
| 32 | + } |
| 33 | + |
| 34 | + @override |
| 35 | + void initState() { |
| 36 | + super.initState(); |
| 37 | + _pageController = PageController(initialPage: _currentPage.value); |
| 38 | + } |
| 39 | + |
| 40 | + @override |
| 41 | + void dispose() { |
| 42 | + _pageController.dispose(); |
| 43 | + super.dispose(); |
| 44 | + } |
| 45 | + |
| 46 | + @override |
| 47 | + Widget build(BuildContext context) { |
| 48 | + return Scaffold( |
| 49 | + body: ValueListenableBuilder( |
| 50 | + valueListenable: _currentPage, |
| 51 | + builder: (context, currentPage, child) { |
| 52 | + return Stack( |
| 53 | + children: [ |
| 54 | + if (child case final child?) child, |
| 55 | + // Chrome overlay |
| 56 | + ValueListenableBuilder<bool>( |
| 57 | + valueListenable: _isDisplayingDetail, |
| 58 | + builder: (context, isVisible, child) { |
| 59 | + return AttachmentGalleryChrome( |
| 60 | + isVisible: isVisible, |
| 61 | + metadata: widget.metadata, |
| 62 | + currentAttachment: widget.attachments[currentPage], |
| 63 | + currentIndex: currentPage + 1, |
| 64 | + totalCount: widget.attachments.length, |
| 65 | + ); |
| 66 | + }, |
| 67 | + ), |
| 68 | + ], |
| 69 | + ); |
| 70 | + }, |
| 71 | + child: InkWell( |
| 72 | + onTap: switchDisplayingDetail, |
| 73 | + child: PageView.builder( |
| 74 | + controller: _pageController, |
| 75 | + itemCount: widget.attachments.length, |
| 76 | + onPageChanged: (page) => _currentPage.value = page, |
| 77 | + itemBuilder: (context, index) { |
| 78 | + final attachment = widget.attachments[index]; |
| 79 | + return ValueListenableBuilder( |
| 80 | + valueListenable: _isDisplayingDetail, |
| 81 | + builder: (context, displayingDetail, child) { |
| 82 | + final padding = MediaQuery.paddingOf(context); |
| 83 | + |
| 84 | + return AnimatedContainer( |
| 85 | + duration: kThemeAnimationDuration, |
| 86 | + color: switch (displayingDetail) { |
| 87 | + true => context.appColors.appBg, |
| 88 | + false => AppColorTokens.black, |
| 89 | + }, |
| 90 | + padding: EdgeInsetsDirectional.only( |
| 91 | + top: padding.top + kToolbarHeight, |
| 92 | + bottom: padding.bottom + kToolbarHeight, |
| 93 | + ), |
| 94 | + child: child, |
| 95 | + ); |
| 96 | + }, |
| 97 | + child: AttachmentGalleryItem(attachment: attachment), |
| 98 | + ); |
| 99 | + }, |
| 100 | + ), |
| 101 | + ), |
| 102 | + ), |
| 103 | + ); |
| 104 | + } |
| 105 | +} |
0 commit comments