From 3f4f07450bc7f5167e870c576c9af9170f8082c7 Mon Sep 17 00:00:00 2001 From: "Stephen (Alex) Wallen" Date: Tue, 29 Aug 2023 18:17:29 -1000 Subject: [PATCH] feat: allow overriding curve --- lib/scroll_to_index.dart | 33 ++++++++++++++++++++------------- 1 file changed, 20 insertions(+), 13 deletions(-) diff --git a/lib/scroll_to_index.dart b/lib/scroll_to_index.dart index 7b37c6b..81ca49e 100644 --- a/lib/scroll_to_index.dart +++ b/lib/scroll_to_index.dart @@ -210,9 +210,12 @@ mixin AutoScrollControllerMixin on ScrollController duration: duration, preferPosition: preferPosition)); } - Future _scrollToIndex(int index, - {Duration duration: scrollAnimationDuration, - AutoScrollPosition? preferPosition}) async { + Future _scrollToIndex( + int index, { + Duration duration: scrollAnimationDuration, + AutoScrollPosition? preferPosition, + Curve curve = Curves.ease, + }) async { assert(duration > Duration.zero); // In listView init or reload case, widget state of list item may not be ready for query. @@ -240,7 +243,7 @@ mixin AutoScrollControllerMixin on ScrollController await _bringIntoViewportIfNeed(index, preferPosition, (double offset) async { - await animateTo(offset, duration: duration, curve: Curves.ease); + await animateTo(offset, duration: duration, curve: curve); await _waitForWidgetStateBuild(); return null; }); @@ -268,12 +271,12 @@ mixin AutoScrollControllerMixin on ScrollController prevOffset = currentOffset; final nearest = _getNearestIndex(index); - if (tagMap[nearest ?? 0] == null) + if (tagMap[nearest ?? 0] == null) return null; final moveTarget = _forecastMoveUnit(index, nearest, usedSuggestedRowHeightIfAny)!; - + // assume suggestRowHeight will move to correct offset in just one time. // if the rule doesn't work (in variable row height case), we will use backup solution (non-suggested way) final suggestedDuration = @@ -286,7 +289,7 @@ mixin AutoScrollControllerMixin on ScrollController spentDuration += suggestedDuration ?? moveDuration; final oldOffset = offset; await animateTo(currentOffset, - duration: suggestedDuration ?? moveDuration, curve: Curves.ease); + duration: suggestedDuration ?? moveDuration, curve: curve); await _waitForWidgetStateBuild(); if (!hasClients || offset == oldOffset) { // already scroll to begin or end @@ -303,9 +306,11 @@ mixin AutoScrollControllerMixin on ScrollController if (finalOffset != offset) { _isAutoScrolling = true; final remaining = duration - spentDuration; - await animateTo(finalOffset, - duration: remaining <= Duration.zero ? _millisecond : remaining, - curve: Curves.ease); + await animateTo( + finalOffset, + duration: remaining <= Duration.zero ? _millisecond : remaining, + curve: curve, + ); await _waitForWidgetStateBuild(); // not sure why it doesn't scroll to the given offset, try more within 3 times @@ -315,7 +320,7 @@ mixin AutoScrollControllerMixin on ScrollController i < count && hasClients && offset != finalOffset; i++) { await animateTo(finalOffset, - duration: _millisecond, curve: Curves.ease); + duration: _millisecond, curve: curve); await _waitForWidgetStateBuild(); } } @@ -379,7 +384,7 @@ mixin AutoScrollControllerMixin on ScrollController } else { final offsetToLastState = _offsetToRevealInViewport(currentNearestIndex, alignment); - + absoluteOffsetToViewport = offsetToLastState?.offset; if (absoluteOffsetToViewport == null) absoluteOffsetToViewport = defaultScrollDistanceOffset; @@ -646,7 +651,9 @@ class AutoScrollTagState extends State } } -Widget buildHighlightTransition({required BuildContext context, required Animation highlight, +Widget buildHighlightTransition( + {required BuildContext context, + required Animation highlight, required Widget child, Color? background, Color? highlightColor}) { return DecoratedBoxTransition( decoration: DecorationTween(