defaultHitTestChildren method

bool defaultHitTestChildren(
  1. BoxHitTestResult result, {
  2. Offset? position,
})

Performs a hit test on each child by walking the child list backwards.

Stops walking once after the first child reports that it contains the given point. Returns whether any children contain the given point.

See also:

  • defaultPaint, which paints the children appropriate for this hit-testing strategy.

Implementation

bool defaultHitTestChildren(BoxHitTestResult result, {Offset? position}) {
  // The x, y parameters have the top left of the node's box as the origin.

  // The z-index needs to be sorted, and higher-level nodes are processed first.
  List<RenderObject?> paintingOrder = (this as RenderLayoutBox).paintingOrder;
  for (int i = paintingOrder.length - 1; i >= 0; i--) {
    ChildType child = paintingOrder[i] as ChildType;
    // Ignore detached render object.
    if (!child.attached) {
      continue;
    }
    final ParentDataType childParentData = child.parentData as ParentDataType;
    final bool isHit = result.addWithPaintOffset(
      offset: childParentData.offset == Offset.zero ? null : childParentData.offset,
      position: position!,
      hitTest: (BoxHitTestResult result, Offset transformed) {
        assert(transformed == position - childParentData.offset);
        return child.hitTest(result, position: transformed);
      },
    );
    if (isHit) return true;
  }

  return false;
}