|
| 1 | +import 'package:flutter/material.dart'; |
| 2 | +import 'package:flutter/services.dart'; |
| 3 | +import 'package:intl/intl.dart'; |
| 4 | + |
| 5 | +import '../api/core.dart'; |
| 6 | +import '../api/model/model.dart'; |
| 7 | +import 'content.dart'; |
| 8 | +import 'store.dart'; |
| 9 | + |
| 10 | +class LightboxHero extends Hero { |
| 11 | + LightboxHero({ |
| 12 | + super.key, |
| 13 | + required Message message, |
| 14 | + required String src, |
| 15 | + required super.child |
| 16 | + }) : super( |
| 17 | + // TODO: Add index of the image preview in the message, to not break if |
| 18 | + // there are multiple image previews with the same URL in the same |
| 19 | + // message. Maybe keep `src`, so that on exit the lightbox image doesn't |
| 20 | + // fly to an image preview with a different URL, following a message edit |
| 21 | + // while the lightbox was open. |
| 22 | + tag: '${message.id.toString()} $src' |
| 23 | + ); |
| 24 | +} |
| 25 | + |
| 26 | +class _Page extends StatefulWidget { |
| 27 | + const _Page({ |
| 28 | + required this.routeEntranceAnimation, |
| 29 | + required this.message, |
| 30 | + required this.src |
| 31 | + }); |
| 32 | + |
| 33 | + final Animation routeEntranceAnimation; |
| 34 | + final Message message; |
| 35 | + final String src; |
| 36 | + |
| 37 | + @override |
| 38 | + State<_Page> createState() => _PageState(); |
| 39 | +} |
| 40 | + |
| 41 | +class _PageState extends State<_Page> { |
| 42 | + // TODO: Animate entrance/exit of header and footer |
| 43 | + bool _headerFooterVisible = false; |
| 44 | + |
| 45 | + void _handleRouteEntranceAnimationStatusChange(AnimationStatus status) { |
| 46 | + if (status == AnimationStatus.completed) { |
| 47 | + setState(() { |
| 48 | + _headerFooterVisible = true; |
| 49 | + }); |
| 50 | + } |
| 51 | + } |
| 52 | + |
| 53 | + void _handleTap() { |
| 54 | + setState(() { |
| 55 | + _headerFooterVisible = !_headerFooterVisible; |
| 56 | + }); |
| 57 | + } |
| 58 | + |
| 59 | + @override |
| 60 | + void initState() { |
| 61 | + super.initState(); |
| 62 | + widget.routeEntranceAnimation.addStatusListener(_handleRouteEntranceAnimationStatusChange); |
| 63 | + } |
| 64 | + |
| 65 | + @override |
| 66 | + void dispose() { |
| 67 | + widget.routeEntranceAnimation.removeStatusListener(_handleRouteEntranceAnimationStatusChange); |
| 68 | + super.dispose(); |
| 69 | + } |
| 70 | + |
| 71 | + @override |
| 72 | + Widget build(BuildContext context) { |
| 73 | + final store = PerAccountStoreWidget.of(context); |
| 74 | + |
| 75 | + final themeData = Theme.of(context); |
| 76 | + |
| 77 | + final appBarBackgroundColor = Colors.grey.shade900.withOpacity(0.87); |
| 78 | + const appBarForegroundColor = Colors.white; |
| 79 | + |
| 80 | + return Theme( |
| 81 | + data: themeData.copyWith( |
| 82 | + iconTheme: themeData.iconTheme.copyWith(color: appBarForegroundColor), |
| 83 | + ), |
| 84 | + child: Scaffold( |
| 85 | + backgroundColor: Colors.black, |
| 86 | + extendBody: true, // For the BottomAppBar |
| 87 | + extendBodyBehindAppBar: true, // For the AppBar |
| 88 | + appBar: _headerFooterVisible |
| 89 | + ? AppBar( |
| 90 | + // TODO: Show user's avatar |
| 91 | + title: RichText( |
| 92 | + text: TextSpan( |
| 93 | + children: [ |
| 94 | + TextSpan( |
| 95 | + text: '${widget.message.sender_full_name}\n', |
| 96 | + |
| 97 | + // Restate default |
| 98 | + style: themeData.textTheme.titleLarge!.copyWith(color: appBarForegroundColor), |
| 99 | + ), |
| 100 | + TextSpan( |
| 101 | + // TODO: Format with e.g. "Yesterday at 4:47 PM" |
| 102 | + text: DateFormat |
| 103 | + .yMMMd(/* TODO(i18n): Pass selected language here, I think? */) |
| 104 | + .add_Hms() |
| 105 | + .format(DateTime.fromMillisecondsSinceEpoch(widget.message.timestamp * 1000)), |
| 106 | + |
| 107 | + // Make smaller, like a subtitle |
| 108 | + style: themeData.textTheme.titleSmall!.copyWith(color: appBarForegroundColor)), |
| 109 | + ] |
| 110 | + )), |
| 111 | + centerTitle: false, |
| 112 | + foregroundColor: appBarForegroundColor, |
| 113 | + backgroundColor: appBarBackgroundColor, |
| 114 | + |
| 115 | + // With Material 3, later, try removing these "transparent" overrides |
| 116 | + shadowColor: Colors.transparent, |
| 117 | + surfaceTintColor: Colors.transparent, |
| 118 | + |
| 119 | + // TODO: Author's avatar, name; date of message |
| 120 | + ) |
| 121 | + : null, |
| 122 | + body: MediaQuery( |
| 123 | + // Declare the vertical insets unconsumed by `appBar` and |
| 124 | + // `bottomNavigationBar`, so we can position the image to not overlap |
| 125 | + // any system intrusions, at least until the user zooms in. The top |
| 126 | + // and bottom app bars overlay the pan-zoom layer in the Z direction; |
| 127 | + // they don't affect anything in the pan-zoom layer's Y direction. |
| 128 | + // |
| 129 | + // Done by clobbering the ambient MediaQueryData (prepared by Scaffold |
| 130 | + // for the `body`) with one taken from a BuildContext above the |
| 131 | + // Scaffold. |
| 132 | + data: MediaQuery.of(context), |
| 133 | + |
| 134 | + child: GestureDetector( |
| 135 | + behavior: HitTestBehavior.translucent, |
| 136 | + onTap: _handleTap, |
| 137 | + child: SizedBox.expand( |
| 138 | + child: InteractiveViewer( |
| 139 | + child: SafeArea( |
| 140 | + child: LightboxHero( |
| 141 | + message: widget.message, |
| 142 | + src: widget.src, |
| 143 | + child: |
| 144 | + Image.network( |
| 145 | + widget.src, |
| 146 | + filterQuality: FilterQuality.medium, |
| 147 | + headers: isUrlOnRealm(widget.src, store.account) |
| 148 | + ? Map.fromEntries([authHeader(store.account)]) |
| 149 | + : null))))))), |
| 150 | + bottomNavigationBar: _headerFooterVisible |
| 151 | + ? BottomAppBar( |
| 152 | + color: appBarBackgroundColor, |
| 153 | + |
| 154 | + // With Material 3, later, try removing these "transparent" overrides |
| 155 | + shadowColor: Colors.transparent, |
| 156 | + surfaceTintColor: Colors.transparent, |
| 157 | + |
| 158 | + child: Row( |
| 159 | + children: [ |
| 160 | + IconButton( |
| 161 | + tooltip: 'Copy link', |
| 162 | + icon: const Icon(Icons.copy), |
| 163 | + onPressed: () async { |
| 164 | + await Clipboard.setData(ClipboardData(text: widget.src)); |
| 165 | + if (!context.mounted) return; // ignore: use_build_context_synchronously |
| 166 | + ScaffoldMessenger.of(context).showSnackBar( |
| 167 | + const SnackBar( |
| 168 | + behavior: SnackBarBehavior.floating, |
| 169 | + content: Text('Link copied'))); |
| 170 | + }), |
| 171 | + // TODO: Share image |
| 172 | + // TODO: Download image |
| 173 | + ], |
| 174 | + )) |
| 175 | + : null)); |
| 176 | + } |
| 177 | +} |
| 178 | + |
| 179 | +Route getLightboxRoute({required Message message, required String src}) { |
| 180 | + return PageRouteBuilder( |
| 181 | + fullscreenDialog: true, |
| 182 | + pageBuilder: ( |
| 183 | + BuildContext context, |
| 184 | + Animation<double> animation, |
| 185 | + Animation<double> secondaryAnimation |
| 186 | + ) { |
| 187 | + // TODO: Drag down to close? |
| 188 | + return _Page(routeEntranceAnimation: animation, message: message, src: src); |
| 189 | + }, |
| 190 | + transitionsBuilder: ( |
| 191 | + BuildContext context, |
| 192 | + Animation<double> animation, |
| 193 | + Animation<double> secondaryAnimation, |
| 194 | + Widget child |
| 195 | + ) { |
| 196 | + return FadeTransition( |
| 197 | + opacity: animation.drive(CurveTween(curve: Curves.easeIn)), |
| 198 | + child: child, |
| 199 | + ); |
| 200 | + }, |
| 201 | + ); |
| 202 | +} |
0 commit comments