showInViewport static method

Rect? showInViewport({
  1. RenderObject? descendant,
  2. Rect? rect,
  3. required RenderLayoutBox viewport,
  4. Duration duration = Duration.zero,
  5. Curve curve = Curves.ease,
  6. AxisDirection? axisDirection,
})

Shows a descendant in the viewport by scrolling if necessary.

Calculates the necessary scroll adjustments to make a descendant visible within the viewport. Handles both horizontal and vertical scrolling and supports animated scrolling with specified duration and curve.

Returns the target rectangle that will be visible, or null if no scrolling is needed.

Implementation

static Rect? showInViewport({
  RenderObject? descendant,
  Rect? rect,
  required RenderLayoutBox viewport,
  Duration duration = Duration.zero,
  Curve curve = Curves.ease,
  AxisDirection? axisDirection,
}) {
  if (descendant == null) {
    return rect;
  }

  Rect? showVertical(Rect? rect) {
    return _showInViewportForAxisDirection(
      descendant: descendant,
      viewport: viewport,
      axis: Axis.vertical,
      rect: rect,
      duration: duration,
      curve: curve,
    );
  }

  Rect? showHorizontal(Rect? rect) {
    return _showInViewportForAxisDirection(
      descendant: descendant,
      viewport: viewport,
      axis: Axis.horizontal,
      rect: rect,
      duration: duration,
      curve: curve,
    );
  }

  switch (axisDirection) {
    case AxisDirection.left:
    case AxisDirection.right:
      return showHorizontal(rect);
    case AxisDirection.up:
    case AxisDirection.down:
      return showVertical(rect);
    case null:
      // Update rect after revealing in one axis before revealing in the next.
      rect = showHorizontal(rect) ?? rect;
      // We only return the final rect after both have been revealed.
      rect = showVertical(rect);
      if (rect == null) {
        // `descendant` is between leading and trailing edge and hence already
        //  fully shown on screen.
        assert(viewport.parent != null);
        final Matrix4 transform = descendant.getTransformTo(viewport.parent);
        return MatrixUtils.transformRect(
          transform,
          rect ?? descendant.paintBounds,
        );
      }
      return rect;
  }
}