Sub topic 5
Adding labels, roles and hints with Semantics
Assume you know basic Dart syntax, Flutter layouts, and how to run a Flutter app. This chapter focuses on the semantics tree: the structured description of your interface that Flutter sends to TalkBack, VoiceOver, and other assistive technologies.
What the Semantics widget does
A Flutter widget draws the interface, but a screen reader needs more information than pixels. It needs to know what an element is, what it is called, and what will happen when the user activates it.
The Semantics widget adds this information. Common properties include:
label: the name announced to the user.button: identifies the element as a button.hint: explains the result of an action.enabled: tells the screen reader whether the element can be used.checkedortoggled: describes the current state of a control.value: reports changing content, such as a slider value.
Standard widgets often provide semantics automatically. For example, an IconButton uses its tooltip as its accessibility label. Prefer standard controls when they fit your design.
import 'package:flutter/material.dart';
class FavoriteExample extends StatefulWidget {
const FavoriteExample({super.key});
@override
State<FavoriteExample> createState() => _FavoriteExampleState();
}
class _FavoriteExampleState extends State<FavoriteExample> {
bool isFavorite = false;
void toggleFavorite() {
setState(() => isFavorite = !isFavorite);
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: const Text('Semantics example')),
body: Center(
child: Semantics(
button: true,
label: isFavorite ? 'Remove from favorites' : 'Add to favorites',
hint: 'Saves this item to your list',
toggled: isFavorite,
child: GestureDetector(
onTap: toggleFavorite,
child: Icon(
isFavorite ? Icons.favorite : Icons.favorite_border,
size: 48,
),
),
),
),
);
}
}The custom gesture has no useful description by itself. Semantics gives it a role, a name, a hint, and its current state. The label changes with the state, so the announcement remains meaningful.
Labels for images and icons
An informative image needs a text alternative. Use semanticLabel when the image communicates information that the surrounding text does not.
Image.asset(
'assets/team.png',
semanticLabel: 'Four teammates standing around a whiteboard',
)A decorative image adds no information. Remove it from the semantics tree:
Image.asset(
'assets/divider.png',
excludeFromSemantics: true,
)Avoid repeating nearby text. If a card already says “Team members,” an image with the same label may cause a screen reader to announce the same idea twice.
Understanding the result
Flutter combines semantics from widgets into a tree. Platform accessibility services consume that tree rather than inspecting your layout directly.
Exercise
Exercise
A custom icon opens a settings page, but TalkBack announces only “unlabeled.” What should you add?
Show answer
Wrap the icon in Semantics with button: true, a label such as “Open settings,” and a hint such as “Shows application settings.”