animateToHeader method
Animates the scroll position of an [axis from its current value to the
corresponding value of a header in the index index
.
See also:
- jumpToHeader instantly scroll to a header with no animation
- animateToCoordinate animate to a particular coordinate
- ScrollController.animateTo the corresponding built int to flutter scroll mechanism.
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,
);
}