animateToHeader method

Future<void> animateToHeader(
  1. int index,
  2. Axis axis, {
  3. Duration duration = kDefaultScrollAnimationDuration,
  4. Curve curve = kDefaultScrollAnimationCurve,
})

Animates the scroll position of an [axis from its current value to the corresponding value of a header in the index index.

See also:

Implementation

Future<void> animateToHeader(
  int index,
  Axis axis, {
  Duration duration = kDefaultScrollAnimationDuration,
  Curve curve = kDefaultScrollAnimationCurve,
}) async {
  if (!isAttached) {
    return;
  }

  final scrollController = getScrollControllerFor(axis: axis)!;

  // Get the pixel offset of this header necessary to make it visible.
  final animationOffset = _getOffsetToHeader(index, axis, scrollController);

  // If there is no animation offset, don't scroll
  if (animationOffset == null) {
    return;
  }

  // Finally, apply offset
  return scrollController.animateTo(
    animationOffset,
    duration: duration,
    curve: curve,
  );
}