animateTo method

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

Animates the attached sheet from its current size to the given size, a fractional value of the parent container's height.

Any active sheet animation is canceled. If the sheet's internal scrollable is currently animating (e.g. responding to a user fling), that animation is canceled as well.

An animation will be interrupted whenever the user attempts to scroll manually, whenever another activity is started, or when the sheet hits its max or min size (e.g. if you animate to 1 but the max size is .8, the animation will stop playing when it reaches .8).

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

The sheet will not snap after calling animateTo even if DraggableScrollableSheet.snap is true. Snapping only occurs after user drags.

When calling animateTo in widget tests, awaiting the returned Future may cause the test to hang and timeout. Instead, use WidgetTester.pumpAndSettle.

Implementation

Future<void> animateTo(
  double size, {
  required Duration duration,
  required Curve curve,
}) async {
  _assertAttached();
  assert(size >= 0 && size <= 1);
  assert(duration != Duration.zero);
  final AnimationController animationController = AnimationController.unbounded(
    vsync: _attachedController!.position.context.vsync,
    value: _attachedController!.extent.currentSize,
  );
  _animationControllers.add(animationController);
  _attachedController!.position.goIdle();
  // This disables any snapping until the next user interaction with the sheet.
  _attachedController!.extent.hasDragged = false;
  _attachedController!.extent.hasChanged = true;
  _attachedController!.extent.startActivity(onCanceled: () {
    // Don't stop the controller if it's already finished and may have been disposed.
    if (animationController.isAnimating) {
      animationController.stop();
    }
  });
  animationController.addListener(() {
    _attachedController!.extent.updateSize(
      animationController.value,
      _attachedController!.position.context.notificationContext!,
    );
  });
  await animationController.animateTo(
    clampDouble(size, _attachedController!.extent.minSize, _attachedController!.extent.maxSize),
    duration: duration,
    curve: curve,
  );
}