hitTestChildren method Null safety

  1. @override
  2. @protected
bool hitTestChildren(
  1. BoxHitTestResult result,
  2. {required Offset position}
)
protected">@protectedoverride

Override this method to check whether any children are located at the given position.

Subclasses should return true if at least one child reported a hit at the specified position.

Typically children should be hit-tested in reverse paint order so that hit tests at locations where children overlap hit the child that is visually "on top" (i.e., paints later).

The caller is responsible for transforming position from global coordinates to its location relative to the origin of this RenderBox. Likewise, this RenderBox is responsible for transforming the position that it passes to its children when it calls hitTest on each child.

If transforming is necessary, BoxHitTestResult.addWithPaintTransform, BoxHitTestResult.addWithPaintOffset, or BoxHitTestResult.addWithRawTransform need to be invoked by subclasses to record the required transform operations in the BoxHitTestResult. These methods will also help with applying the transform to position.

Used by hitTest. If you override hitTest and do not call this function, then you don't need to implement this function.

Implementation

@override
@protected
bool hitTestChildren(BoxHitTestResult result, { required Offset position }) {
  // Hit test text spans.
  bool hitText = false;
  final Offset effectivePosition = position - _paintOffset;
  final TextPosition textPosition = _textPainter.getPositionForOffset(effectivePosition);
  final InlineSpan? span = _textPainter.text!.getSpanForPosition(textPosition);
  if (span != null && span is HitTestTarget) {
    result.add(HitTestEntry(span as HitTestTarget));
    hitText = true;
  }

  // Hit test render object children
  RenderBox? child = firstChild;
  int childIndex = 0;
  while (child != null && childIndex < _textPainter.inlinePlaceholderBoxes!.length) {
    final TextParentData textParentData = child.parentData! as TextParentData;
    final Matrix4 transform = Matrix4.translationValues(
      textParentData.offset.dx,
      textParentData.offset.dy,
      0.0,
    )..scale(
      textParentData.scale,
      textParentData.scale,
      textParentData.scale,
    );
    final bool isHit = result.addWithPaintTransform(
      transform: transform,
      position: position,
      hitTest: (BoxHitTestResult result, Offset transformed) {
        assert(() {
          final Offset manualPosition = (position - textParentData.offset) / textParentData.scale!;
          return (transformed.dx - manualPosition.dx).abs() < precisionErrorTolerance
            && (transformed.dy - manualPosition.dy).abs() < precisionErrorTolerance;
        }());
        return child!.hitTest(result, position: transformed);
      },
    );
    if (isHit) {
      return true;
    }
    child = childAfter(child);
    childIndex += 1;
  }
  return hitText;
}