Determines the set of render objects located at the given position.
Returns true if the given point is contained in this render object or one of its descendants. Adds any render objects that contain the point to the given hit test result.
The caller is responsible for transforming position
into the local
coordinate space of the callee. The callee is responsible for checking
whether the given position is within its bounds.
Hit testing requires layout to be up-to-date but does not require painting
to be up-to-date. That means a render object can rely upon performLayout
having been called in hitTest but cannot rely upon paint having been
called. For example, a render object might be a child of a RenderOpacity
object, which calls hitTest on its children when its opacity is zero
even through it does not paint its children.
Source
bool hitTest(HitTestResult result, { @required Point position }) { assert(() { if (needsLayout) { throw new FlutterError( 'Cannot hit test a dirty render box.\n' 'The hitTest() method was called on this RenderBox:\n' ' $this\n' 'Unfortunately, since this object has been marked as needing layout, its geometry is not known at this time. ' 'This means it cannot be accurately hit-tested. Make sure to only mark nodes as needing layout during a pipeline ' 'flush, so that it is marked clean before any event handling occurs. If you are trying to perform a hit test ' 'during the layout phase itself, make sure you only hit test nodes that have completed layout (e.g. the node\'s ' 'children, after their layout() method has been called).' ); } if (!hasSize) { throw new FlutterError( 'Cannot hit test a render box with no size.\n' 'The hitTest() method was called on this RenderBox:\n' ' $this\n' 'Although this node is not marked as needing layout, its size is not set. A RenderBox object must have an ' 'explicit size before it can be hit-tested. Make sure that the RenderBox in question sets its size during layout.' ); } return true; }); if (position.x >= 0.0 && position.x < _size.width && position.y >= 0.0 && position.y < _size.height) { if (hitTestChildren(result, position: position) || hitTestSelf(position)) { result.add(new BoxHitTestEntry(this, position)); return true; } } return false; }