Sub topic 12
Making Painted Shapes Respond to Touch
Assume you know Dart classes, Flutter widget trees, layout constraints, and StatefulWidget. This chapter focuses on the boundary between painting and input. Drawing a shape does not automatically make that shape interactive. You must define which local coordinates count as a hit and then handle the gesture.
How painter hit testing works
CustomPainter.hitTest receives a pointer position in the painter's local coordinate system. Return true when that position belongs to the painted target. Return false when it does not.
A GestureDetector outside CustomPaint can then receive gestures only when the custom-painted render object accepts the hit.
The position is relative to the CustomPaint box, not the whole screen. Therefore, the geometry used by paint and hitTest must agree.
Implementing a circular target
This example paints a circle, accepts taps only inside that circle, and changes its color after activation.
import 'package:flutter/material.dart';
void main() {
runApp(const MaterialApp(home: TouchDemo()));
}
class TouchDemo extends StatefulWidget {
const TouchDemo({super.key});
@override
State<TouchDemo> createState() => _TouchDemoState();
}
class _TouchDemoState extends State<TouchDemo> {
static const center = Offset(120, 90);
bool _selected = false;
void _activate() {
setState(() {
_selected = !_selected;
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: Center(
child: Semantics(
button: true,
label: 'Interactive teal circle',
hint: 'Double tap to change its color',
onTap: _activate,
child: GestureDetector(
onTap: _activate,
child: CustomPaint(
size: const Size(240, 180),
painter: DotPainter(
center: center,
radius: 48,
selected: _selected,
),
),
),
),
),
);
}
}
class DotPainter extends CustomPainter {
const DotPainter({
required this.center,
required this.radius,
required this.selected,
});
final Offset center;
final double radius;
final bool selected;
@override
void paint(Canvas canvas, Size size) {
final paint = Paint()
..color = selected ? Colors.orange : Colors.teal;
canvas.drawCircle(center, radius, paint);
}
@override
bool? hitTest(Offset position) {
return (position - center).distance <= radius;
}
@override
bool shouldRepaint(covariant DotPainter oldDelegate) {
return oldDelegate.center != center ||
oldDelegate.radius != radius ||
oldDelegate.selected != selected;
}
}hitTest uses the distance between two offsets. A point is inside the circle when that distance is no greater than the radius. This test includes the boundary.
For a painted path, use the same path construction in both methods:
@override
bool? hitTest(Offset position) {
return path.contains(position);
}Keep the path available through a helper or a field so that painting and hit testing cannot drift apart.
Gestures, sizing, and accessibility
CustomPaint needs a size. If it has no child, provide size or constrain it with a parent. Otherwise, it may have no meaningful area to hit.
hitTest handles pointer routing, but it does not describe the control to screen readers. Wrap the painter in Semantics and provide a label and action. The semantic action usually represents the whole control, so it can be larger than the exact visual hit region.
For complex interactive drawings, consider separate semantic widgets or a custom SemanticsBuilder. This lets assistive technology discover individual chart points, regions, or controls instead of encountering one unlabeled canvas.
Exercise
Why should a painter's hitTest geometry be kept consistent with its paint geometry?
Show answer
Because hitTest receives local coordinates and decides whether a gesture reaches the widget. If its geometry differs from the painted shape, users may tap visible areas that do not respond or activate empty areas unexpectedly.