Sub topic 17
Making text, images, and icons accessible
This chapter covers how Flutter exposes Text, Image, and Icon to assistive technology such as screen readers, and how to keep them usable when users change their font settings. It assumes you can write a StatelessWidget, you know Theme.of(context).textTheme, and you have seen the Image and Icon constructors from earlier chapters.
How Flutter describes your UI to assistive tools
Flutter does not use native views, so the operating system cannot inspect it directly. Flutter builds a semantics tree, a parallel tree of nodes that carry a label, a role, and available actions. TalkBack on Android and VoiceOver on iOS read this tree.
Most built-in widgets add semantics for you. Text contributes its string as a label. Icon and Image contribute a label only if you give one. Without it, a screen reader skips them or reads nothing useful.
Text scaling
Many users raise the system font size. Flutter reads that setting and applies a text scale factor to every Text that has no override. A font size of 16 with a scale of 1.5 renders at 24 logical pixels.
Read the current scaling with MediaQuery:
import 'package:flutter/material.dart';
class ScaleReport extends StatelessWidget {
const ScaleReport({super.key});
@override
Widget build(BuildContext context) {
final scaler = MediaQuery.textScalerOf(context);
final scaled = scaler.scale(16);
return Text('A 16 px font renders at $scaled px');
}
}TextScaler is the current API. The older textScaleFactor is deprecated, so prefer textScalerOf and TextScaler in new code.
Layouts that survive large text
Large text breaks layouts that assume fixed sizes. These practices help:
- Avoid fixed heights on containers that hold text. Let the content decide, or use
minHeightconstraints. - Allow wrapping. Do not set
maxLines: 1on text that carries essential meaning. - Put long rows inside
FlexibleorExpanded, or switch to aWrap, so text does not overflow. - Wrap whole screens in a scroll view so tall content stays reachable.
Limiting scaling
Sometimes a widget such as a tab badge cannot grow. You can override the scaler for a subtree:
import 'package:flutter/material.dart';
class CappedText extends StatelessWidget {
const CappedText({super.key});
@override
Widget build(BuildContext context) {
return MediaQuery(
data: MediaQuery.of(context).copyWith(
textScaler: MediaQuery.textScalerOf(context)
.clamp(maxScaleFactor: 1.3),
),
child: const Text('NEW'),
);
}
}clamp keeps the user's preference but sets an upper bound. Setting TextScaler.noScaling ignores the preference entirely.
Labeling images
Image has a semanticLabel parameter. It sets the text a screen reader speaks. Write what the image conveys, not "image of".
import 'package:flutter/material.dart';
class Hero extends StatelessWidget {
const Hero({super.key});
@override
Widget build(BuildContext context) {
return Image.asset(
'assets/images/hero.png',
width: 300,
height: 180,
fit: BoxFit.cover,
semanticLabel: 'Mountain landscape at sunrise',
);
}
}The screen reader already announces the node as an image, so a label such as "Photo of a mountain" repeats itself.
Decorative images
Some images add nothing, such as a background flourish or a picture next to text that already says the same thing. Mark them with excludeFromSemantics: true. Screen readers then skip them instead of announcing noise.
import 'package:flutter/material.dart';
class Divider extends StatelessWidget {
const Divider({super.key});
@override
Widget build(BuildContext context) {
return Image.asset(
'assets/images/flourish.png',
excludeFromSemantics: true,
);
}
}If you set both semanticLabel and excludeFromSemantics, the exclusion wins and the label is never spoken.
Labeling icons
Icon takes a semanticLabel too. An icon with no label is silent, which is acceptable when it sits beside text, and a problem when it stands alone.
import 'package:flutter/material.dart';
class FavoriteIcon extends StatelessWidget {
const FavoriteIcon({super.key});
@override
Widget build(BuildContext context) {
return const Icon(
Icons.favorite,
color: Colors.red,
size: 32,
semanticLabel: 'Favorite',
);
}
}Icon buttons use tooltip
For tappable icons, label the button instead of the icon. IconButton has a tooltip that doubles as the accessible name, and it shows a hint on long press or hover.
import 'package:flutter/material.dart';
class DeleteButton extends StatelessWidget {
const DeleteButton({super.key, required this.onPressed});
final VoidCallback onPressed;
@override
Widget build(BuildContext context) {
return IconButton(
icon: const Icon(Icons.delete),
tooltip: 'Delete item',
onPressed: onPressed,
);
}
}Do not label both the icon and the button with the same text. The reader would speak it twice.
The Semantics widget
When a built-in label is not enough, wrap content in Semantics. It lets you set a label, mark a node as a button or header, or hide children.
import 'package:flutter/material.dart';
class SectionTitle extends StatelessWidget {
const SectionTitle({super.key});
@override
Widget build(BuildContext context) {
return Semantics(
header: true,
child: Text(
'Settings',
style: Theme.of(context).textTheme.titleLarge,
),
);
}
}Marking headings with header: true lets users jump between sections. Two related widgets cover other cases:
| Widget | Effect |
|---|---|
| `Semantics` | Adds or overrides label, role, and actions on its child |
| `ExcludeSemantics` | Hides the whole subtree from screen readers |
| `MergeSemantics` | Combines child nodes into one announcement |
MergeSemantics is useful for an icon next to a value. Without it, a reader stops twice. With it, the user hears one phrase.
import 'package:flutter/material.dart';
class Rating extends StatelessWidget {
const Rating({super.key});
@override
Widget build(BuildContext context) {
return Semantics(
label: 'Rating: 4.5 out of 5 stars',
child: const ExcludeSemantics(
child: Row(
children: [
Icon(Icons.star),
SizedBox(width: 4),
Text('4.5'),
],
),
),
);
}
}This pattern replaces two fragments, a bare star and "4.5", with one clear sentence.
Contrast and tap size
Two more checks apply to all three widget types:
- Contrast. WCAG asks for a contrast ratio of at least 4.5:1 for normal text and 3:1 for large text and icons. Light gray on white often fails.
- Tap targets. Material guidelines set a minimum of 48 by 48 logical pixels for tappable items.
IconButtonmeets this by default, so avoid shrinking it with tightconstraintsunless you pad it back.
Testing accessibility
Test with a real screen reader at least once: TalkBack on Android and VoiceOver on iOS. Flutter also offers automated checks in widget tests:
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
void main() {
testWidgets('icon button meets guidelines', (tester) async {
final handle = tester.ensureSemantics();
await tester.pumpWidget(
MaterialApp(
home: Scaffold(
body: IconButton(
icon: const Icon(Icons.delete),
tooltip: 'Delete item',
onPressed: () {},
),
),
),
);
await expectLater(tester, meetsGuideline(labeledTapTargetGuideline));
await expectLater(tester, meetsGuideline(androidTapTargetGuideline));
handle.dispose();
});
}labeledTapTargetGuideline fails if a tappable node has no label. androidTapTargetGuideline checks the 48 pixel minimum. The debugShowSemanticsTree-style inspection in Flutter DevTools, and the Show semantics option in the widget inspector, let you see what a screen reader receives.
Common mistakes
- Leaving a standalone
Iconor informativeImagewithout a label. - Labeling a decorative image, so users hear clutter.
- Setting
maxLines: 1with ellipsis on essential text, which hides content at large scales. - Using a fixed
SizedBoxheight around text. - Labeling both an
IconButtonand its innerIcon. - Disabling text scaling globally to "fix" a layout.
Exercise
A card shows a decorative photo of a coffee cup above the heading "Morning Roast", and a row with a star icon and the text "4.8". How would you make this card work well with a screen reader?
Show answer
Mark the photo with excludeFromSemantics: true, since the heading already conveys the topic. Wrap the heading in Semantics(header: true) so users can navigate to it. For the rating, wrap the star and "4.8" in Semantics with a label such as "Rating: 4.8 out of 5 stars" and place the Row inside ExcludeSemantics, so the reader speaks one clear sentence instead of an unlabeled icon and a bare number.
Exercise
A tab badge reading "NEW" overflows its fixed 20 px box when users set a large system font. What are two ways to fix it, and which is preferable?
Show answer
You can clamp the scaler for the badge, for example with MediaQuery and textScaler.clamp(maxScaleFactor: 1.3), or remove the fixed height so the badge grows with the text. Removing the fixed size is preferable, because clamping still overrides the user's preference. Clamping is acceptable only for small decorative elements like this one.