Sub topic 4
What Flutter widgets give you for free
This chapter assumes you can build layouts with Flutter widgets, know the difference between StatelessWidget and StatefulWidget, and have run an app on an emulator or device. You should also know that Flutter builds a semantics tree next to the widget tree, and that screen readers such as TalkBack and VoiceOver read that tree.
The goal here is to learn which widgets already describe themselves to assistive technology. Then you can spend your effort only where the framework cannot guess what you mean.
Why standard widgets matter
Flutter draws its own pixels. The platform has no native button or text field to inspect, so each widget must report what it is. Material and Cupertino widgets do this inside their build methods. They add Semantics nodes with a role, a label, a state and a set of actions.
When you use a standard widget, you get several things at once:
- A role, such as button, text field, slider or checkbox.
- A label, usually taken from the widget's visible text or a property you supply.
- State, such as enabled, selected, checked or focused.
- Actions, such as tap, increase, decrease, scroll or set text.
- A focus node and keyboard activation on platforms that use them.
A custom GestureDetector over an icon gives you none of these. The next chapter covers how to add them by hand. First, look at what you can skip.
Text and buttons
A Text widget exposes its string as the semantics label. A screen reader reads it as is, so you rarely add anything.
The Material button family (ElevatedButton, FilledButton, OutlinedButton, TextButton) is marked as a button. Its label comes from its descendants' text. When onPressed is null, the button reports itself as disabled, and the screen reader says so.
import 'package:flutter/material.dart';
class SaveButton extends StatelessWidget {
const SaveButton({super.key, required this.canSave, required this.onSave});
final bool canSave;
final VoidCallback onSave;
@override
Widget build(BuildContext context) {
return FilledButton(
// Passing null makes the button announce as "dimmed" or "unavailable".
onPressed: canSave ? onSave : null,
child: const Text('Save'),
);
}
}Disabling a button through onPressed: null is better than keeping it enabled and ignoring taps. The state then reaches the screen reader without extra code.
Icon buttons and tooltips
An icon has no text, so a bare IconButton has no label. The tooltip property fills that gap. Flutter uses it as the semantics label, and it also shows a visible hint on long press or hover.
IconButton(
icon: const Icon(Icons.favorite_border),
tooltip: 'Add to favorites',
onPressed: _toggleFavorite,
)Without tooltip, TalkBack would say only "button". Several of the same unlabeled buttons in a row make a screen unusable.
The same applies to FloatingActionButton, PopupMenuButton and BackButton. The last two supply localized default tooltips, such as "Back" and "Show menu". Check that your app's localization delegates are set up, or those defaults will be missing for non-English users.
Form controls
Form widgets carry the richest built-in semantics. The table lists the common ones.
| Widget | Role and state exposed | Label source |
|---|---|---|
| `TextField` | Text field, current value, focused, obscured, multiline | `InputDecoration.labelText`, then `hintText` |
| `Checkbox` | Checkbox, checked or unchecked | None. Wrap it in `CheckboxListTile` or add a label. |
| `Switch` | Toggle, on or off | None. Use `SwitchListTile` or add a label. |
| `Radio` | Radio button, selected, part of a group | None. Use `RadioListTile`. |
| `Slider` | Slider, current value, increase and decrease actions | `semanticFormatterCallback` and `label` |
| `DropdownButton` | Button with a popup menu | The selected item's text |
The pattern is that the bare control has no label. The ListTile variants fix this, because they combine the control with a title and merge them into one focusable item.
SwitchListTile(
title: const Text('Notifications'),
subtitle: const Text('Send me a daily summary'),
value: _enabled,
onChanged: (value) => setState(() => _enabled = value),
)A screen reader reads this as "Notifications, Send me a daily summary, switch, on". The user double-taps once to toggle it.
For text fields, prefer labelText over hintText. A hint disappears when the user types, and it is a weaker label on some platforms. Put the field's name in labelText, and use hintText only for an example format.
const TextField(
decoration: InputDecoration(
labelText: 'Email',
hintText: '[email protected]',
errorText: null,
),
keyboardType: TextInputType.emailAddress,
)When you set errorText, the error becomes part of the field's semantics, so the reader announces it with the field.
Sliders
A Slider exposes increase and decrease actions. A screen reader user swipes up or down to change it. By default the spoken value is a percentage. Override that when the number has a unit.
Slider(
value: _volume,
min: 0,
max: 10,
divisions: 10,
label: _volume.round().toString(),
semanticFormatterCallback: (value) => '${value.round()} out of 10',
onChanged: (value) => setState(() => _volume = value),
)Lists, tiles and navigation
ListTile merges its title, subtitle, leading and trailing widgets into one semantics node when it has an onTap. The reader treats the row as one item. This is usually what you want, but it has a cost. If a trailing widget is a separate button, the merge can hide it. Test any tile that has both a tap handler and a trailing action.
Navigation widgets add position information:
BottomNavigationBarandNavigationBarreport each item as a tab with its place in the set, such as "tab 2 of 4", and mark the selected one.TabBardoes the same for tabs.AppBarmarks its title as a header, so users can jump between headers.ScaffoldandNavigatorannounce route changes on platforms that support it, so pushing a new page moves the reader's focus to it.
The same applies to dialogs. AlertDialog and showModalBottomSheet set a scope and a route name. Content behind a modal route is hidden from the semantics tree, so the user cannot swipe into it by accident.
Images and icons
Images are the main place where the framework cannot help. It cannot tell whether a picture is informative or decorative.
Imagehas no description unless you setsemanticLabel.Icontakes an optionalsemanticLabel. Without it, the icon is skipped by most readers.excludeFromSemantics: trueremoves a decorative image from the tree.
Image.asset(
'assets/team.png',
semanticLabel: 'Four teammates standing around a whiteboard',
)
Image.asset(
'assets/divider.png',
excludeFromSemantics: true,
)An Icon placed next to text is usually redundant, because the text already says the same thing. Leave it without a label in that case, so the screen reader does not read the same idea twice.
How to check what you got
Do not assume. Open the semantics tree and read it. The simplest way is to turn on showSemanticsDebugger and look at the labels drawn over each widget.
import 'package:flutter/material.dart';
void main() {
runApp(
MaterialApp(
showSemanticsDebugger: true,
home: Scaffold(
appBar: AppBar(title: const Text('Settings')),
body: Center(
child: IconButton(
icon: const Icon(Icons.settings),
tooltip: 'Open settings',
onPressed: () {},
),
),
),
),
);
}The overlay replaces the normal UI with boxes showing each node's label. A button with no label shows up as an empty box, which is easy to spot.
Common errors
- Unlabeled icon buttons. Add
tooltipto everyIconButton,FloatingActionButtonand similar widget. - Bare
CheckboxorSwitchin aRow. Use theListTilevariants, or the control has no name. - Using
hintTextas the only label. UselabelText. - Decorative images read aloud. Set
excludeFromSemantics: true. - Informative images skipped. Set
semanticLabel. - Merged tiles hiding inner buttons. Test tiles with both
onTapand a trailing control. - Custom tappable widgets.
GestureDetectorandInkWellover plain content give no role. Prefer a standard button, or addSemanticsyourself.
Decision rule
Pick the highest-level standard widget that fits. Reach for Semantics only when no standard widget matches, or when the default label is wrong. The order of preference is:
- A standard widget with its built-in labeling property, such as
tooltiporlabelText. - A composite widget that merges parts for you, such as
SwitchListTile. - A standard widget plus a
semanticLabelor formatter callback. - A
Semanticswrapper around a custom widget.
Exercise
A teammate writes Row(children: [Checkbox(value: _agree, onChanged: _setAgree), const Text('I agree to the terms')]). A screen reader user hears only "checkbox, not checked" for the control. Which standard widget fixes this without writing Semantics yourself, and why?
Show answer
Use CheckboxListTile with title: const Text('I agree to the terms'). The tile merges the title and the checkbox into one node, so the reader announces the label together with the role and the checked state. A bare Checkbox has no label source of its own, and the neighboring Text is a separate node.
Exercise
You add IconButton(icon: Icon(Icons.delete), onPressed: _delete) to a list row. TalkBack reads it as just "button". What single property do you add, and what two things does it do?
Show answer
Add tooltip: 'Delete item'. Flutter uses it as the semantics label, so the screen reader announces "Delete item, button". It also shows a visible tooltip on long press or hover for sighted users.