getRenderBox method

RenderBox? getRenderBox(
  1. String id
)

Returns the RenderBox of the widget, needed to calculate position and size.

Triple strategy:

  1. If the widget uses getGlobalKey as key → uses globalKey.currentContext
  2. If the widget uses McpMetadataKey directly as key → walks the element tree
  3. If the widget uses ValueKey<String> with matching value → walks the element tree

Implementation

RenderBox? getRenderBox(String id) {
  // Strategy 1: via getContext (covers GlobalKey, McpMetadataKey, and ValueKey)
  final context = getContext(id);
  if (context != null) {
    final rb = context.findRenderObject() as RenderBox?;
    if (rb != null) return rb;
  }

  // Strategy 2: deep search for first RenderBox in subtree
  RenderBox? found;

  void visitElement(Element el) {
    if (found != null) return;
    final key = el.widget.key;
    final matches =
        (key is McpMetadataKey && key.id == id) ||
        (key is ValueKey<String> && key.value == id);
    if (matches) {
      found = _firstRenderBox(el);
      return;
    }
    el.visitChildElements(visitElement);
  }

  WidgetsBinding.instance.rootElement?.visitChildElements(visitElement);
  return found;
}