From b0fab05ba24158f10388420d0e78f487056f5fa7 Mon Sep 17 00:00:00 2001 From: Hank Grabowski Date: Mon, 24 Apr 2023 08:19:43 -0400 Subject: [PATCH] Fix media viewer media width not taking up whole screen on zoom issue --- lib/main.dart | 2 +- lib/screens/media_viewer_screen.dart | 63 ++++++++++++++++++---------- 2 files changed, 42 insertions(+), 23 deletions(-) diff --git a/lib/main.dart b/lib/main.dart index dd60652..ef3f5a1 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -29,7 +29,7 @@ void main() async { WidgetsFlutterBinding.ensureInitialized(); MediaKit.ensureInitialized(); // await dotenv.load(fileName: '.env'); - const enablePreview = true; + const enablePreview = false; Logger.root.level = Level.FINER; Logger.root.onRecord.listen((event) { final logName = event.loggerName.isEmpty ? 'ROOT' : event.loggerName; diff --git a/lib/screens/media_viewer_screen.dart b/lib/screens/media_viewer_screen.dart index e69907c..aed0383 100644 --- a/lib/screens/media_viewer_screen.dart +++ b/lib/screens/media_viewer_screen.dart @@ -37,6 +37,25 @@ class _MediaViewerScreenState extends State { currentIndex = widget.initialIndex; } + void nextAttachment() { + if (currentIndex >= widget.attachments.length - 1) { + return; + } + + setState(() { + currentIndex++; + }); + } + + void previousAttachment() { + if (currentIndex < 1) { + return; + } + setState(() { + currentIndex--; + }); + } + Future saveImage( BuildContext context, MediaAttachment attachment, @@ -74,7 +93,11 @@ class _MediaViewerScreenState extends State { @override Widget build(BuildContext context) { final currentAttachment = widget.attachments[currentIndex]; - final height = MediaQuery.of(context).size.height; + final width = MediaQuery.of(context).size.width; + final height = MediaQuery.of(context).size.height * 0.9; + final descriptionHeightPct = widget.attachments.isEmpty ? 0.0 : 0.1; + final mediaHeight = height * (1 - descriptionHeightPct); + final descriptionHeight = height * descriptionHeightPct; return Scaffold( appBar: AppBar( actions: [ @@ -89,22 +112,23 @@ class _MediaViewerScreenState extends State { Column( mainAxisAlignment: MainAxisAlignment.start, children: [ - Expanded( + SizedBox( + width: width, + height: mediaHeight, child: InteractiveViewer( maxScale: 10.0, scaleFactor: 400, child: LoginAwareCachedNetworkImage( - imageUrl: - widget.attachments[currentIndex].uri.toString()), + imageUrl: currentAttachment.uri.toString()), ), ), if (currentAttachment.description.isNotEmpty) - buildTextArea(currentAttachment), + buildTextArea(currentAttachment, descriptionHeight), ], ), if (widget.attachments.length > 1) ...[ Positioned( - bottom: height * 0.5, + bottom: mediaHeight * 0.5, child: Opacity( opacity: 0.8, child: currentIndex < 1 @@ -113,18 +137,14 @@ class _MediaViewerScreenState extends State { color: Colors.black, child: IconButton( color: Colors.white, - onPressed: () { - setState(() { - currentIndex--; - }); - }, + onPressed: previousAttachment, icon: const Icon(Icons.chevron_left), ), ), ), ), Positioned( - bottom: height * 0.5, + bottom: mediaHeight * 0.5, right: 0.0, child: Opacity( opacity: 0.8, @@ -134,11 +154,7 @@ class _MediaViewerScreenState extends State { color: Colors.black, child: IconButton( color: Colors.white, - onPressed: () { - setState(() { - currentIndex++; - }); - }, + onPressed: nextAttachment, icon: const Icon(Icons.chevron_right), ), ), @@ -151,10 +167,9 @@ class _MediaViewerScreenState extends State { ); } - Widget buildTextArea(MediaAttachment attachment) { - final height = MediaQuery.of(context).size.height * 0.1; + Widget buildTextArea(MediaAttachment attachment, double height) { return Padding( - padding: const EdgeInsets.all(8.0), + padding: const EdgeInsets.only(left: 8.0, right: 8.0), child: SizedBox( height: height, child: attachment.description.isEmpty @@ -163,8 +178,12 @@ class _MediaViewerScreenState extends State { mainAxisAlignment: MainAxisAlignment.spaceBetween, children: [ Expanded( - child: SingleChildScrollView( - child: Text(attachment.description), + child: Scrollbar( + controller: ScrollController(), + thumbVisibility: true, + child: SingleChildScrollView( + child: Text(attachment.description), + ), )), IconButton( onPressed: () async {