Skip to content

Commit

Permalink
add slidableMessageBuilder/2 optional method (#385)
Browse files Browse the repository at this point in the history
* add slidableMessageBuilder/2 method

* _autoScrollIndexById 修改为 chatMessageAutoScrollIndexById,设置为全局变量,便于引用方复用(_autoScrollIndexById modified to chatMessageAutoScrollIndexById, set to global variables, easy reference to reuse)

* format

---------

Co-authored-by: leeyi <[email protected]>
  • Loading branch information
leeyisoft and leeyi authored Nov 3, 2023
1 parent d8aa2d1 commit 23c48eb
Showing 1 changed file with 21 additions and 13 deletions.
34 changes: 21 additions & 13 deletions lib/src/widgets/chat.dart
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ import 'state/inherited_user.dart';
import 'typing_indicator.dart';
import 'unread_header.dart';

/// Keep track of all the auto scroll indices by their respective message's id to allow animating to them.
final Map<String, int> chatMessageAutoScrollIndexById = {};

/// Entry widget, represents the complete chat. If you wrap it in [SafeArea] and
/// it should be full screen, set [SafeArea]'s `bottom` to `false`.
class Chat extends StatefulWidget {
Expand Down Expand Up @@ -97,6 +100,7 @@ class Chat extends StatefulWidget {
this.userAgent,
this.useTopSafeAreaInset,
this.videoMessageBuilder,
this.slidableMessageBuilder,
});

/// See [Message.audioMessageBuilder].
Expand Down Expand Up @@ -318,13 +322,17 @@ class Chat extends StatefulWidget {
final Widget Function(types.VideoMessage, {required int messageWidth})?
videoMessageBuilder;

/// See [Message.slidableMessageBuilder].
final Widget Function(types.Message, Widget msgWidget)?
slidableMessageBuilder;

@override
State<Chat> createState() => ChatState();
}

/// [Chat] widget state.
class ChatState extends State<Chat> {
/// Used to get the correct auto scroll index from [_autoScrollIndexById].
/// Used to get the correct auto scroll index from [chatMessageAutoScrollIndexById].
static const String _unreadHeaderId = 'unread_header_id';

List<Object> _chatMessages = [];
Expand All @@ -333,8 +341,6 @@ class ChatState extends State<Chat> {
bool _hadScrolledToUnreadOnOpen = false;
bool _isImageViewVisible = false;

/// Keep track of all the auto scroll indices by their respective message's id to allow animating to them.
final Map<String, int> _autoScrollIndexById = {};
late final AutoScrollController _scrollController;

@override
Expand All @@ -348,7 +354,7 @@ class ChatState extends State<Chat> {

/// Scroll to the unread header.
void scrollToUnreadHeader() {
final unreadHeaderIndex = _autoScrollIndexById[_unreadHeaderId];
final unreadHeaderIndex = chatMessageAutoScrollIndexById[_unreadHeaderId];
if (unreadHeaderIndex != null) {
_scrollController.scrollToIndex(
unreadHeaderIndex,
Expand All @@ -365,13 +371,13 @@ class ChatState extends State<Chat> {
Duration? highlightDuration,
}) async {
await _scrollController.scrollToIndex(
_autoScrollIndexById[id]!,
chatMessageAutoScrollIndexById[id]!,
duration: scrollDuration ?? scrollAnimationDuration,
preferPosition: AutoScrollPosition.middle,
);
if (withHighlight) {
await _scrollController.highlight(
_autoScrollIndexById[id]!,
chatMessageAutoScrollIndexById[id]!,
highlightDuration: highlightDuration ?? const Duration(seconds: 3),
);
}
Expand All @@ -380,7 +386,7 @@ class ChatState extends State<Chat> {
/// Highlight the message with the specified [id].
void highlightMessage(String id, {Duration? duration}) =>
_scrollController.highlight(
_autoScrollIndexById[id]!,
chatMessageAutoScrollIndexById[id]!,
highlightDuration: duration ?? const Duration(seconds: 3),
);

Expand Down Expand Up @@ -456,8 +462,7 @@ class ChatState extends State<Chat> {
widget.showUserAvatars && message.author.id != widget.user.id
? min(constraints.maxWidth * 0.72, 440).floor()
: min(constraints.maxWidth * 0.78, 440).floor();

messageWidget = Message(
final Widget msgWidget = Message(
audioMessageBuilder: widget.audioMessageBuilder,
avatarBuilder: widget.avatarBuilder,
bubbleBuilder: widget.bubbleBuilder,
Expand Down Expand Up @@ -499,6 +504,9 @@ class ChatState extends State<Chat> {
userAgent: widget.userAgent,
videoMessageBuilder: widget.videoMessageBuilder,
);
messageWidget = widget.slidableMessageBuilder == null
? msgWidget
: widget.slidableMessageBuilder!(message, msgWidget);
}

return AutoScrollTag(
Expand Down Expand Up @@ -536,16 +544,16 @@ class ChatState extends State<Chat> {
widget.onPreviewDataFetched?.call(message, previewData);
}

/// Updates the [_autoScrollIndexById] mapping with the latest messages.
/// Updates the [chatMessageAutoScrollIndexById] mapping with the latest messages.
void _refreshAutoScrollMapping() {
_autoScrollIndexById.clear();
chatMessageAutoScrollIndexById.clear();
var i = 0;
for (final object in _chatMessages) {
if (object is UnreadHeaderData) {
_autoScrollIndexById[_unreadHeaderId] = i;
chatMessageAutoScrollIndexById[_unreadHeaderId] = i;
} else if (object is Map<String, Object>) {
final message = object['message']! as types.Message;
_autoScrollIndexById[message.id] = i;
chatMessageAutoScrollIndexById[message.id] = i;
}
i++;
}
Expand Down

0 comments on commit 23c48eb

Please sign in to comment.