From 092fed6fddae6e923a1327b8afd4a0328aa1ff7f Mon Sep 17 00:00:00 2001 From: Salvatore Giordano Date: Fri, 10 Apr 2020 11:00:55 +0200 Subject: [PATCH] refs #21: fix video aspect ratio --- lib/src/message_widget.dart | 83 ++++++++++++++++++++++--------------- 1 file changed, 49 insertions(+), 34 deletions(-) diff --git a/lib/src/message_widget.dart b/lib/src/message_widget.dart index 62c41148..43104bd6 100644 --- a/lib/src/message_widget.dart +++ b/lib/src/message_widget.dart @@ -39,6 +39,7 @@ class MessageWidget extends StatefulWidget { this.isParent = false, this.onMentionTap, this.showOtherMessageUsername = false, + this.showVideoFullScreen = true, }) : super(key: key); /// Function called on mention tap @@ -65,6 +66,9 @@ class MessageWidget extends StatefulWidget { /// True if this is the parent of the thread being showed final bool isParent; + /// True if the video player will allow fullscreen mode + final bool showVideoFullScreen; + @override _MessageWidgetState createState() => _MessageWidgetState(); } @@ -944,42 +948,53 @@ class _MessageWidgetState extends State _videoControllers[attachment.assetUrl] = videoController; } - ChewieController chewieController; - if (_chuwieControllers.containsKey(attachment.assetUrl)) { - chewieController = _chuwieControllers[attachment.assetUrl]; - } else { - chewieController = ChewieController( - videoPlayerController: videoController, - autoInitialize: true, - errorBuilder: (_, e) { - return Stack( - children: [ - Container( - decoration: BoxDecoration( - image: DecorationImage( - fit: BoxFit.cover, - image: CachedNetworkImageProvider( - attachment.thumbUrl, + return FutureBuilder( + future: videoController.initialize(), + builder: (_, snapshot) { + if (snapshot.connectionState != ConnectionState.done) { + return Center( + child: CircularProgressIndicator(), + ); + } + ChewieController chewieController; + if (_chuwieControllers.containsKey(attachment.assetUrl)) { + chewieController = _chuwieControllers[attachment.assetUrl]; + } else { + chewieController = ChewieController( + allowFullScreen: widget.showVideoFullScreen, + videoPlayerController: videoController, + autoInitialize: false, + aspectRatio: videoController.value.aspectRatio, + errorBuilder: (_, e) { + return Stack( + children: [ + Container( + decoration: BoxDecoration( + image: DecorationImage( + fit: BoxFit.cover, + image: CachedNetworkImageProvider( + attachment.thumbUrl, + ), + ), ), ), - ), - ), - Material( - color: Colors.transparent, - child: InkWell( - onTap: () => _launchURL(attachment.titleLink), - ), - ), - ], - ); - }); - _chuwieControllers[attachment.assetUrl] = chewieController; - } - - return Chewie( - key: ValueKey( - 'ATTACHMENT-${attachment.title}-${widget.message.id}'), - controller: chewieController, + Material( + color: Colors.transparent, + child: InkWell( + onTap: () => _launchURL(attachment.titleLink), + ), + ), + ], + ); + }); + _chuwieControllers[attachment.assetUrl] = chewieController; + } + return Chewie( + key: ValueKey( + 'ATTACHMENT-${attachment.title}-${widget.message.id}'), + controller: chewieController, + ); + }, ); }