getOffsetToReveal method

  1. @protected
  2. @override
RevealedOffset getOffsetToReveal(
  1. RenderObject target,
  2. double alignment,
  3. {Rect? rect,
  4. Axis? axis}
)
override

Returns the offset that would be needed to reveal the target RenderObject.

This is used by RenderViewportBase.showInViewport, which is itself used by RenderObject.showOnScreen for RenderViewportBase, which is in turn used by the semantics system to implement scrolling for accessibility tools.

The optional rect parameter describes which area of that target object should be revealed in the viewport. If rect is null, the entire target RenderObject (as defined by its RenderObject.paintBounds) will be revealed. If rect is provided it has to be given in the coordinate system of the target object.

The alignment argument describes where the target should be positioned after applying the returned offset. If alignment is 0.0, the child must be positioned as close to the leading edge of the viewport as possible. If alignment is 1.0, the child must be positioned as close to the trailing edge of the viewport as possible. If alignment is 0.5, the child must be positioned as close to the center of the viewport as possible.

The target might not be a direct child of this viewport but it must be a descendant of the viewport. Other viewports in between this viewport and the target will not be adjusted.

This method assumes that the content of the viewport moves linearly, i.e. when the offset of the viewport is changed by x then target also moves by x within the viewport.

The optional Axis is used by RenderTwoDimensionalViewport.getOffsetToReveal to determine which of the two axes to compute an offset for. One dimensional subclasses like RenderViewportBase and RenderListWheelViewport will ignore the axis value if provided, since there is only one Axis.

If the axis is omitted when called on RenderTwoDimensionalViewport, the RenderTwoDimensionalViewport.mainAxis is used. To reveal an object properly in both axes, this method should be called for each Axis as the returned RevealedOffset.offset only represents the offset of one of the the two ScrollPositions.

See also:

Implementation

@protected
@override
RevealedOffset getOffsetToReveal(
  RenderObject target,
  double alignment, {
  Rect? rect,
  Axis? axis,
}) {
  // If an axis has not been specified, use the mainAxis.
  axis ??= mainAxis;

  final (double offset, AxisDirection axisDirection) = switch (axis) {
    Axis.vertical => (verticalOffset.pixels, verticalAxisDirection),
    Axis.horizontal => (horizontalOffset.pixels, horizontalAxisDirection),
  };

  rect ??= target.paintBounds;
  // `child` will be the last RenderObject before the viewport when walking
  // up from `target`.
  RenderObject child = target;
  while (child.parent != this) {
    child = child.parent!;
  }

  assert(child.parent == this);
  final RenderBox box = child as RenderBox;
  final Rect rectLocal = MatrixUtils.transformRect(target.getTransformTo(child), rect);

  final double targetMainAxisExtent;
  double leadingScrollOffset = offset;
  // The scroll offset of `rect` within `child`.
  switch (axisDirection) {
    case AxisDirection.up:
      leadingScrollOffset += child.size.height - rectLocal.bottom;
      targetMainAxisExtent = rectLocal.height;
    case AxisDirection.right:
      leadingScrollOffset += rectLocal.left;
      targetMainAxisExtent = rectLocal.width;
    case AxisDirection.down:
      leadingScrollOffset += rectLocal.top;
      targetMainAxisExtent = rectLocal.height;
    case AxisDirection.left:
      leadingScrollOffset += child.size.width - rectLocal.right;
      targetMainAxisExtent = rectLocal.width;
  }

  // The scroll offset in the viewport to `rect`.
  final TwoDimensionalViewportParentData childParentData = parentDataOf(box);
  leadingScrollOffset += switch (axisDirection) {
    AxisDirection.down => childParentData.paintOffset!.dy,
    AxisDirection.up => viewportDimension.height - childParentData.paintOffset!.dy - box.size.height,
    AxisDirection.right => childParentData.paintOffset!.dx,
    AxisDirection.left => viewportDimension.width - childParentData.paintOffset!.dx - box.size.width,
  };

  // This step assumes the viewport's layout is up-to-date, i.e., if
  // the position is changed after the last performLayout, the new scroll
  // position will not be accounted for.
  final Matrix4 transform = target.getTransformTo(this);
  Rect targetRect = MatrixUtils.transformRect(transform, rect);

  final double mainAxisExtent = switch (axisDirectionToAxis(axisDirection)) {
    Axis.horizontal => viewportDimension.width,
    Axis.vertical => viewportDimension.height,
  };

  final double targetOffset = leadingScrollOffset - (mainAxisExtent - targetMainAxisExtent) * alignment;

  final double offsetDifference = switch (axisDirectionToAxis(axisDirection)){
    Axis.vertical => verticalOffset.pixels - targetOffset,
    Axis.horizontal => horizontalOffset.pixels - targetOffset,
  };
  switch (axisDirection) {
    case AxisDirection.down:
      targetRect = targetRect.translate(0.0, offsetDifference);
    case AxisDirection.right:
      targetRect = targetRect.translate(offsetDifference, 0.0);
    case AxisDirection.up:
      targetRect = targetRect.translate(0.0, -offsetDifference);
    case AxisDirection.left:
      targetRect = targetRect.translate(-offsetDifference, 0.0);
  }

  final RevealedOffset revealedOffset = RevealedOffset(
    offset: targetOffset,
    rect: targetRect,
  );
  return revealedOffset;
}