Sub topic 2
How Flutter describes your UI to assistive technology
This chapter explains how Flutter turns widgets into something TalkBack, VoiceOver and other assistive technologies can read. It assumes you can build layouts with StatelessWidget and StatefulWidget, and that you can run an app on an emulator or device. Later chapters cover testing with real screen readers and adding labels with Semantics. This one covers the model they build on.
Why Flutter needs a separate tree
Android and iOS apps built with native views get accessibility from the platform. Each TextView or UILabel is already an object the operating system can inspect. Flutter is different. It draws every pixel itself on a single surface, so to the operating system your app looks like one big canvas.
A screen reader cannot read pixels. Flutter therefore builds a second description of the screen alongside the visual one, called the semantics tree. It contains only what an assistive technology needs: what each element means, what the user can do with it, and where it is on screen. Flutter then passes this tree to the platform's accessibility API.
From widgets to platform services
Flutter keeps several trees. Only the last two matter for accessibility.
- The widget tree is your configuration, the code you write.
- The element tree holds the live instances of those widgets.
- The render tree does layout and painting.
- The semantics tree is built from the render tree, and each render object may contribute to it.
- The platform bridge sends the semantics tree to Android's accessibility framework or to iOS's UIAccessibility.
Each render object can describe itself by overriding describeSemanticsConfiguration. Standard widgets such as Text, ElevatedButton and TextField do this for you. A custom CustomPaint or a bare GestureDetector child does not, so nothing is described unless you add it.
What a semantics node contains
The semantics tree is made of SemanticsNode objects. A node is a summary of one or more widgets. The properties you will meet most often are in this table.
| Property | Meaning | Example |
|---|---|---|
| `label` | The text a screen reader speaks | "Add to favorites" |
| `value` | The current value of a control | "50 percent" |
| `hint` | What happens on activation | "Saves this item to your list" |
| Flags | The role and state of the element | button, header, text field, checked, enabled |
| Actions | What the user can trigger | tap, long press, scroll, increase, decrease |
| Rect and transform | Position and size on screen | Used for touch exploration and focus outlines |
Flags and actions are what give an element its role. A node with the button flag and a tap action is announced as a button. A node with only a label is announced as plain text. A node with a tap action but no label and no role is the classic accessibility bug: the screen reader lands on something tappable and has nothing to say about it.
Semantics are generated on demand
Building the semantics tree costs CPU time, so Flutter only does it when something asks for it. Android and iOS tell the engine when an assistive technology is running, and Flutter turns semantics on. When no assistive technology is running, the tree is not built.
This has practical consequences:
- If you print the tree in a normal debug session with no screen reader on, you get nothing.
- In widget tests, semantics are off unless you request them with
tester.ensureSemantics(). - A bug that only appears with TalkBack or VoiceOver on will not show up in a normal run.
Merging and boundaries
Flutter does not send one node per widget. A RaisedButton-style control is made of a Material, an InkWell, a Padding and a Text. If each were a separate stop, the user would swipe four times to pass one button.
To avoid this, Flutter merges nodes. A node that is marked as a semantic boundary collects the semantics of its descendants into one node, joining their labels. This is why ElevatedButton(child: Text('Save')) is announced as "Save, button" and not as a separate text and a separate button. Later chapters show how to control merging yourself with MergeSemantics, ExcludeSemantics and Semantics(container: true).
Seeing the tree
You can inspect the tree in three ways. The first two work without a screen reader.
- The semantics debugger. Setting
showSemanticsDebugger: trueonMaterialAppdraws each node's label and role over a plain white background, so you see what a screen reader would get. - A text dump. Calling
debugDumpSemanticsTree()frompackage:flutter/rendering.dartprints the tree to the console. It only prints something when semantics are enabled. - Flutter DevTools. The Flutter Inspector has an option to show semantics in the widget tree.
This complete app shows a labeled button and an unlabeled gesture target side by side.
import 'package:flutter/material.dart';
void main() => runApp(const DemoApp());
class DemoApp extends StatelessWidget {
const DemoApp({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(
showSemanticsDebugger: true,
home: Scaffold(
appBar: AppBar(title: const Text('Semantics demo')),
body: Center(
child: Row(
mainAxisSize: MainAxisSize.min,
children: [
IconButton(
icon: const Icon(Icons.favorite_border),
tooltip: 'Add to favorites',
onPressed: () {},
),
GestureDetector(
onTap: () {},
child: const Padding(
padding: EdgeInsets.all(12),
child: Icon(Icons.star_border),
),
),
],
),
),
),
);
}
}Run it and look at the overlay. The IconButton shows its tooltip text because IconButton uses the tooltip as its accessible name. The star shows a tappable box with no text. GestureDetector adds a tap action to its node, but nothing tells the node what it is. This is the case that the Semantics widget, covered in a later chapter, exists to fix.
Checking the tree in a test
Because semantics must be enabled explicitly in tests, a test that looks for labels without ensureSemantics() finds nothing. This test checks that a button exposes the right label and role.
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
void main() {
testWidgets('Save button is exposed as a labeled button', (tester) async {
final handle = tester.ensureSemantics();
await tester.pumpWidget(
MaterialApp(
home: Scaffold(
body: ElevatedButton(onPressed: () {}, child: const Text('Save')),
),
),
);
expect(
tester.getSemantics(find.byType(ElevatedButton)),
containsSemantics(label: 'Save', isButton: true),
);
handle.dispose();
});
}ensureSemantics() returns a handle. Dispose it at the end so semantics are switched off again for other tests.
Common errors
- Unlabeled icons. An
Iconwith nosemanticLabeland no surrounding label contributes nothing. Inside a button with no tooltip, the user hears "button" and nothing else. - Custom gestures with no role.
GestureDetectorandInkWellon a custom child give an action but no role and no name. - Testing only with the debugger. The overlay is a good approximation, but TalkBack and VoiceOver differ in small ways, such as how they speak hints. Confirm important screens on a real device.
- Visual order assumed to be reading order. The tree follows layout order by default. Stacks and positioned widgets can place nodes in a surprising sequence, which a later chapter on reading order addresses.
- Forgetting that semantics are lazy. Code that inspects the tree, in tests or at runtime, needs semantics to be enabled first.
Exercises
Exercise
Why does Flutter build a semantics tree, and why is it not always built?
Show answer
Flutter draws its own pixels, so the operating system cannot inspect native views to find out what is on screen. The semantics tree gives assistive technology a description of labels, roles, actions and positions. Building it costs CPU time, so Flutter generates it only when an assistive technology, or a request such as ensureSemantics() in a test, turns it on.
Exercise
A GestureDetector wraps an Icon(Icons.star_border). A screen reader user can focus it and double-tap, but hears no useful description. Which parts of the semantics node are missing, and where does the node's tap action come from?
Show answer
The node is missing a label and a role flag such as button. The tap action comes from GestureDetector, which adds one to its semantics node, while the Icon supplies no label. Wrapping the detector in a Semantics widget with button: true and a label fixes it.
Exercise
Write the test setup lines needed before tester.getSemantics(...) returns a node, and name the line that should run at the end of the test.
Show answer
Call final handle = tester.ensureSemantics(); before pumping the widget, and call handle.dispose(); at the end of the test.