hitTest method
Determines the set of render objects located at the given position.
Returns true, and adds any render objects that contain the point to the given hit test result, if this render object or one of its descendants absorbs the hit (preventing objects below this one from being hit). Returns false if the hit can continue to other objects below this one.
The caller is responsible for transforming position
from global
coordinates to its location relative to the origin of this RenderBox.
This RenderBox is responsible for checking whether the given position is
within its bounds.
If transforming is necessary, BoxHitTestResult.addWithPaintTransform,
BoxHitTestResult.addWithPaintOffset, or
BoxHitTestResult.addWithRawTransform need to be invoked by the caller
to record the required transform operations in the HitTestResult. These
methods will also help with applying the transform to position
.
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 though it does not paint its children.
Implementation
@override
bool hitTest(BoxHitTestResult result, {required Offset position}) {
if (animationController != null && animationController!.isAnimating) {
return false;
}
bool isDataLabelHit = false;
if (dataLabelContainer != null) {
final ChartSeriesParentData childParentData =
dataLabelContainer!.parentData! as ChartSeriesParentData;
isDataLabelHit = result.addWithPaintOffset(
offset: childParentData.offset,
position: position,
hitTest: (BoxHitTestResult result, Offset transformed) {
return dataLabelContainer!.hitTest(result, position: transformed);
},
);
}
bool isTrendlineHit = false;
if (trendlineContainer != null) {
final ChartSeriesParentData childParentData =
trendlineContainer!.parentData! as ChartSeriesParentData;
isTrendlineHit = result.addWithPaintOffset(
offset: childParentData.offset,
position: position,
hitTest: (BoxHitTestResult result, Offset transformed) {
return trendlineContainer!.hitTest(result, position: transformed);
},
);
}
final bool hasTouchCallback = onPointLongPress != null ||
onPointTap != null ||
onPointDoubleTap != null;
bool isSeriesHit = false;
if (_isVisible() &&
(_tooltipEnabled || _selectionEnabled || hasTouchCallback)) {
if (hitInsideSegment(position)) {
isSeriesHit = true;
}
}
return isTrendlineHit || isDataLabelHit || isSeriesHit;
}