feat: add flag to disable new/old message loading on scroll

This commit is contained in:
Joey Boerwinkel 2025-02-28 09:31:31 +01:00 committed by FlutterJoey
parent 371ff6c335
commit 5126b8dab7
3 changed files with 22 additions and 6 deletions

View file

@ -25,6 +25,7 @@
- Added autoScrollTriggerOffset to the ChatPaginationControls to adjust when the auto scroll should be enabled
- Added the ability to set the color of the CircularProgressIndicator of the ImageLoadingSnackbar by theme.snackBarTheme.actionTextColor
- Added semantics for variable text, buttons and textfields
- Added flag to enable/disable loading new and old messages on scrolling to the end of the current view.
## 4.0.0
- Move to the new user story architecture

View file

@ -302,6 +302,8 @@ class ChatPaginationControls {
this.autoScrollTriggerOffset = 50.0,
this.loadingIndicatorForNewMessages = true,
this.loadingIndicatorForOldMessages = true,
this.loadNewMessagesOnScroll = true,
this.loadOldMessagesOnScroll = true,
this.loadingNewMessageMinDuration = Duration.zero,
this.loadingOldMessageMinDuration = Duration.zero,
});
@ -314,6 +316,15 @@ class ChatPaginationControls {
/// chat. Defaults to 50.0
final double autoScrollTriggerOffset;
/// Whether to load new messages when someone scrolls to the end of the chat
final bool loadNewMessagesOnScroll;
/// Whether to load older messages when scrolling towards the start of the
/// chat.
///
/// If the messages are loaded by pagination, it is smart to add this.
final bool loadOldMessagesOnScroll;
/// Whether to show a loading indicator for new messages loading
final bool loadingIndicatorForNewMessages;

View file

@ -352,15 +352,19 @@ class _ChatBody extends HookWidget {
var distanceFromBottom = maxScroll - offset;
if (options.paginationControls.loadOldMessagesOnScroll) {
if (offset <= threshold && !isLoadingOlder.value) {
unawaited(loadOlderMessages());
}
}
if (options.paginationControls.loadNewMessagesOnScroll) {
if (distanceFromBottom <= threshold &&
!isLoadingNewer.value &&
!autoScrollEnabled.value) {
unawaited(loadNewerMessages());
}
}
if (distanceFromBottom > autoScrollThreshold) {
autoScrollEnabled.value = false;