isRenderObjectAncestorOfTarget function

bool isRenderObjectAncestorOfTarget(
  1. RenderObject renderObject,
  2. HitTestTarget target
)

Returns true if renderObject is equal to target or is an ancestor of target in the render tree.

This is useful for hit testing because some render objects (like RenderTransform) don't add themselves to the hit test path but instead just transform the hit test point and pass it to their children.

Implementation

bool isRenderObjectAncestorOfTarget(RenderObject renderObject, HitTestTarget target) {
  if (target == renderObject) {
    return true;
  }
  if (target is RenderObject) {
    for (RenderObject? current = target.parent; current != null; current = current.parent) {
      if (current == renderObject) {
        return true;
      }
    }
  }
  return false;
}