ensureVisible static method

Future<void> ensureVisible(
  1. BuildContext context, {
  2. double alignment = 0.0,
  3. Duration duration = Duration.zero,
  4. Curve curve = Curves.ease,
  5. ScrollPositionAlignmentPolicy alignmentPolicy = ScrollPositionAlignmentPolicy.explicit,
})

Scrolls the scrollables that enclose the given context so as to make the given context visible.

If the Scrollable of the provided BuildContext is a TwoDimensionalScrollable, both vertical and horizontal axes will ensure the target is made visible.

Implementation

static Future<void> ensureVisible(
  BuildContext context, {
  double alignment = 0.0,
  Duration duration = Duration.zero,
  Curve curve = Curves.ease,
  ScrollPositionAlignmentPolicy alignmentPolicy = ScrollPositionAlignmentPolicy.explicit,
}) {
  final List<Future<void>> futures = <Future<void>>[];

  // The targetRenderObject is used to record the first target renderObject.
  // If there are multiple scrollable widgets nested, the targetRenderObject
  // is made to be as visible as possible to improve the user experience. If
  // the targetRenderObject is already visible, then let the outer
  // renderObject be as visible as possible.
  //
  // Also see https://github.com/flutter/flutter/issues/65100
  RenderObject? targetRenderObject;
  ScrollableState? scrollable = Scrollable.maybeOf(context);
  while (scrollable != null) {
    final List<Future<void>> newFutures;
    (newFutures, scrollable) = scrollable._performEnsureVisible(
      context.findRenderObject()!,
      alignment: alignment,
      duration: duration,
      curve: curve,
      alignmentPolicy: alignmentPolicy,
      targetRenderObject: targetRenderObject,
    );
    futures.addAll(newFutures);

    targetRenderObject ??= context.findRenderObject();
    context = scrollable.context;
    scrollable = Scrollable.maybeOf(context);
  }

  if (futures.isEmpty || duration == Duration.zero) {
    return Future<void>.value();
  }
  if (futures.length == 1) {
    return futures.single;
  }
  return Future.wait<void>(futures).then<void>((List<void> _) => null);
}