Sub topic 4
What Flutter Widgets Give You for Free
This chapter assumes basic Dart syntax, Flutter layouts, and familiarity with StatelessWidget and StatefulWidget. It focuses on the accessibility information that standard Flutter widgets provide automatically.
Standard widgets expose useful semantics
Flutter builds a semantics tree alongside the widget tree. The semantics tree describes visible controls to platform accessibility services such as TalkBack, VoiceOver, and keyboard tools.
Many Material and Cupertino widgets already expose their role, label, state, and available actions. For example, a TextButton is announced as a button, a Checkbox exposes whether it is checked, and a TextField exposes its label and editable state.
| Widget | Information commonly provided |
|---|---|
| `TextButton` | Button role, visible text, tap action |
| `Checkbox` | Checkbox role, label, checked state, change action |
| `TextField` | Text field role, label, value, editing action |
| `Switch` | Switch role, label, on or off state |
| `Image` | Description when `semanticLabel` is provided |
A standard widget can still be inaccessible when its visible content is incomplete. An icon by itself does not explain its purpose. Give an IconButton a tooltip:
IconButton(
icon: const Icon(Icons.favorite_border),
tooltip: 'Add to favorites',
onPressed: _toggleFavorite,
)For an IconButton, the tooltip becomes the accessibility label. A screen reader can announce “Add to favorites, button” instead of announcing only an unlabeled icon. The tooltip also helps sighted users who hold the pointer over the control.
Images need an intentional meaning
An informative image needs a text description. Use semanticLabel when the image communicates information that users need:
Image.asset(
'assets/team.png',
semanticLabel: 'Four teammates standing around a whiteboard',
)Some images are decoration only. A decorative divider should not interrupt screen-reader navigation:
Image.asset(
'assets/divider.png',
excludeFromSemantics: true,
)Do not describe the same information twice. If a nearby text caption already explains the image, excluding the duplicate image description may make the reading experience clearer.
When built-in semantics are not enough
A custom GestureDetector has no useful role or label by default. A screen reader may skip it or announce an unlabeled element. Use a standard button where possible. If the design requires a custom control, add a Semantics widget around it.
The relationship looks like this:
Check the result
Built-in semantics are a starting point, not a guarantee. Run the app with TalkBack on Android or VoiceOver on iOS. Move through the screen and check that each control has a useful name, role, state, and action. Pay special attention to icon-only controls and custom gestures.
Exercise
What should you add to an icon-only IconButton that performs a meaningful action?
Show answer
Add a tooltip that clearly names the action, such as 'Add to favorites'. Flutter uses it as the accessibility label.