Skip to content

Commit

Permalink
#111: Fix scroll to an index is not working when row height is too large
Browse files Browse the repository at this point in the history
  • Loading branch information
jerrywell committed Jan 28, 2025
1 parent 6acd1ee commit cf154fb
Showing 1 changed file with 19 additions and 9 deletions.
28 changes: 19 additions & 9 deletions lib/scroll_to_index.dart
Original file line number Diff line number Diff line change
Expand Up @@ -378,15 +378,25 @@ mixin AutoScrollControllerMixin on ScrollController
} else {
final revealedOffset0 = _offsetToRevealInViewport(currentNearestIndex, 0);
final revealedOffset1 = _offsetToRevealInViewport(currentNearestIndex, 1);
// make it scroll to as short as possible
// since it's the farest possible one we can scroll to
double? offsetToLastState = revealedOffset0 == null
? revealedOffset1 == null
? null
: revealedOffset1.offset
: revealedOffset1 == null
? revealedOffset0.offset
: math.min(revealedOffset0!.offset, revealedOffset1!.offset);

double? offsetToLastState;
if (currentNearestIndex < targetIndex) {
// the current nearest index is less than the target
// we should scroll to the end of it in this round to
// make it as close to the target as possible.
offsetToLastState = revealedOffset1?.offset ?? revealedOffset0?.offset;
} else {
// make it scroll to as short as possible
// since it's the farest possible one we can scroll to
offsetToLastState = revealedOffset0 == null
? revealedOffset1 == null
? null
: revealedOffset1.offset
: revealedOffset1 == null
? revealedOffset0.offset
: math.min(revealedOffset0!.offset, revealedOffset1!.offset);
}

if (offsetToLastState == double.maxFinite) offsetToLastState = null;

absoluteOffsetToViewport = offsetToLastState;
Expand Down

0 comments on commit cf154fb

Please sign in to comment.