Sub topic 17
Making Text, Images, and Icons Accessible
This chapter assumes basic Dart syntax, Flutter widgets, StatelessWidget, the build method, and a working Flutter project. It also assumes that you know how to use Text, Image, and Icon.
Accessibility means making an interface usable by people with different abilities, devices, and settings. Flutter supports accessibility through text scaling, semantic information, useful contrast, and controls that assistive technologies can understand.
Supporting text scaling
Users may increase the system font size. Your layout should allow text to grow instead of forcing it into a fixed height.
Avoid placing important text in a SizedBox with a fixed height. Let a Column, ListView, or other parent give the text enough room. Also avoid using a small fixed font size for every screen. Theme text styles respond better to application-wide design choices.
Flutter uses the TextScaler associated with the current MediaQuery. Do not manually reduce text to compensate for large user settings. Test your layout with large accessibility text enabled.
Text is already exposed to the semantics tree, which is the structured description that screen readers use. Write visible text that makes sense when read without the surrounding decoration.
Adding semantic labels to images
An image needs a description when it communicates information. Use semanticLabel to describe its meaning:
import 'package:flutter/material.dart';
class AccessibleCard extends StatelessWidget {
const AccessibleCard({super.key});
@override
Widget build(BuildContext context) {
return Card(
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Image.asset(
'assets/images/hero.png',
width: double.infinity,
height: 180,
fit: BoxFit.cover,
semanticLabel: 'A mountain landscape at sunrise',
),
Padding(
padding: const EdgeInsets.all(16),
child: Text(
'Plan a weekend hike',
style: Theme.of(context).textTheme.titleLarge,
),
),
],
),
);
}
}A decorative image should not add noise to a screen reader. For such an image, use excludeFromSemantics: true:
Image.asset(
'assets/images/texture.png',
excludeFromSemantics: true,
)The label should describe the image's purpose, not every visible detail. For example, “Map of the park trails” is usually more useful than a long list of colors.
Making icons understandable
An icon that communicates information needs a semantic label. An icon that only decorates a labeled control should be excluded or should not duplicate the control's label.
Semantics(
button: true,
label: 'Add this item to favorites',
child: IconButton(
onPressed: () {},
icon: const Icon(Icons.favorite_border),
tooltip: 'Add to favorites',
),
)tooltip helps users who can see the icon. Semantics supplies information for assistive technologies and can identify the element as a button. Avoid labels such as “heart icon” when the real action is “Add to favorites.”
Checking the semantics tree
The following relationship is a useful mental model:
Use a screen reader on an emulator or device to check the order and wording of announcements. Also test large text, high contrast, and landscape orientation. A label cannot fix a layout that clips or overlaps when text grows.
Exercise
An image shows a decorative gradient behind a heading, and an icon button removes an item. Which accessibility changes should you make?
Show answer
Exclude the decorative image from semantics. Give the button a meaningful label such as “Remove item,” and provide a matching tooltip for sighted users.