Skip to content

Commit e97d5c1

Browse files
authored
fix: scroll to bottom button appearance threshold #769 (#774)
* Factorize _isAtChatEndScrollPosition * Add offset to show the scroll to bottom button
1 parent a817ad8 commit e97d5c1

1 file changed

Lines changed: 17 additions & 4 deletions

File tree

packages/flutter_chat_ui/lib/src/chat_animated_list/chat_animated_list.dart

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,9 @@ class ChatAnimatedList extends StatefulWidget {
6161
/// Delay before the scroll-to-bottom button appears after scrolling up.
6262
final Duration scrollToBottomAppearanceDelay;
6363

64+
/// Threshold for triggering scroll-to-bottom button appearance.
65+
final double scrollToBottomAppearanceThreshold;
66+
6467
/// Padding added above the first item in the list.
6568
final double? topPadding;
6669

@@ -114,6 +117,7 @@ class ChatAnimatedList extends StatefulWidget {
114117
this.removeAnimationDurationResolver,
115118
this.scrollToEndAnimationDuration = const Duration(milliseconds: 250),
116119
this.scrollToBottomAppearanceDelay = const Duration(milliseconds: 250),
120+
this.scrollToBottomAppearanceThreshold = 0,
117121
this.topPadding = 8,
118122
this.bottomPadding = 20,
119123
this.topSliver,
@@ -304,9 +308,8 @@ class _ChatAnimatedListState extends State<ChatAnimatedList>
304308
/// For a normal list, this means the scroll offset is at or beyond `maxScrollExtent`.
305309
bool get _isAtChatEndScrollPosition {
306310
return widget.reversed
307-
? _scrollController.offset <= 0
308-
: _scrollController.offset >=
309-
_scrollController.position.maxScrollExtent;
311+
? _scrollController.offset <= _chatEndScrollPosition
312+
: _scrollController.offset >= _chatEndScrollPosition;
310313
}
311314

312315
/// The scroll position that represents the end of the chat list.
@@ -316,6 +319,16 @@ class _ChatAnimatedListState extends State<ChatAnimatedList>
316319
return widget.reversed ? 0 : _scrollController.position.maxScrollExtent;
317320
}
318321

322+
/// If the scroll-to-bottom button should be shown.
323+
bool get _shouldShowScrollToBottomButton {
324+
final scrollOffsetFromBottom =
325+
widget.reversed
326+
? _scrollController.offset
327+
: _chatEndScrollPosition - _scrollController.offset;
328+
329+
return scrollOffsetFromBottom > widget.scrollToBottomAppearanceThreshold;
330+
}
331+
319332
@override
320333
Widget build(BuildContext context) {
321334
final builders = context.read<Builders>();
@@ -651,7 +664,7 @@ class _ChatAnimatedListState extends State<ChatAnimatedList>
651664
void _handleToggleScrollToBottom() {
652665
if (!_isScrollingToBottom) {
653666
_scrollToBottomShowTimer?.cancel();
654-
if (!_isAtChatEndScrollPosition) {
667+
if (_shouldShowScrollToBottomButton) {
655668
_scrollToBottomShowTimer = Timer(
656669
widget.scrollToBottomAppearanceDelay,
657670
() {

0 commit comments

Comments
 (0)