From 33810de18f1abc808c483c91b16a791a22859798 Mon Sep 17 00:00:00 2001 From: Sahil Kumar Date: Mon, 4 Jan 2021 15:49:24 +0530 Subject: [PATCH] Refactoring, UI fixes Signed-off-by: Sahil Kumar --- lib/src/message_input.dart | 6 +- lib/src/message_widget.dart | 67 +++++++------ ...widget.dart => quoted_message_widget.dart} | 98 ++++++++++--------- 3 files changed, 90 insertions(+), 81 deletions(-) rename lib/src/{reply_message_widget.dart => quoted_message_widget.dart} (83%) diff --git a/lib/src/message_input.dart b/lib/src/message_input.dart index a24bda9d..72c5f70d 100644 --- a/lib/src/message_input.dart +++ b/lib/src/message_input.dart @@ -25,7 +25,7 @@ import 'package:video_compress/video_compress.dart'; import 'extension.dart'; import '../stream_chat_flutter.dart'; -import 'reply_message_widget.dart'; +import 'quoted_message_widget.dart'; import 'stream_channel.dart'; typedef FileUploader = Future Function(PlatformFile, Channel); @@ -2015,7 +2015,9 @@ class MessageInputState extends State { textEditingController.clear(); _attachments.clear(); - widget.onQuotedMessageCleared(); + if (widget.onQuotedMessageCleared != null) { + widget.onQuotedMessageCleared(); + } setState(() { _messageIsPresent = false; diff --git a/lib/src/message_widget.dart b/lib/src/message_widget.dart index c0fea794..1c49a9ca 100644 --- a/lib/src/message_widget.dart +++ b/lib/src/message_widget.dart @@ -11,7 +11,7 @@ import 'package:jiffy/jiffy.dart'; import 'package:stream_chat_flutter/src/message_actions_modal.dart'; import 'package:stream_chat_flutter/src/message_reactions_modal.dart'; import 'package:stream_chat_flutter/src/reaction_bubble.dart'; -import 'package:stream_chat_flutter/src/reply_message_widget.dart'; +import 'package:stream_chat_flutter/src/quoted_message_widget.dart'; import 'package:stream_chat_flutter/src/url_attachment.dart'; import 'package:stream_chat_flutter/stream_chat_flutter.dart'; @@ -356,37 +356,39 @@ class _MessageWidgetState extends State { widget.messageTheme, ), ) - : GestureDetector( - onLongPress: () => - onLongPress(context), - child: Material( - clipBehavior: - Clip.antiAlias, - shape: widget.shape ?? - RoundedRectangleBorder( - side: isOnlyEmoji - ? BorderSide.none - : widget.borderSide ?? - BorderSide( - color: Theme.of(context).brightness == - Brightness - .dark - ? StreamChatTheme.of(context) - .colorTheme - .white - .withAlpha( - 24) - : StreamChatTheme.of(context) - .colorTheme - .black - .withAlpha(24), - ), - borderRadius: widget - .borderRadiusGeometry ?? - BorderRadius.zero, - ), - color: - _getBackgroundColor(), + : Material( + clipBehavior: Clip.antiAlias, + shape: widget.shape ?? + RoundedRectangleBorder( + side: isOnlyEmoji + ? BorderSide.none + : widget.borderSide ?? + BorderSide( + color: Theme.of(context) + .brightness == + Brightness + .dark + ? StreamChatTheme.of( + context) + .colorTheme + .white + .withAlpha( + 24) + : StreamChatTheme.of( + context) + .colorTheme + .black + .withAlpha( + 24), + ), + borderRadius: widget + .borderRadiusGeometry ?? + BorderRadius.zero, + ), + color: _getBackgroundColor(), + child: InkWell( + onLongPress: () => + onLongPress(context), child: Padding( padding: EdgeInsets.all( hasFiles ? 2.0 : 0.0), @@ -399,6 +401,7 @@ class _MessageWidgetState extends State { children: [ if (_hasQuotedMessage) QuotedMessageWidget( + onTap: () {}, message: widget .message .quotedMessage, diff --git a/lib/src/reply_message_widget.dart b/lib/src/quoted_message_widget.dart similarity index 83% rename from lib/src/reply_message_widget.dart rename to lib/src/quoted_message_widget.dart index 40b78cfa..7c5dd512 100644 --- a/lib/src/reply_message_widget.dart +++ b/lib/src/quoted_message_widget.dart @@ -12,8 +12,9 @@ import 'message_text.dart'; import 'stream_chat_theme.dart'; import 'user_avatar.dart'; import 'utils.dart'; +import 'extension.dart'; -typedef ReplyMessageAttachmentThumbnailBuilder = Widget Function( +typedef QuotedMessageAttachmentThumbnailBuilder = Widget Function( BuildContext, Attachment, ); @@ -80,9 +81,11 @@ class QuotedMessageWidget extends StatelessWidget { final int textLimit; /// Map that defines a thumbnail builder for an attachment type - final Map + final Map attachmentThumbnailBuilders; + final GestureTapCallback onTap; + /// QuotedMessageWidget({ Key key, @@ -92,6 +95,7 @@ class QuotedMessageWidget extends StatelessWidget { this.showBorder = false, this.textLimit = 170, this.attachmentThumbnailBuilders, + this.onTap, }) : super(key: key); bool get _hasAttachments => message.attachments?.isNotEmpty == true; @@ -104,32 +108,54 @@ class QuotedMessageWidget extends StatelessWidget { @override Widget build(BuildContext context) { - return Padding( - padding: const EdgeInsets.only(top: 8, bottom: 6, right: 4, left: 8), - child: Row( - crossAxisAlignment: CrossAxisAlignment.end, - mainAxisSize: MainAxisSize.min, - children: [ - Flexible(child: _buildMessage(context)), - SizedBox(width: 4), - _buildUserAvatar(), - ], + return InkWell( + onTap: onTap, + child: Padding( + padding: const EdgeInsets.only(top: 8, bottom: 6, right: 4, left: 8), + child: Row( + crossAxisAlignment: CrossAxisAlignment.end, + mainAxisSize: MainAxisSize.min, + children: [ + Flexible(child: _buildMessage(context)), + SizedBox(width: 4), + _buildUserAvatar(), + ], + ), ), ); } Widget _buildMessage(BuildContext context) { + final isOnlyEmoji = + message.text.characters.every((c) => Emoji.byChar(c) != null); + var msg = _hasAttachments && !_containsText + ? message.copyWith(text: message.attachments.last?.title ?? '') + : message; + if (msg.text.length > textLimit) { + msg = msg.copyWith(text: '${msg.text.substring(0, textLimit - 3)}...'); + } + final children = [ - if (_hasAttachments) ...[ - _parseAttachments(context), - SizedBox(width: 8), - ], - Flexible(child: _buildTextMessage()), - ]; + if (_hasAttachments) _parseAttachments(context), + if (msg.text.isNotEmpty) + Flexible( + child: Transform( + transform: Matrix4.rotationY(reverse ? pi : 0), + alignment: Alignment.center, + child: MessageText( + message: msg, + messageTheme: isOnlyEmoji && _containsText + ? messageTheme.copyWith( + messageText: messageTheme.messageText.copyWith( + fontSize: 24, + )) + : messageTheme, + ), + ), + ), + ].insertBetween(const SizedBox(width: 8)); + return Container( - constraints: BoxConstraints( - minHeight: 48.0, - ), decoration: BoxDecoration( color: _getBackgroundColor(context), border: showBorder @@ -146,6 +172,7 @@ class QuotedMessageWidget extends StatelessWidget { padding: const EdgeInsets.all(8), child: Row( mainAxisSize: MainAxisSize.min, + crossAxisAlignment: CrossAxisAlignment.start, mainAxisAlignment: reverse ? MainAxisAlignment.end : MainAxisAlignment.start, children: reverse ? children.reversed.toList() : children, @@ -153,30 +180,6 @@ class QuotedMessageWidget extends StatelessWidget { ); } - Widget _buildTextMessage() { - final isOnlyEmoji = - message.text.characters.every((c) => Emoji.byChar(c) != null); - var msg = _hasAttachments && !_containsText - ? message.copyWith(text: message.attachments.last?.title ?? '') - : message; - if (msg.text.length > textLimit) { - msg = msg.copyWith(text: '${msg.text.substring(0, textLimit - 3)}...'); - } - return Transform( - transform: Matrix4.rotationY(reverse ? pi : 0), - alignment: Alignment.center, - child: MessageText( - message: msg, - messageTheme: isOnlyEmoji && _containsText - ? messageTheme.copyWith( - messageText: messageTheme.messageText.copyWith( - fontSize: 24, - )) - : messageTheme, - ), - ); - } - Widget _buildUrlAttachment(Attachment attachment) { final size = Size(32, 32); if (attachment.thumbUrl != null) { @@ -208,7 +211,7 @@ class QuotedMessageWidget extends StatelessWidget { ); child = _buildUrlAttachment(attachment); } else { - ReplyMessageAttachmentThumbnailBuilder attachmentBuilder; + QuotedMessageAttachmentThumbnailBuilder attachmentBuilder; attachment = message.attachments.last; if (attachmentThumbnailBuilders?.containsKey(attachment?.type) == true) { attachmentBuilder = attachmentThumbnailBuilders[attachment?.type]; @@ -219,6 +222,7 @@ class QuotedMessageWidget extends StatelessWidget { } child = attachmentBuilder(context, attachment); } + child = AbsorbPointer(child: child); return Transform( transform: Matrix4.rotationY(reverse ? pi : 0), alignment: Alignment.center, @@ -258,7 +262,7 @@ class QuotedMessageWidget extends StatelessWidget { ); } - Map + Map get _defaultAttachmentBuilder { return { 'image': (_, attachment) {