Sub topic 11
Announcing changes as they happen
This chapter assumes you know the basics of the semantics tree, the Semantics widget, and how to turn on TalkBack or VoiceOver. A screen reader user hears the part of the screen they have focused. When something changes elsewhere, such as a counter updating, a form error appearing or a download finishing, they have no way to notice it unless the app tells them. This chapter covers the two tools Flutter gives you for that: live regions and explicit announcements.
The problem: silent updates
A sighted user sees a cart badge change from 2 to 3. A screen reader user whose focus is on the "Add to cart" button hears nothing, because the badge is not focused and its text changed without a user action on it.
Visual changes that need an audio equivalent include:
- A result count that updates while the user types in a search field.
- A validation error that appears under a field.
- A status line such as "Uploading... 40%" or "Saved".
- A short-lived message that disappears on its own.
Live regions
A live region is a semantics node that the platform watches. When its label or value changes, the screen reader announces the new text without moving focus. You mark a node as live with liveRegion: true:
import 'package:flutter/material.dart';
void main() => runApp(const MaterialApp(home: CartPage()));
class CartPage extends StatefulWidget {
const CartPage({super.key});
@override
State<CartPage> createState() => _CartPageState();
}
class _CartPageState extends State<CartPage> {
int _count = 0;
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: const Text('Shop')),
body: Center(
child: Column(
mainAxisSize: MainAxisSize.min,
children: [
Semantics(
liveRegion: true,
child: Text('Items in cart: $_count'),
),
const SizedBox(height: 16),
ElevatedButton(
onPressed: () => setState(() => _count++),
child: const Text('Add item'),
),
],
),
),
);
}
}The Semantics widget takes its label from the Text child. Each time the user presses "Add item", the text changes, and TalkBack or VoiceOver reads "Items in cart: 1", then "Items in cart: 2", and so on. Focus stays on the button, so the user can press it again right away.
The sequence below shows what happens on each update.
What counts as a change
The platform announces a live region when its text changes while the node is already on screen. Keep these rules in mind:
- The live region should exist before the change. A node that appears for the first time may or may not be announced, depending on the platform and screen reader version.
- Only the label, value or hint of that node matters. Changing its color or position produces no announcement.
- If
buildruns again and the text is identical, nothing is announced. - Several quick changes can interrupt each other. The screen reader may skip the earlier ones.
Keep live regions small
A live region announces its whole label each time. If you wrap a large container, every small change makes the screen reader read everything inside it.
// Too broad: any change inside re-announces the entire page content.
Semantics(
liveRegion: true,
child: Column(children: [header, list, footer]),
)
// Better: only the status line is live.
Column(
children: [
header,
list,
Semantics(
liveRegion: true,
child: Text(statusMessage),
),
],
)Use one small live region per kind of status. A page with five live regions that all update on every keystroke is noisy and tiring to use.
Explicit announcements
Sometimes there is no persistent widget to mark as live. A file finishes uploading and you only want to say "Upload complete" once. For this, use SemanticsService:
import 'package:flutter/material.dart';
import 'package:flutter/semantics.dart';
void main() => runApp(const MaterialApp(home: UploadPage()));
class UploadPage extends StatefulWidget {
const UploadPage({super.key});
@override
State<UploadPage> createState() => _UploadPageState();
}
class _UploadPageState extends State<UploadPage> {
bool _busy = false;
Future<void> _upload() async {
setState(() => _busy = true);
await Future<void>.delayed(const Duration(seconds: 2));
if (!mounted) return;
setState(() => _busy = false);
SemanticsService.announce(
'Upload complete',
Directionality.of(context),
);
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: Center(
child: ElevatedButton(
onPressed: _busy ? null : _upload,
child: Text(_busy ? 'Uploading' : 'Upload file'),
),
),
);
}
}SemanticsService.announce takes the message and a text direction, which tells the platform how to read it. It sends the message straight to the screen reader and does nothing visible. If no screen reader is running, nothing happens.
Choosing between the two
| Situation | Better tool | Reason |
|---|---|---|
| A counter, status line or total that stays on screen | `liveRegion: true` | The label is the single source of truth, and it is read on every change. |
| A one-time event with no widget, such as "Upload complete" | `SemanticsService.announce` | There is nothing persistent to mark as live. |
| A short-lived message such as a snack bar | Usually the widget itself | Material's `SnackBar` marks its own content as a live region, so check before adding your own. |
| A change the user caused by activating the focused control | Often neither | Updating the control's own label, such as "Mute" to "Unmute", is usually clearer. |
Common mistakes
- Announcing twice. If a live region already speaks "Saved", also calling
announce('Saved')makes the user hear it twice. Pick one. - Announcing too often. A progress bar that announces every percent floods the speech queue. Announce milestones such as 25%, 50% and 100%, or on completion.
- Long messages. The user cannot skip an announcement easily. Keep it short and put the important word first.
- Announcing before the UI is ready. Call
announceaftersetStateand after anymountedcheck, so the spoken text matches what is on screen. - Relying on color. A red border on an invalid field says nothing to a screen reader. Put the error in text and make that text live.
- Skipping real-device tests. Behavior differs between TalkBack and VoiceOver, and between OS versions. Test on both.
Testing live regions
You can check the flag in a widget test without a screen reader. Enable semantics, find the node and check its flag:
import 'package:flutter/material.dart';
import 'package:flutter/semantics.dart';
import 'package:flutter_test/flutter_test.dart';
void main() {
testWidgets('status text is a live region', (tester) async {
final handle = tester.ensureSemantics();
await tester.pumpWidget(
const MaterialApp(
home: Scaffold(
body: Semantics(
liveRegion: true,
child: Text('Saved'),
),
),
),
);
final node = tester.getSemantics(find.text('Saved'));
expect(node.hasFlag(SemanticsFlag.isLiveRegion), isTrue);
handle.dispose();
});
}This test proves the flag is set. It does not prove how TalkBack or VoiceOver will speak the text, so a manual pass is still needed.
Manual checklist
- Turn on TalkBack or VoiceOver and put focus on the control that triggers the change.
- Activate the control and listen. You should hear the new status once.
- Repeat quickly several times. Check that the announcements are not cut off in a confusing way.
- Move focus to the live region itself. It should read as a normal text element with its current value.
Exercise
A download screen shows a progress bar and the text "Downloading 37%" that updates every 100 ms. A screen reader user hears constant speech and cannot use the rest of the screen. How would you fix it, and which tool would you use for the final result?
Show answer
Remove liveRegion from the percentage text, or throttle what is announced, so the text is not read on every change. Announce only milestones such as 25%, 50% and 75%, or announce nothing during the download. When the download finishes, use SemanticsService.announce (or sendAnnouncement in newer Flutter versions) with a short message like "Download complete", or switch a small separate live region to that text once. The progress bar itself can still expose its value through its own semantics for users who focus it.