ensureVisible method Null safety

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

Animates the position such that the given object is as visible as possible by just scrolling this position.

The optional targetRenderObject parameter is used to determine which area of that object should be as visible as possible. If targetRenderObject is null, the entire RenderObject (as defined by its RenderObject.paintBounds) will be as visible as possible. If targetRenderObject is provided, it must be a descendant of the object.

See also:

Implementation

Future<void> ensureVisible(
  RenderObject object, {
  double alignment = 0.0,
  Duration duration = Duration.zero,
  Curve curve = Curves.ease,
  ScrollPositionAlignmentPolicy alignmentPolicy = ScrollPositionAlignmentPolicy.explicit,
  RenderObject? targetRenderObject,
}) {
  assert(alignmentPolicy != null);
  assert(object.attached);
  final RenderAbstractViewport viewport = RenderAbstractViewport.of(object)!;
  assert(viewport != null);

  Rect? targetRect;
  if (targetRenderObject != null && targetRenderObject != object) {
    targetRect = MatrixUtils.transformRect(
      targetRenderObject.getTransformTo(object),
      object.paintBounds.intersect(targetRenderObject.paintBounds),
    );
  }

  double target;
  switch (alignmentPolicy) {
    case ScrollPositionAlignmentPolicy.explicit:
      target = clampDouble(viewport.getOffsetToReveal(object, alignment, rect: targetRect).offset, minScrollExtent, maxScrollExtent);
      break;
    case ScrollPositionAlignmentPolicy.keepVisibleAtEnd:
      target = clampDouble(viewport.getOffsetToReveal(object, 1.0, rect: targetRect).offset, minScrollExtent, maxScrollExtent);
      if (target < pixels) {
        target = pixels;
      }
      break;
    case ScrollPositionAlignmentPolicy.keepVisibleAtStart:
      target = clampDouble(viewport.getOffsetToReveal(object, 0.0, rect: targetRect).offset, minScrollExtent, maxScrollExtent);
      if (target > pixels) {
        target = pixels;
      }
      break;
  }

  if (target == pixels) {
    return Future<void>.value();
  }

  if (duration == Duration.zero) {
    jumpTo(target);
    return Future<void>.value();
  }

  return animateTo(target, duration: duration, curve: curve);
}