animateTo method

  1. @override
Future<void> animateTo(
  1. double to, {
  2. required Duration duration,
  3. required Curve curve,
})
override

Animates the position from its current value to the given value.

Any active animation is canceled. If the user is currently scrolling, that action is canceled.

The returned Future will complete when the animation ends, whether it completed successfully or whether it was interrupted prematurely.

An animation will be interrupted whenever the user attempts to scroll manually, or whenever another activity is started, or whenever the animation reaches the edge of the viewport and attempts to overscroll. (If the ScrollPosition does not overscroll but instead allows scrolling beyond the extents, then going beyond the extents will not interrupt the animation.)

The animation is indifferent to changes to the viewport or content dimensions.

Once the animation has completed, the scroll position will attempt to begin a ballistic activity in case its value is not stable (for example, if it is scrolled beyond the extents and in that situation the scroll position would normally bounce back).

The duration must not be zero. To jump to a particular value without an animation, use jumpTo.

The animation is typically handled by an DrivenScrollActivity.

Implementation

@override
Future<void> animateTo(
  double to, {
  required Duration duration,
  required Curve curve,
}) {
  if (nearEqual(to, pixels, physics.toleranceFor(this).distance)) {
    // Skip the animation, go straight to the position as we are already close.
    jumpTo(to);
    return Future<void>.value();
  }

  final DrivenScrollActivity activity = DrivenScrollActivity(
    this,
    from: pixels,
    to: to,
    duration: duration,
    curve: curve,
    vsync: context.vsync,
  );
  beginActivity(activity);
  return activity.done;
}