Sub topic 14
Finding Widgets by Semantics
Assume you know basic Dart, Flutter widgets, and flutter_test. This chapter focuses on semantic labels, which describe what a control means to a user rather than how it is implemented.
Why semantics matter
Flutter builds a semantics tree for accessibility services such as screen readers. A semantics node is a description of a screen element, including information such as:
- Its label, such as
Increase count - Its role, such as button or text field
- Whether it is enabled
- The action a user can perform
A visible icon does not always provide a useful accessible name. A test that finds an icon by its visual implementation can pass even when a screen reader cannot identify the control.
A semantic finder makes the test closer to how an accessibility user understands the interface:
final increaseButton = find.bySemanticsLabel('Increase count');The finder searches the semantics tree, not the visible text tree.
Adding a semantic label
Use Semantics when a widget needs a specific accessible description. The button property tells assistive technology what kind of control it is.
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
class Counter extends StatefulWidget {
const Counter({super.key});
@override
State<Counter> createState() => _CounterState();
}
class _CounterState extends State<Counter> {
int _count = 0;
@override
Widget build(BuildContext context) {
return Scaffold(
body: Center(child: Text('$_count')),
floatingActionButton: Semantics(
label: 'Increase count',
button: true,
child: IconButton(
onPressed: () => setState(() => _count++),
icon: const Icon(Icons.add),
),
),
);
}
}
void main() {
testWidgets('finds the increment control by its semantic label',
(tester) async {
final semanticsHandle = tester.ensureSemantics();
addTearDown(semanticsHandle.dispose);
await tester.pumpWidget(const MaterialApp(home: Counter()));
final increaseButton = find.bySemanticsLabel('Increase count');
expect(increaseButton, findsOneWidget);
await tester.tap(increaseButton);
await tester.pump();
expect(find.text('1'), findsOneWidget);
});
}ensureSemantics enables semantics during the test. The returned handle keeps semantics enabled until it is disposed. addTearDown guarantees cleanup even if an assertion fails.
Choosing a semantic finder
| Finder | Best use |
|---|---|
| `find.bySemanticsLabel('Save')` | Find a control by its accessible name |
| `find.bySemanticsLabel(RegExp(r'^Save'))` | Match a changing or partially known label |
| `find.byKey(const Key('save'))` | Target an implementation-specific test hook |
| `find.byIcon(Icons.save)` | Target a particular visual icon |
Prefer a semantic finder for user-facing controls when the label is part of the intended accessibility behavior. Use a key when the widget has no useful semantic label or when you need to distinguish identical controls.
A semantic label can change because of state. For example, a play button might expose Play before activation and Pause afterward. Test the label that users should hear in each state.
Exercise
Why might find.byIcon(Icons.add) be less useful than find.bySemanticsLabel('Increase count') for an accessibility-focused test?
Show answer
The icon finder checks the visual implementation, while the semantic finder checks the name exposed to assistive technology. The semantic test can detect a missing or incorrect accessible label.