scrollTo method
Animate the list over duration
using the given curve
such that the
item at index
ends up with its leading edge at the given alignment
.
See jumpTo for an explanation of alignment.
The duration
must be greater than 0; otherwise, use jumpTo.
When item position is not available, because it's too far, the scroll is composed into three phases:
- The currently displayed list view starts scrolling.
- Another list view, which scrolls with the same speed, fades over the first one and shows items that are close to the scroll target.
- The second list view scrolls and stops on the target.
The opacityAnimationWeights
can be used to apply custom weights to these
three stages of this animation. The default weights, [40, 20, 40]
, are
good with default Curves.linear. Different weights might be better for
other cases. For example, if you use Curves.easeOut, consider setting
opacityAnimationWeights
to [20, 20, 60]
.
See TweenSequenceItem.weight for more info.
Implementation
Future<void> scrollTo({
required int index,
double alignment = 0,
required Duration duration,
Curve curve = Curves.linear,
List<double> opacityAnimationWeights = const [40, 20, 40],
}) {
assert(
_scrollableListState != null,
'''ScrollController must be attached to a ScrollablePositionedList to scroll.''',
);
assert(
opacityAnimationWeights.length == 3,
'opacityAnimationWeights must have exactly three elements.',
);
assert(duration > Duration.zero, 'Duration must be greater than zero.');
return _scrollableListState!._scrollTo(
index: index,
alignment: alignment,
duration: duration,
curve: curve,
opacityAnimationWeights: opacityAnimationWeights,
);
}