Accessibility in Flutter: solved exercise with Semantics and TalkBack/VoiceOver support

Accessibility in Flutter: solved exercise with Semantics and TalkBack/VoiceOver

Accessibility is a requirement in professional apps and in many markets it is a legal obligation (WCAG 2.1, EN 301 549). Flutter provides the Semantics widget to describe the UI to screen readers (TalkBack on Android, VoiceOver on iOS) without changing the visual appearance.

Problem statement

Build a screen that:

  • Uses Semantics to label buttons, images, and icons with useful descriptions.
  • Uses MergeSemantics to group related elements.
  • Uses ExcludeSemantics to hide irrelevant decorations.
  • Implements correct focus traversal with FocusTraversalGroup.
  • Respects the system textScaleFactor (large fonts).
  • Passes Flutter DevTools accessibility checklist.

Dependencies

None additional โ€” everything is in the Flutter SDK.

Full solution

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
import 'package:flutter/material.dart';

void main() => runApp(const MaterialApp(home: AccessibilityDemo()));

class AccessibilityDemo extends StatefulWidget {
  const AccessibilityDemo({super.key});
  @override
  State<AccessibilityDemo> createState() => _AccessibilityDemoState();
}

class _AccessibilityDemoState extends State<AccessibilityDemo> {
  bool _liked = false;
  bool _subscribed = false;
  int _counter = 0;
  double _volume = 0.5;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: const Text('Accessibility in Flutter')),
      body: ListView(
        padding: const EdgeInsets.all(16),
        children: [
          // โ”€โ”€ 1. Image with semantics โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
          _SectionTitle('1. Images with semantic description'),
          const SizedBox(height: 8),
          Semantics(
            label: 'Illustration of a mobile phone running the Flutter app',
            image: true,
            child: Container(
              height: 120,
              decoration: BoxDecoration(
                color: Colors.blue.shade100,
                borderRadius: BorderRadius.circular(12),
              ),
              child: const Center(
                child: Icon(Icons.phone_android, size: 64, color: Colors.blue),
              ),
            ),
          ),
          const SizedBox(height: 4),
          // Decorative text โ†’ exclude from semantics
          ExcludeSemantics(
            child: Text(
              'โ˜…โ˜…โ˜…โ˜…โ˜…',
              style: TextStyle(color: Colors.amber.shade600, fontSize: 18),
            ),
          ),

          const SizedBox(height: 24),

          // โ”€โ”€ 2. Button with custom semantic action โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
          _SectionTitle('2. Button with state and semantics'),
          const SizedBox(height: 8),
          Semantics(
            label: _liked ? 'Unlike' : 'Like',
            hint: 'Double tap to ${_liked ? 'unlike' : 'like'}',
            button: true,
            checked: _liked,
            child: GestureDetector(
              onTap: () => setState(() => _liked = !_liked),
              child: AnimatedContainer(
                duration: const Duration(milliseconds: 200),
                padding: const EdgeInsets.symmetric(horizontal: 16, vertical: 10),
                decoration: BoxDecoration(
                  color: _liked ? Colors.red.shade50 : Colors.grey.shade100,
                  borderRadius: BorderRadius.circular(24),
                  border: Border.all(
                    color: _liked ? Colors.red : Colors.grey.shade300,
                  ),
                ),
                child: Row(
                  mainAxisSize: MainAxisSize.min,
                  children: [
                    Icon(
                      _liked ? Icons.favorite : Icons.favorite_border,
                      color: _liked ? Colors.red : Colors.grey,
                    ),
                    const SizedBox(width: 8),
                    Text(_liked ? 'Liked' : 'Like'),
                  ],
                ),
              ),
            ),
          ),

          const SizedBox(height: 24),

          // โ”€โ”€ 3. MergeSemantics: group text + image โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
          _SectionTitle('3. MergeSemantics: product card'),
          const SizedBox(height: 8),
          MergeSemantics(
            child: Card(
              child: ListTile(
                leading: ExcludeSemantics(
                  child: CircleAvatar(
                    backgroundColor: Colors.green.shade100,
                    child: const Icon(Icons.book, color: Colors.green),
                  ),
                ),
                title: const Text('Flutter in Depth'),
                subtitle: const Text('Book ยท \$29.99'),
                trailing: Semantics(
                  label: _subscribed ? 'Bookmarked' : 'Bookmark',
                  button: true,
                  child: IconButton(
                    icon: Icon(
                      _subscribed ? Icons.bookmark : Icons.bookmark_border,
                      color: _subscribed ? Colors.green : null,
                    ),
                    onPressed: () => setState(() => _subscribed = !_subscribed),
                  ),
                ),
              ),
            ),
          ),

          const SizedBox(height: 24),

          // โ”€โ”€ 4. Counter with value semantics โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
          _SectionTitle('4. Accessible counter'),
          const SizedBox(height: 8),
          Semantics(
            label: 'Counter',
            value: '$_counter',
            child: Row(
              mainAxisAlignment: MainAxisAlignment.center,
              children: [
                Semantics(
                  label: 'Decrement counter',
                  button: true,
                  child: IconButton.filled(
                    onPressed: () => setState(() => _counter--),
                    icon: const Icon(Icons.remove),
                  ),
                ),
                Padding(
                  padding: const EdgeInsets.symmetric(horizontal: 24),
                  child: Text(
                    '$_counter',
                    style: Theme.of(context).textTheme.displaySmall,
                  ),
                ),
                Semantics(
                  label: 'Increment counter',
                  button: true,
                  child: IconButton.filled(
                    onPressed: () => setState(() => _counter++),
                    icon: const Icon(Icons.add),
                  ),
                ),
              ],
            ),
          ),

          const SizedBox(height: 24),

          // โ”€โ”€ 5. Accessible slider โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
          _SectionTitle('5. Slider with value semantics'),
          const SizedBox(height: 8),
          Semantics(
            label: 'Volume control',
            value: '${(_volume * 100).round()} percent',
            increasedValue: '${((_volume + 0.1).clamp(0, 1) * 100).round()} percent',
            decreasedValue: '${((_volume - 0.1).clamp(0, 1) * 100).round()} percent',
            child: Slider(
              value: _volume,
              onChanged: (v) => setState(() => _volume = v),
              label: '${(_volume * 100).round()}%',
              divisions: 10,
            ),
          ),

          const SizedBox(height: 24),

          // โ”€โ”€ 6. Focus traversal โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
          _SectionTitle('6. Correct focus order'),
          const SizedBox(height: 8),
          FocusTraversalGroup(
            policy: OrderedTraversalPolicy(),
            child: Column(
              children: [
                _FocusableField(order: 1, label: 'Name', hint: 'Enter your name'),
                const SizedBox(height: 8),
                _FocusableField(order: 2, label: 'Email', hint: 'name@example.com'),
                const SizedBox(height: 8),
                _FocusableField(order: 3, label: 'Password', hint: 'Minimum 8 characters', obscure: true),
                const SizedBox(height: 8),
                FocusTraversalOrder(
                  order: const NumericFocusOrder(4),
                  child: FilledButton(
                    onPressed: () {},
                    child: const Text('Create account'),
                  ),
                ),
              ],
            ),
          ),

          const SizedBox(height: 24),

          // โ”€โ”€ 7. Adaptive text scale โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
          _SectionTitle('7. Adaptive text scale'),
          const SizedBox(height: 8),
          Builder(builder: (context) {
            final textScale = MediaQuery.textScalerOf(context).scale(1.0);
            return Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                Text('Current scale: ${textScale.toStringAsFixed(2)}x'),
                const SizedBox(height: 4),
                Text(
                  'This text respects the user\'s font size preferences.',
                  style: Theme.of(context).textTheme.bodyLarge,
                ),
                Text(
                  'Text with scale capped at 1.5x (exceptional case)',
                  style: Theme.of(context).textTheme.bodyMedium,
                  textScaler: TextScaler.linear(textScale.clamp(0.8, 1.5)),
                ),
              ],
            );
          }),
        ],
      ),
    );
  }
}

