Sub topic 2
Adding labels, roles and hints with Semantics
This chapter assumes you know basic Dart syntax, can build layouts with Row, Column and Text, understand StatelessWidget and StatefulWidget, and can run an app on an emulator or device. Flutter builds a semantics tree that screen readers use, and this chapter shows how to label and group that tree, size tap targets, and handle contrast and text scaling.
Labels, roles and hints
A screen reader announces what it finds in the semantics tree. If a widget has no label, the user hears nothing useful. Many standard widgets set their own semantics, but custom widgets usually need help.
Using the Semantics widget
Semantics attaches a role, label and hint to its child. Use it when a custom tappable widget has no built-in meaning.
Semantics(
button: true,
label: 'Add to favorites',
hint: 'Saves this item to your list',
child: GestureDetector(
onTap: _toggleFavorite,
child: const Icon(Icons.favorite_border),
),
)Using built-in properties
Many widgets accept an accessibility property directly. IconButton uses its tooltip as the label, so prefer it over wrapping the button in Semantics.
IconButton(
icon: const Icon(Icons.favorite_border),
tooltip: 'Add to favorites',
onPressed: _toggleFavorite,
)Use semanticLabel on images. Decorative images should be excluded.
Image.asset(
'assets/team.png',
semanticLabel: 'Four teammates standing around a whiteboard',
)
Image.asset(
'assets/divider.png',
excludeFromSemantics: true,
)Grouping and hiding semantics
Without grouping, a screen reader may read each child of a row separately. MergeSemantics combines the children into one node.
MergeSemantics(
child: Row(
children: [
const Icon(Icons.star),
const Text('4.5 stars'),
],
),
)ExcludeSemantics removes a subtree from the semantics tree. Use it for decoration that repeats information already present, such as a duplicate icon next to a visible label.
Making tap targets large enough
A tap target is the area that responds to touch. Material guidance for Android recommends at least 48 by 48 logical pixels. Apple's Human Interface Guidelines recommend at least 44 by 44 points. Use 48 as the minimum on both platforms.
ConstrainedBox(
constraints: const BoxConstraints(minWidth: 48, minHeight: 48),
child: InkWell(
onTap: _toggleFavorite,
child: const Center(child: Icon(Icons.favorite_border)),
),
)Choosing colors with enough contrast
WCAG 2.1 Level AA requires a contrast ratio of at least 4.5:1 for normal text and 3:1 for large text (18 pt, or 14 pt bold) and for icons and control borders. Never rely on color alone to show state, so pair a color change with an icon or text.
> Note: Check contrast with a tool such as the WebAIM contrast checker before you ship a palette.
Layouts that survive large text
Users can raise their text size in system settings. Fixed heights clip text, so use minimum constraints and let the height grow. Read the scale with MediaQuery.textScalerOf.
final scaler = MediaQuery.textScalerOf(context);
return ConstrainedBox(
constraints: BoxConstraints(minHeight: scaler.scale(48)),
child: const Center(child: Text('Continue')),
);Exercise
An icon-only button has no tooltip, and a screen reader announces it as "unlabeled button". Which property fixes this with the least code, and what should it contain?
Show answer
Set tooltip on the IconButton, for example tooltip: 'Add to favorites'. IconButton uses the tooltip as its accessibility label, so no Semantics wrapper is needed.