Sub topic 14
Adding Semantics to Custom-Painted Widgets
Assume you know Dart classes, basic Flutter widget trees, layout constraints, and CustomPainter. This chapter focuses on accessibility. It does not cover painting shapes or handling gestures in detail.
Why custom painting needs semantics
A Canvas draws pixels. Screen readers do not understand those pixels automatically. A painted chart, button, icon, or status indicator can look correct while exposing no useful information to TalkBack or VoiceOver.
Flutter's semantics tree is a separate description of the interface. It contains information such as labels, values, hints, roles, and actions. Flutter sends this information to the platform's accessibility services.
You can add semantics in two common ways:
- Wrap the entire
CustomPaintin aSemanticswidget when it represents one accessible object. - Override
semanticsBuilderinCustomPainterwhen different painted regions need different semantic nodes.
One semantic object with `Semantics`
Use a Semantics widget for a simple control. Its label should explain what the object is, and its value should describe its current state.
import 'package:flutter/material.dart';
void main() {
runApp(const MaterialApp(home: Scaffold(body: Center(child: ScoreCard()))));
}
class ScoreCard extends StatelessWidget {
const ScoreCard({super.key});
@override
Widget build(BuildContext context) {
return Semantics(
button: true,
label: 'Team score',
value: '42 points',
hint: 'Double tap to open score details',
child: CustomPaint(
size: const Size(180, 100),
painter: ScorePainter(),
),
);
}
}
class ScorePainter extends CustomPainter {
@override
void paint(Canvas canvas, Size size) {
final background = Paint()..color = Colors.indigo;
canvas.drawRRect(
RRect.fromRectAndRadius(
Offset.zero & size,
const Radius.circular(12),
),
background,
);
final textPainter = TextPainter(
text: const TextSpan(
text: '42',
style: TextStyle(color: Colors.white, fontSize: 40),
),
textDirection: TextDirection.ltr,
)..layout();
textPainter.paint(
canvas,
Offset(
(size.width - textPainter.width) / 2,
(size.height - textPainter.height) / 2,
),
);
}
@override
bool shouldRepaint(covariant ScorePainter oldDelegate) => false;
}The Semantics node describes the whole card. A GestureDetector can handle the actual tap, but the semantics properties tell an assistive technology what the gesture means. Do not use the visible text as the only accessibility description. A screen reader needs a concise, meaningful label.
Multiple regions with `semanticsBuilder`
For a chart or diagram, one node may be insufficient. semanticsBuilder returns semantic regions in the painter's coordinate system. Each CustomPainterSemantics object contains a rectangle and SemanticsProperties.
class BarChartPainter extends CustomPainter {
const BarChartPainter(this.values);
final List<int> values;
@override
void paint(Canvas canvas, Size size) {
final barWidth = size.width / values.length;
final paint = Paint()..color = Colors.teal;
for (var index = 0; index < values.length; index++) {
final height = values[index].toDouble();
canvas.drawRect(
Rect.fromLTWH(
index * barWidth,
size.height - height,
barWidth - 8,
height,
),
paint,
);
}
}
@override
SemanticsBuilderCallback get semanticsBuilder {
return (Size size) {
final barWidth = size.width / values.length;
return [
for (var index = 0; index < values.length; index++)
CustomPainterSemantics(
rect: Rect.fromLTWH(index * barWidth, 0, barWidth, size.height),
properties: SemanticsProperties(
label: 'Bar ${index + 1}',
value: '${values[index]} units',
),
),
];
};
}
@override
bool shouldRepaint(covariant BarChartPainter oldDelegate) =>
oldDelegate.values != values;
@override
bool shouldRebuildSemantics(covariant BarChartPainter oldDelegate) =>
oldDelegate.values != values;
}The semantic rectangles should match the painted regions or the touch targets associated with them. If the data changes, update both painting and semantics. shouldRebuildSemantics controls whether Flutter rebuilds the semantic description.
Testing accessibility
Test with TalkBack on Android and VoiceOver on iOS. Move focus through every important painted object. Check that:
- Each object has a meaningful label.
- Dynamic values are current.
- Decorative details are not announced.
- Interactive objects expose their role and action.
- Semantic rectangles do not overlap unexpectedly.
Exercise
A custom-painted progress ring displays 75 percent but a screen reader announces nothing. What should you add?
Show answer
Wrap it in a Semantics widget with a label such as “Upload progress” and a value such as “75 percent”. If the ring contains separate interactive regions, provide those regions through semanticsBuilder instead.