hitTest method

  1. @override
bool hitTest(
  1. HitTestResult result, {
  2. required double localX,
  3. required double localY,
})
override

Performs hit-testing at (localX, localY) in this object's coordinate space. Adds matching entries to result, deepest first.

Returns true if this object or a descendant was hit.

Implementation

@override
bool hitTest(
  HitTestResult result, {
  required double localX,
  required double localY,
}) {
  if (localX < 0 ||
      localY < 0 ||
      localX >= size.width ||
      localY >= size.height) {
    return false;
  }

  if (children.isEmpty) return true;

  final offset = controller.offset.clamp(0, controller.maxOffset).toDouble();
  final contentY = localY + offset;
  final separatorBreaks = _separatorBreaks(separator).toDouble();
  var top = 0.0;

  for (var i = 0; i < children.length; i++) {
    final child = children[i];
    final bottom = top + child.size.height;

    if (contentY >= top && contentY < bottom) {
      final childX = localX - child.offset.dx;
      final childY = contentY - child.offset.dy;
      child.hitTest(result, localX: childX, localY: childY);
      break;
    }

    top = bottom;
    if (i < children.length - 1) {
      top += separatorBreaks;
    }
  }

  // Capture the hit for wheel/key scrolling even when no child is hit.
  return true;
}