appendMessages method
Adds older messages to the beginning of the list, e.g. when loading
history from Chat.onScrollToTop.
Pass them newest first: each message is inserted before the current oldest one.
Implementation
void appendMessages(List<Message> messages) {
final groupCountBefore = _messageGroups.length;
for (final message in messages) {
if (message.isRead != null && message.isRead == false) {
_unreadMessageIds.add(message.id);
}
if (_messageGroups.isEmpty) {
_messageGroups.add(
MessageGroup(
name: message.name,
isSender: message.isSender,
timestamp: message.timestamp,
messages: [message],
),
);
continue;
}
if (_canJoinGroup(_messageGroups.first, message)) {
_messageGroups.first.messages.insert(0, message);
} else {
_messageGroups.insert(
0,
MessageGroup(
name: message.name,
isSender: message.isSender,
timestamp: message.timestamp,
messages: [message],
),
);
}
}
// In a normal list the older end is offset 0, so content added there pushes what the user is
// looking at downward. Chat shifts the offset by the added height in its next layout.
// (In a reversed list older messages are added at the far end and nothing moves.)
if (!_reverse && groupCountBefore > 0 && messages.isNotEmpty) {
_groupsPrepended = (_groupsPrepended ?? 0) + _messageGroups.length - groupCountBefore;
}
_notifyMessagesChanged();
}