From fc2da1391598759e3a50a6a45eb616240882926f Mon Sep 17 00:00:00 2001 From: Sahil Kumar Date: Mon, 4 Jan 2021 17:51:59 +0530 Subject: [PATCH] Handle onQuotedMessageTap Signed-off-by: Sahil Kumar --- lib/src/message_list_view.dart | 12 ++- lib/src/message_widget.dart | 172 +++++++++++++++++---------------- 2 files changed, 99 insertions(+), 85 deletions(-) diff --git a/lib/src/message_list_view.dart b/lib/src/message_list_view.dart index 7cd68eb3..169d9d45 100644 --- a/lib/src/message_list_view.dart +++ b/lib/src/message_list_view.dart @@ -181,7 +181,6 @@ class MessageListView extends StatefulWidget { class _MessageListViewState extends State { ItemScrollController _scrollController; bool _bottomWasVisible = false; - bool _topWasVisible = false; Function _onThreadTap; bool _showScrollToBottom = false; ItemPositionsListener _itemPositionListener; @@ -772,6 +771,17 @@ class _MessageListViewState extends State { bottom: index == 0 ? 30 : (isNextUser ? 2 : 7), top: 3, ), + onQuotedMessageTap: (quotedMessageId) { + if (messages.map((e) => e.id).contains(quotedMessageId)) { + final index = messages.indexWhere((m) => m.id == quotedMessageId); + _scrollController?.scrollTo( + index: index, + duration: const Duration(milliseconds: 350), + ); + } else { + streamChannel.loadChannelAtMessage(quotedMessageId); + } + }, showInChannelIndicator: widget.parentMessage == null, showThreadReplyIndicator: widget.parentMessage == null, showUsername: !isMyMessage && !isNextUser, diff --git a/lib/src/message_widget.dart b/lib/src/message_widget.dart index 1c49a9ca..3aa608ce 100644 --- a/lib/src/message_widget.dart +++ b/lib/src/message_widget.dart @@ -20,6 +20,7 @@ import 'message_text.dart'; import 'extension.dart'; typedef AttachmentBuilder = Widget Function(BuildContext, Message, Attachment); +typedef OnQuotedMessageTap = void Function(String); /// The display behaviour of a widget enum DisplayWidget { @@ -135,6 +136,9 @@ class MessageWidget extends StatefulWidget { /// Center user avatar with bottom of the message final bool translateUserAvatar; + /// Function called when quotedMessage is tapped + final OnQuotedMessageTap onQuotedMessageTap; + /// MessageWidget({ Key key, @@ -177,6 +181,7 @@ class MessageWidget extends StatefulWidget { ), this.attachmentPadding = EdgeInsets.zero, this.allRead = false, + this.onQuotedMessageTap, }) : attachmentBuilders = { 'image': (context, message, attachment) { return ImageAttachment( @@ -338,97 +343,81 @@ class _MessageWidgetState extends State { : 0, ) : EdgeInsets.zero, - child: - (widget.message.isDeleted && - !isFailedState) - ? Transform( - alignment: Alignment.center, - transform: Matrix4.rotationY( - widget.reverse ? pi : 0), - child: DeletedMessage( - reverse: widget.reverse, - borderRadiusGeometry: widget - .borderRadiusGeometry, - borderSide: - widget.borderSide, - shape: widget.shape, - messageTheme: - widget.messageTheme, - ), - ) - : 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), - child: Column( - crossAxisAlignment: - CrossAxisAlignment - .start, - mainAxisSize: - MainAxisSize.min, - children: [ - if (_hasQuotedMessage) - QuotedMessageWidget( - onTap: () {}, - message: widget - .message - .quotedMessage, - messageTheme: isMyMessage + child: (widget.message.isDeleted && + !isFailedState) + ? Transform( + alignment: Alignment.center, + transform: Matrix4.rotationY( + widget.reverse ? pi : 0), + child: DeletedMessage( + reverse: widget.reverse, + borderRadiusGeometry: + widget.borderRadiusGeometry, + borderSide: widget.borderSide, + shape: widget.shape, + messageTheme: + widget.messageTheme, + ), + ) + : 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) - .otherMessageTheme + .colorTheme + .white + .withAlpha( + 24) : StreamChatTheme.of( context) - .ownMessageTheme, - reverse: widget - .reverse, + .colorTheme + .black + .withAlpha( + 24), ), - ..._parseAttachments( - context), - if (widget - .message.text - .trim() - .isNotEmpty && - !isGiphy) - _buildTextBubble( - context), - ], - ), - ), + borderRadius: widget + .borderRadiusGeometry ?? + BorderRadius.zero, + ), + color: _getBackgroundColor(), + child: InkWell( + onLongPress: () => + onLongPress(context), + child: Padding( + padding: EdgeInsets.all( + hasFiles ? 2.0 : 0.0), + child: Column( + crossAxisAlignment: + CrossAxisAlignment + .start, + mainAxisSize: + MainAxisSize.min, + children: [ + if (_hasQuotedMessage) + _buildQuotedMessage( + isMyMessage), + ..._parseAttachments( + context), + if (widget.message.text + .trim() + .isNotEmpty && + !isGiphy) + _buildTextBubble( + context), + ], ), ), + ), + ), ), if (widget.showReactionPickerIndicator) Positioned( @@ -480,6 +469,21 @@ class _MessageWidgetState extends State { ); } + Widget _buildQuotedMessage(bool isMyMessage) { + return QuotedMessageWidget( + onTap: () { + if (widget.onQuotedMessageTap != null) { + widget.onQuotedMessageTap(widget.message.quotedMessageId); + } + }, + message: widget.message.quotedMessage, + messageTheme: isMyMessage + ? StreamChatTheme.of(context).otherMessageTheme + : StreamChatTheme.of(context).ownMessageTheme, + reverse: widget.reverse, + ); + } + Widget _buildBottomRow(double leftPadding) { final deleted = widget.message.isDeleted; var children = [];