2.9 KiB
2.9 KiB
Chat View Scrolling Fix Summary
Problem Analysis
The chat view had inconsistent scroll thumb behavior where it would "jump around" during use. This was caused by:
- Aggressive auto-scrolling: The
_scrollToBottom()function was called on every build when messages were present - Interference with user scrolling: During message streaming, the chat provider calls
notifyListeners()frequently (on each text delta), triggering rebuilds and auto-scrolling - No user scroll detection: The system couldn't distinguish between user-initiated scrolling and auto-scrolling
Solution Implemented
1. Smart Auto-Scrolling Logic
- Only auto-scrolls when new messages arrive AND user is near the bottom (within 150px)
- Uses
_isNearBottom()to check scroll position - Tracks actual message content changes, not just rebuilds
2. User Scroll Detection
- Uses
ScrollControllerlistener to detect when user is scrolling - Implements 150ms debouncing to detect when scrolling stops
- Sets
_isUserScrollingflag to prevent auto-scrolling while user is interacting
3. Jump-to-Bottom Button
- When user scrolls away from bottom (>200px) and new messages arrive, shows a "New messages" button
- Button appears in bottom-right corner with subtle animation
- Clicking it smoothly scrolls to bottom and hides the button
- Button only shows when there are actually new messages while user is scrolled away
4. Message Change Tracking
- Tracks previous message contents to detect actual changes (not just re-renders)
- Prevents unnecessary auto-scrolling on provider updates that don't change message content
Technical Details
Key Variables
_isUserScrolling: Tracks if user is actively scrolling_showJumpToBottom: Whether to show the jump-to-bottom button_hasNewMessagesWhileScrolledAway: Whether new messages arrived while user was scrolled away_previousMessageContents: List of previous message contents for change detection
Scroll Thresholds
- Near bottom: Within 150px of bottom (triggers auto-scroll)
- Far from bottom: More than 200px from bottom (shows jump button)
- Debounce timeout: 150ms (detects scroll stop)
Benefits
- Smooth scrolling: No more jumpy scroll thumb during streaming
- User control: Users can scroll up to read previous messages without being forced back to bottom
- Clear UX: Jump-to-bottom button provides clear indication of new messages
- Performance: Reduces unnecessary scroll animations
Testing
To test the fix:
- Send multiple messages to create a scrollable chat
- Scroll up to read previous messages during streaming
- Observe that auto-scroll doesn't interfere
- See the jump-to-bottom button appear when new messages arrive
- Click the button to smoothly return to bottom
The fix maintains the original behavior for users who are at/near the bottom while preventing the disruptive scrolling behavior for users actively reading previous messages.