Skip to content

Commit 2369637

Browse files
feat: 🌟 handle prev and next message
1 parent 311d647 commit 2369637

File tree

2 files changed

+42
-3
lines changed

2 files changed

+42
-3
lines changed

lib/src/models/chat_bubble_configuration.dart

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,12 @@ class ChatBubbleConfiguration {
3535
/// Used for giving maximum width of chat bubble.
3636
final double? maxWidth;
3737

38+
/// Used for insetting the messages from the bottom according to text field height.
39+
final double? additionalBottomInsetIfMessageIsLast;
40+
41+
/// Used for insetting the messages from the bottom more if the user is about to reply to another message.
42+
final double? additionalBottomInsetIfReplyHeaderIsThere;
43+
3844
/// Provides callback when user long press on chat bubble.
3945
final Duration? longPressAnimationDuration;
4046

@@ -53,6 +59,8 @@ class ChatBubbleConfiguration {
5359
this.padding,
5460
this.margin,
5561
this.maxWidth,
62+
this.additionalBottomInsetIfMessageIsLast,
63+
this.additionalBottomInsetIfReplyHeaderIsThere,
5664
this.longPressAnimationDuration,
5765
this.inComingChatBubbleConfig,
5866
this.outgoingChatBubbleConfig,

lib/src/widgets/chat_bubble_widget.dart

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ class ChatBubbleWidget extends StatefulWidget {
3838
required this.onLongPress,
3939
required this.slideValue,
4040
required this.onInitReplyToMessage,
41+
this.previousMessage,
42+
this.nextMessage,
4143
this.profileCircleConfig,
4244
this.chatBubbleConfig,
4345
this.repliedMessageConfig,
@@ -52,6 +54,12 @@ class ChatBubbleWidget extends StatefulWidget {
5254
/// Represent current instance of message.
5355
final Message message;
5456

57+
/// Represent message sent before that one.
58+
final Message? previousMessage;
59+
60+
/// Represent message sent after that one.
61+
final Message? nextMessage;
62+
5563
/// Give callback once user long press on chat bubble.
5664
final DoubleCallBack onLongPress;
5765

@@ -101,6 +109,12 @@ class _ChatBubbleWidgetState extends State<ChatBubbleWidget> {
101109

102110
bool get isMessageBySender => widget.message.sendBy == currentUser?.id;
103111

112+
bool get isMessageBySameSenderAsPrevious =>
113+
widget.previousMessage?.sendBy == widget.message.sendBy;
114+
115+
bool get isMessageLastFromSender =>
116+
widget.nextMessage?.sendBy == widget.message.sendBy;
117+
104118
bool get isLastMessage =>
105119
chatController?.initialMessageList.last.id == widget.message.id;
106120

@@ -163,14 +177,22 @@ class _ChatBubbleWidgetState extends State<ChatBubbleWidget> {
163177
padding:
164178
widget.chatBubbleConfig?.padding ?? const EdgeInsets.only(left: 5.0),
165179
margin:
166-
widget.chatBubbleConfig?.margin ?? const EdgeInsets.only(bottom: 10),
180+
(widget.chatBubbleConfig?.margin ?? const EdgeInsets.only(bottom: 10))
181+
.add(
182+
isLastMessage
183+
? EdgeInsets.only(
184+
bottom: widget.chatBubbleConfig
185+
?.additionalBottomInsetIfMessageIsLast ??
186+
16)
187+
: const EdgeInsets.all(0),
188+
),
167189
child: Row(
168190
mainAxisSize: MainAxisSize.min,
169191
mainAxisAlignment:
170192
isMessageBySender ? MainAxisAlignment.end : MainAxisAlignment.start,
171193
crossAxisAlignment: CrossAxisAlignment.end,
172194
children: [
173-
if (!isMessageBySender &&
195+
if (!(isMessageBySender || isMessageLastFromSender) &&
174196
(featureActiveConfig?.enableOtherUserProfileAvatar ?? true))
175197
ProfileCircle(
176198
bottomPadding: widget.message.reaction.reactions.isNotEmpty
@@ -188,6 +210,14 @@ class _ChatBubbleWidgetState extends State<ChatBubbleWidget> {
188210
circleRadius: profileCircleConfig?.circleRadius,
189211
onTap: () => _onAvatarTap(messagedUser),
190212
onLongPress: () => _onAvatarLongPress(messagedUser),
213+
)
214+
else
215+
SizedBox(
216+
width: profileCircleConfig == null
217+
? 10
218+
: (profileCircleConfig!.circleRadius * 2 +
219+
(profileCircleConfig?.padding?.collapsedSize.width ??
220+
12)),
191221
),
192222
Expanded(
193223
child: SwipeToReply(
@@ -295,7 +325,8 @@ class _ChatBubbleWidgetState extends State<ChatBubbleWidget> {
295325
crossAxisAlignment:
296326
isMessageBySender ? CrossAxisAlignment.end : CrossAxisAlignment.start,
297327
children: [
298-
if ((chatController?.chatUsers.length ?? 0) > 1 &&
328+
if (!isMessageBySameSenderAsPrevious &&
329+
(chatController?.chatUsers.length ?? 0) > 1 &&
299330
!isMessageBySender &&
300331
(featureActiveConfig?.enableOtherUserName ?? true))
301332
Padding(

0 commit comments

Comments
 (0)