From 988f19ece837806dc5c680e0644ecc57e1c8b01b Mon Sep 17 00:00:00 2001 From: Salvatore Giordano Date: Thu, 20 May 2021 16:31:36 +0200 Subject: [PATCH] fix file attachment in messageinput --- .../lib/src/attachment/file_attachment.dart | 112 ++++++++---------- .../lib/src/message_input.dart | 13 +- .../lib/src/video_service.dart | 4 +- 3 files changed, 59 insertions(+), 70 deletions(-) diff --git a/packages/stream_chat_flutter/lib/src/attachment/file_attachment.dart b/packages/stream_chat_flutter/lib/src/attachment/file_attachment.dart index 63bcdd9b..a676e576 100644 --- a/packages/stream_chat_flutter/lib/src/attachment/file_attachment.dart +++ b/packages/stream_chat_flutter/lib/src/attachment/file_attachment.dart @@ -159,6 +159,7 @@ class FileAttachment extends AttachmentWidget { shape: _getDefaultShape(context), child: source.when( local: () => VideoThumbnailImage( + fit: BoxFit.cover, video: attachment.file!.path!, placeholderBuilder: (_) => const Center( child: SizedBox( @@ -169,6 +170,7 @@ class FileAttachment extends AttachmentWidget { ), ), network: () => VideoThumbnailImage( + fit: BoxFit.cover, video: attachment.assetUrl!, placeholderBuilder: (_) => const Center( child: SizedBox( @@ -212,50 +214,42 @@ class FileAttachment extends AttachmentWidget { final attachmentId = attachment.id; var trailingWidget = trailing; trailingWidget ??= attachment.uploadState.when( - preparing: () => Padding( - padding: const EdgeInsets.all(8), - child: _buildButton( - icon: StreamSvgIcon.close(color: theme.colorTheme.white), - fillColor: theme.colorTheme.overlayDark, - onPressed: () => channel.cancelAttachmentUpload(attachmentId), - ), + preparing: () => Padding( + padding: const EdgeInsets.all(8), + child: _buildButton( + icon: StreamSvgIcon.close(color: theme.colorTheme.white), + fillColor: theme.colorTheme.overlayDark, + onPressed: () => channel.cancelAttachmentUpload(attachmentId), + ), + ), + inProgress: (_, __) => Padding( + padding: const EdgeInsets.all(8), + child: _buildButton( + icon: StreamSvgIcon.close(color: theme.colorTheme.white), + fillColor: theme.colorTheme.overlayDark, + onPressed: () => channel.cancelAttachmentUpload(attachmentId), + ), + ), + success: () => Padding( + padding: const EdgeInsets.all(8), + child: CircleAvatar( + backgroundColor: theme.colorTheme.accentBlue, + maxRadius: 12, + child: StreamSvgIcon.check(color: theme.colorTheme.white), + ), + ), + failed: (_) => Padding( + padding: const EdgeInsets.all(8), + child: _buildButton( + icon: StreamSvgIcon.retry(color: theme.colorTheme.white), + fillColor: theme.colorTheme.overlayDark, + onPressed: () => channel.retryAttachmentUpload( + message.id, + attachmentId, ), - inProgress: (_, __) => Padding( - padding: const EdgeInsets.all(8), - child: _buildButton( - icon: StreamSvgIcon.close(color: theme.colorTheme.white), - fillColor: theme.colorTheme.overlayDark, - onPressed: () => channel.cancelAttachmentUpload(attachmentId), - ), - ), - success: () => Padding( - padding: const EdgeInsets.all(8), - child: CircleAvatar( - backgroundColor: theme.colorTheme.accentBlue, - maxRadius: 12, - child: StreamSvgIcon.check(color: theme.colorTheme.white), - ), - ), - failed: (_) => Padding( - padding: const EdgeInsets.all(8), - child: _buildButton( - icon: StreamSvgIcon.retry(color: theme.colorTheme.white), - fillColor: theme.colorTheme.overlayDark, - onPressed: () => channel.retryAttachmentUpload( - message.id, - attachmentId, - ), - ), - ), - ) ?? - IconButton( - icon: StreamSvgIcon.cloudDownload(color: theme.colorTheme.black), - visualDensity: VisualDensity.compact, - splashRadius: 16, - onPressed: () { - launchURL(context, attachment.assetUrl); - }, - ); + ), + ), + ); if (message.status == MessageSendingStatus.sent) { trailingWidget = IconButton( @@ -281,25 +275,17 @@ class FileAttachment extends AttachmentWidget { color: theme.colorTheme.grey, ); return attachment.uploadState.when( - preparing: () => UploadProgressIndicator( - uploaded: 0, - total: double.maxFinite.toInt(), - showBackground: false, - padding: EdgeInsets.zero, - textStyle: textStyle, - progressIndicatorColor: theme.colorTheme.accentBlue, - ), - inProgress: (sent, total) => UploadProgressIndicator( - uploaded: sent, - total: total, - showBackground: false, - padding: EdgeInsets.zero, - textStyle: textStyle, - progressIndicatorColor: theme.colorTheme.accentBlue, - ), - success: () => Text(fileSize(size), style: textStyle), - failed: (_) => Text('UPLOAD ERROR', style: textStyle), - ) ?? - Text(fileSize(size), style: textStyle); + preparing: () => Text(fileSize(size), style: textStyle), + inProgress: (sent, total) => UploadProgressIndicator( + uploaded: sent, + total: total, + showBackground: false, + padding: EdgeInsets.zero, + textStyle: textStyle, + progressIndicatorColor: theme.colorTheme.accentBlue, + ), + success: () => Text(fileSize(size), style: textStyle), + failed: (_) => Text('UPLOAD ERROR', style: textStyle), + ); } } diff --git a/packages/stream_chat_flutter/lib/src/message_input.dart b/packages/stream_chat_flutter/lib/src/message_input.dart index 6d3ec5cf..db082c1c 100644 --- a/packages/stream_chat_flutter/lib/src/message_input.dart +++ b/packages/stream_chat_flutter/lib/src/message_input.dart @@ -1114,8 +1114,8 @@ class MessageInputState extends State { ); if (file.size! > widget.maxAttachmentSize) { - if (medium.type == AssetType.video) { - final mediaInfo = await (VideoService.compressVideo(file.path) + if (medium.type == AssetType.video && file.path != null) { + final mediaInfo = await (VideoService.compressVideo(file.path!) as FutureOr); if (mediaInfo.filesize! > widget.maxAttachmentSize) { @@ -1510,7 +1510,9 @@ class MessageInputState extends State { (e) => ClipRRect( borderRadius: BorderRadius.circular(10), child: FileAttachment( - message: Message(), // dummy message + message: Message( + status: MessageSendingStatus.sending, + ), // dummy message attachment: e, size: Size( MediaQuery.of(context).size.width * 0.65, @@ -1898,12 +1900,13 @@ class MessageInputState extends State { final attachment = Attachment( file: file, type: attachmentType, + uploadState: const UploadState.preparing(), extraData: extraDataMap, ); if (file.size! > widget.maxAttachmentSize) { - if (attachmentType == 'Video') { - final mediaInfo = await (VideoService.compressVideo(file.path) + if (attachmentType == 'video' && file.path != null) { + final mediaInfo = await (VideoService.compressVideo(file.path!) as FutureOr); if (mediaInfo.filesize! > widget.maxAttachmentSize) { diff --git a/packages/stream_chat_flutter/lib/src/video_service.dart b/packages/stream_chat_flutter/lib/src/video_service.dart index ba4761a2..290dd54b 100644 --- a/packages/stream_chat_flutter/lib/src/video_service.dart +++ b/packages/stream_chat_flutter/lib/src/video_service.dart @@ -28,9 +28,9 @@ class IVideoService { /// ); /// debugPrint(info.toJson()); /// ``` - Future compressVideo(String? path) async => _lock.synchronized( + Future compressVideo(String path) async => _lock.synchronized( () => VideoCompress.compressVideo( - path!, + path, ), );