bestPopupAnchorRenderObject function

RenderObject bestPopupAnchorRenderObject(
  1. Element host,
  2. RenderObject root
)

Returns the render object to use as the anchor for a popup menu.

Implementation

RenderObject bestPopupAnchorRenderObject(Element host, RenderObject root) {
  final rootWidth = root.size.width;
  final rootHeight = root.size.height;
  if (rootWidth <= 0 || rootHeight <= 0) return root;

  RenderObject? best;
  for (final candidate in renderObjectsInSubtree(host)) {
    if (identical(candidate, root)) continue;

    final width = candidate.size.width;
    final height = candidate.size.height;
    if (width <= 0 || height <= 0) continue;
    if (height > rootHeight + 0.001) continue;
    if (width >= rootWidth - 0.001) continue;

    if (best == null || width > best.size.width) {
      best = candidate;
    }
  }

  if (best == null) return root;

  // Only switch anchors when the descendant is materially narrower.
  if (rootWidth - best.size.width < 2) return root;
  return best;
}