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.

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, we should let
  // the `targetRenderObject` as visible as possible to improve the user experience.
  // Otherwise, let the outer renderObject as visible as possible maybe cause
  // the `targetRenderObject` invisible.
  // Also see https://github.com/flutter/flutter/issues/65100
  RenderObject? targetRenderObject;
  SheetState? scrollable = SheetScrollable.of(context);
  while (scrollable != null) {
    futures.add(scrollable.position.ensureVisible(
      context.findRenderObject()!,
      alignment: alignment,
      duration: duration,
      curve: curve,
      alignmentPolicy: alignmentPolicy,
      targetRenderObject: targetRenderObject,
    ));

    targetRenderObject = targetRenderObject ?? context.findRenderObject();
    context = scrollable.context;
    scrollable = SheetScrollable.of(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);
}