ChatController class

Holds the messages of a Chat and controls its scroll position.

Messages are kept as a list of MessageGroups in chronological order. Use setMessages to load history, appendMessages to prepend older messages, addMessage for incoming messages and dispatchMessage to send a message with loading, failure and retry handling.

Since it extends ScrollController, it can also be used to read or change the scroll position.

Inheritance

Constructors

ChatController({bool reverse = false, CustomSortCallback? customSortCallback, AdditionalGroupCondition? additionalGroupCondition})
Creates a ChatController.

Properties

debugLabel → String?
A label that is used in the toString output. Intended to aid with identifying scroll controller instances in debug output.
finalinherited
dispatchResultStream → Stream<MessageDispatchResult>
Emits the result of every dispatchMessage and retryMessage call.
no setter
hasClients → bool
Whether any ScrollPosition objects have attached themselves to the ScrollController using the attach method.
no setterinherited
hashCode → int
The hash code for this object.
no setterinherited
hasListeners → bool
Whether any listeners are currently registered.
no setterinherited
initialScrollOffset → double
The initial value to use for offset.
no setterinherited
isCollapsed → bool
A flag you can use to track whether the chat is collapsed.
no setter
isDisposed → bool
Whether dispose has been called.
no setter
keepScrollOffset → bool
Each time a scroll completes, save the current scroll offset with PageStorage and restore it if this controller's scrollable is recreated.
finalinherited
messageGroups → List<MessageGroup>
The current message groups, oldest first.
no setter
messagesRevision → ValueListenable<int>
Incremented only when the message list changes. ScrollController calls notifyListeners on every scroll offset change, so the list view should listen to this instead of the controller; otherwise the whole list is rebuilt on every scroll frame.
no setter
newReceiveMessageStream → Stream<Message>
Emits receiver messages returned by a dispatch callback.
no setter
offset → double
The current scroll offset of the scrollable widget.
no setterinherited
onAttach → ScrollControllerCallback?
Called when a ScrollPosition is attached to the scroll controller.
finalinherited
onDetach → ScrollControllerCallback?
Called when a ScrollPosition is detached from the scroll controller.
finalinherited
position → ScrollPosition
Returns the attached ScrollPosition, from which the actual scroll offset of the ScrollView can be obtained.
no setterinherited
positions → Iterable<ScrollPosition>
The currently attached positions.
no setterinherited
reverse → bool
Whether the list is laid out in reverse, with the newest messages at scroll offset 0.
no setter
runtimeType → Type
A representation of the runtime type of the object.
no setterinherited

Methods

addListener(VoidCallback listener) → void
Register a closure to be called when the object changes.
inherited
addMessage(Message message) → MessageGroup
Adds message to its group. A message whose id already exists is not added again. Adds message after the newest message and returns its group.
animateTo(double offset, {required Duration duration, required Curve curve}) → Future<void>
Animates the position from its current value to the given value.
inherited
appendMessages(List<Message> messages) → void
Adds older messages to the beginning of the list, e.g. when loading history from Chat.onScrollToTop.
attach(ScrollPosition position) → void
Register the given position with this controller.
inherited
containsMessageId(String messageId) → bool
Whether a message with messageId is in the list.
createScrollPosition(ScrollPhysics physics, ScrollContext context, ScrollPosition? oldPosition) → ScrollPosition
Creates a ScrollPosition for use by a Scrollable widget.
inherited
debugFillDescription(List<String> description) → void
Add additional information to the given description for use by toString.
inherited
defaultCanJoinGroup(MessageGroup group, Message message) → bool
Whether message can join group by default: same side (Message.isSender), same Message.name and same minute.
detach(ScrollPosition position) → void
Unregister the given position with this controller.
inherited
dispatchMessage(Message message, {required MessageDispatchCallback onDispatch}) → Future<MessageDispatchResult>
Sends message through onDispatch and tracks its state in the list.
dispose() → void
Discards any resources used by the object.
override
isAtBottom({double threshold = 20}) → bool
Whether the list is scrolled within threshold logical pixels of the newest message. Returns true when not attached to a scroll view.
isDateChangedComparedTo(Message message, Message other) → bool
Whether message and other were sent on different calendar days.
isSameMinute(DateTime a, DateTime b) → bool
Whether a and b fall within the same minute.
jumpTo(double value) → void
Jumps the scroll position from its current value to the given value, without animation, and without checking if the new value is in range.
inherited
noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
notifyListeners() → void
Call all the registered listeners.
override
registerFailedMessage(String messageId, MessageDispatchCallback onDispatch) → void
Makes an already loaded failed message with messageId retryable through onDispatch, e.g. a failed message restored from local storage.
removeListener(VoidCallback listener) → void
Remove a previously registered closure from the list of closures that are notified when the object changes.
inherited
removeMessageEverywhere(Message message) → void
Removes every message with the same Message.id as message.
removeMessageFromGroup(MessageGroup group, Message message) → void
Removes message from group, and the group itself if it becomes empty.
removeMessageGroupAt(MessageGroup group, int messageIndex) → void
Removes the message at messageIndex in group, and the group itself if it becomes empty.
retryMessage(Message message) → Future
Sends a failed message again with the callback it was originally dispatched with.
scrollToBottom() → void
Jumps to the newest message.
scrollToBottomByValue(double offset) → void
Scrolls toward newer messages by offset. Offset direction is the opposite of scrollToTopByValue.
scrollToTopByValue(double offset) → void
Scrolls toward older messages by offset.
setIsCollapsed(bool value) → void
Sets isCollapsed and notifies listeners.
setMessages(List<Message> messages) → void
Replaces all messages with messages.
takeAppearAnimation(String messageId) → bool
Whether the message with messageId was just added and should play its entrance animation. Returns true at most once per added message.
takeGroupsPrepended() → int?
When older messages were just added by appendMessages to a normal (not reversed) list, the number of groups added in front, so the visible groups can be kept in place; otherwise null. Returns the value once.
toString() → String
A string representation of this object.
inherited
updateMessageAllRead() → void
Marks every unread message (Message.isRead is false) as read.

Operators

operator ==(Object other) → bool
The equality operator.
inherited