Sub topic 6
Grouping and Hiding Semantics
This chapter assumes basic Dart syntax, Flutter layouts, and familiarity with StatelessWidget and StatefulWidget. You should also know how to run an app on an emulator or device.
Flutter builds a semantics tree alongside the widget tree. This tree describes controls, text, images, and actions to services such as TalkBack and VoiceOver. A screen reader usually focuses one semantics node at a time. The way you group or hide nodes changes what the user hears and how they interact with the UI.
Grouping related content with MergeSemantics
Use MergeSemantics when several descendants represent one meaningful item. For example, an icon and its rating can be announced as one row instead of as two unrelated elements.
MergeSemantics(
child: Row(
children: [
const Icon(Icons.star),
const SizedBox(width: 8),
const Text('4.5 stars'),
],
),
)A screen reader can now treat the row as one semantics node and announce “star, 4.5 stars.” Without merging, it may focus the icon and text separately.
Use merging for compact information such as:
- An icon and its label.
- A leading status indicator and a status message.
- A product name and its short description.
- A checkbox-like row whose text and control form one interaction.
Merging does not combine visual widgets. It changes only their accessibility representation.
Hiding decorative content with ExcludeSemantics
Some content helps the visual design but adds no information. Decorative dividers, repeated background icons, and purely ornamental images should not interrupt screen-reader navigation.
ExcludeSemantics(
child: Image.asset(
'assets/divider.png',
),
)ExcludeSemantics removes the child’s semantics from the tree while leaving the child visible and interactive visually. Do not use it to hide information that a screen-reader user needs.
For an informative image, provide a description instead:
Image.asset(
'assets/team.png',
semanticLabel: 'Four teammates standing around a whiteboard',
)| Widget | Effect on accessibility | Typical use |
|---|---|---|
| `MergeSemantics` | Combines descendant nodes | One logical item made from several widgets |
| `ExcludeSemantics` | Removes descendant semantics | Decorative or duplicated content |
Choosing between merging and excluding
Ask what a user needs to understand or operate the screen.
A common error is merging a subtree that contains controls needing independent focus. A row with a checkbox and a separate “more options” button should usually remain separate. Otherwise, the user may lose access to one of the actions.
Another error is excluding a child and forgetting to provide equivalent information elsewhere. If an image contains the only description of a chart, hiding it makes the chart inaccessible.
> callout note > Test the spoken result, not only the widget structure. A grouping that looks tidy in code can make navigation confusing when read aloud.
Check the result
Inspect the screen with TalkBack on Android or VoiceOver on iOS. Move through the page one element at a time. Confirm that each meaningful item is announced once, decorative content is skipped, and every action remains reachable.
Exercise
A card contains a product image, product name, price, and a separate favorite button. Should the whole card use MergeSemantics?
Show answer
No. Keep the favorite button separate so it remains independently focusable. Merge only content that forms one logical, non-interactive item.