scrollTo method Null safety

Future<void> scrollTo(
  1. {required int index,
  2. double alignment = 0,
  3. required Duration duration,
  4. Curve curve = Curves.linear,
  5. List<double> opacityAnimationWeights = const [40, 20, 40]}
)

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:

  1. The currently displayed list view starts scrolling.
  2. Another list view, which scrolls with the same speed, fades over the first one and shows items that are close to the scroll target.
  3. 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);
  assert(opacityAnimationWeights.length == 3);
  assert(duration > Duration.zero);
  return _scrollableListState!._scrollTo(
    index: index,
    alignment: alignment,
    duration: duration,
    curve: curve,
    opacityAnimationWeights: opacityAnimationWeights,
  );
}