scrollToVisible method

void scrollToVisible(
  1. Rect rect, {
  2. BuildContext? context,
  3. bool propagate = true,
})

Scrolls an area to be visible.

If the propagate flag is set to true (the default), this will recursively scroll the area in this scroll pane and all ancestor scroll panes. If the propagate flag is false, this will only scroll the area to visible in this scroll pane.

If the area is not able to be made entirely visible, then this will scroll to reveal as much of the area as possible.

The rect argument specifies the area to make visible.

If supplied, the context argument specifies a descendant build context of the scroll pane that defines the coordinate space of rect. If this argument is not specified, rect will be interpreted as in the coordinate space of the scroll pane itself.

Implementation

void scrollToVisible(Rect rect, {BuildContext? context, bool propagate = true}) {
  context ??= this.context;
  final RenderObject? childRenderObject = context.findRenderObject();
  final RenderObject? renderScrollPane = this.context.findRenderObject();
  assert(childRenderObject != null);
  assert(renderScrollPane != null);
  final Matrix4 transform = childRenderObject!.getTransformTo(renderScrollPane!);
  Rect scrollToRect = MatrixUtils.transformRect(transform, rect);
  if (context != this.context) {
    scrollToRect = scrollToRect.shift(controller.scrollOffset);
  }
  controller.scrollToVisible(scrollToRect);
  if (propagate) {
    final Rect adjustedRect = scrollToRect.shift(controller.scrollOffset * -1);
    ScrollPane.of(this.context)?.scrollToVisible(adjustedRect, context: this.context);
  }
}