hitTest method

  1. @override
bool hitTest (Size size, Offset position, { TextDirection textDirection })

Tests whether the given point, on a rectangle of a given size, would be considered to hit the decoration or not. For example, if the decoration only draws a circle, this function might return true if the point was inside the circle and false otherwise.

The decoration may be sensitive to the TextDirection. The textDirection argument should therefore be provided. If it is known that the decoration is not affected by the text direction, then the argument may be omitted or set to null.

When a Decoration is painted in a Container or DecoratedBox (which is what Container uses), the textDirection parameter will be populated based on the ambient Directionality (by way of the RenderDecoratedBox renderer).


bool hitTest(Size size, Offset position, { TextDirection textDirection }) {
  assert(shape != null);
  assert((Offset.zero & size).contains(position));
  switch (shape) {
    case BoxShape.rectangle:
      if (borderRadius != null) {
        final RRect bounds = borderRadius.resolve(textDirection).toRRect(Offset.zero & size);
        return bounds.contains(position);
      return true;
    case BoxShape.circle:
      // Circles are inscribed into our smallest dimension.
      final Offset center = size.center(Offset.zero);
      final double distance = (position - center).distance;
      return distance <= math.min(size.width, size.height) / 2.0;
  assert(shape != null);
  return null;