class _FocusableField extends StatelessWidget {
  final int order;
  final String label;
  final String hint;
  final bool obscure;

  const _FocusableField({
    required this.order, required this.label,
    required this.hint, this.obscure = false,
  });

  @override
  Widget build(BuildContext context) {
    return FocusTraversalOrder(
      order: NumericFocusOrder(order.toDouble()),
      child: Semantics(
        label: label,
        hint: hint,
        textField: true,
        child: TextField(
          obscureText: obscure,
          decoration: InputDecoration(
            labelText: label,
            hintText: hint,
            border: const OutlineInputBorder(),
          ),
        ),
      ),
    );
  }
}

class _SectionTitle extends StatelessWidget {
  final String text;
  const _SectionTitle(this.text);
  @override
  Widget build(BuildContext context) => Text(
        text,
        style: Theme.of(context)
            .textTheme
            .titleSmall
            ?.copyWith(color: Theme.of(context).colorScheme.primary),
      );
}

Flutter accessibility checklist

AspectWidget / Practice
Descriptive imageSemantics(label: '...', image: true)
Button with stateSemantics(button: true, checked: bool)
Group elementsMergeSemantics
Hide decorationsExcludeSemantics
Readable slider valueSemantics(value: '50 percent')
Focus orderFocusTraversalGroup + OrderedTraversalPolicy
Adaptive fontRespect MediaQuery.textScalerOf(context)
Contrastโ‰ฅ 4.5:1 for normal text (WCAG AA)

Common mistakes

  • No label on images: screen readers announce “image” with no context. Always add Semantics(label: '...', image: true) to images that convey information.
  • Icon-only buttons without a label: IconButton without tooltip is unreadable for TalkBack. Add tooltip or wrap in Semantics(label: '...').
  • Restricting textScaleFactor unnecessarily: blocking font scale is an accessibility barrier. Only do it in extreme cases and always allow at least 1.3x.

Practical use

Accessibility is mandatory in government, banking, and healthcare apps in the EU and USA. You can verify your app with the Accessibility Inspector (iOS) or Android’s TalkBack. Flutter DevTools includes the semantics panel.

Guided practice and next step

FAQ

How do I test accessibility without a real device?

On Android you can enable TalkBack on the emulator. On iOS, enable VoiceOver on the simulator with Cmd+F5. Flutter DevTools also shows the semantic tree in the Widget Inspector tab.

Does Semantics affect performance?

The impact is minimal. The semantic tree is only computed when an accessibility service is active on the device. It does not affect performance in normal use.

Does WCAG apply to mobile apps?

WCAG 2.1 guidelines extend to mobile apps and are the basis for standards like EN 301 549 (Europe) and Section 508 (USA). The most relevant criteria are color contrast (4.5:1 for normal text) and minimum touch target size (44ร—44 dp).