Skip to content

Commit 335f424

Browse files
authored
[VPAT] Fix semantics in a11y_assessments drawer, radio, and rail demos (flutter#13159)
## Problem Several a11y_assessments demos expose incomplete semantics to screen readers. The navigation drawer header is not marked as a heading, the drawer does not expose which item is selected, the RadioListTile radios are not in a labeled group, and the NavigationRail "Label type" and "Group alignment" controls are neither grouped with their labels nor expose which option is active. ## Fix Mark the navigation drawer header with `headingLevel: 2`, set `ListTile.selected` from the current page, wrap the radios in a `RadioGroup` with a visible group label, and replace the NavigationRail option rows with `SegmentedButton`s wrapped with their labels in a semantics container. The drawer header itself is already a heading since flutter#12918, so it is not changed here. Only the test app changes, so no package version or CHANGELOG update is needed. ## Demo Run the test app and use a screen reader on #/drawer, #/navigation-drawer, #/radio-list-tile, and #/navigation-rail.
1 parent 84f9f8e commit 335f424

9 files changed

Lines changed: 221 additions & 76 deletions

File tree

‎packages/material_ui/test_apps/a11y_assessments/lib/use_cases/drawer.dart‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ class _DrawerExampleState extends State<DrawerExample> {
4949
ListTile(
5050
leading: const Icon(Icons.message),
5151
title: const Text('Messages'),
52+
selected: selectedPage == 'Messages',
5253
onTap: () {
5354
setState(() {
5455
selectedPage = 'Messages';
@@ -58,6 +59,7 @@ class _DrawerExampleState extends State<DrawerExample> {
5859
ListTile(
5960
leading: const Icon(Icons.account_circle),
6061
title: const Text('Profile'),
62+
selected: selectedPage == 'Profile',
6163
onTap: () {
6264
setState(() {
6365
selectedPage = 'Profile';
@@ -67,6 +69,7 @@ class _DrawerExampleState extends State<DrawerExample> {
6769
ListTile(
6870
leading: const Icon(Icons.settings),
6971
title: const Text('Settings'),
72+
selected: selectedPage == 'Settings',
7073
onTap: () {
7174
setState(() {
7275
selectedPage = 'Settings';

‎packages/material_ui/test_apps/a11y_assessments/lib/use_cases/navigation_drawer.dart‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,11 @@ class _NavigationDrawerExampleState extends State<NavigationDrawerExample> {
8383
children: <Widget>[
8484
Padding(
8585
padding: const EdgeInsets.fromLTRB(28, 16, 16, 10),
86-
child: Text('Header', style: Theme.of(context).textTheme.titleSmall),
86+
child: Semantics(
87+
header: true,
88+
headingLevel: 2,
89+
child: Text('Header', style: Theme.of(context).textTheme.titleSmall),
90+
),
8791
),
8892
...destinations.map((ExampleDestination destination) {
8993
return NavigationDrawerDestination(

‎packages/material_ui/test_apps/a11y_assessments/lib/use_cases/navigation_rail.dart‎

Lines changed: 51 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -97,68 +97,59 @@ class _NavRailExampleState extends State<NavRailExample> {
9797
children: <Widget>[
9898
Text('selectedIndex: $_selectedIndex'),
9999
const SizedBox(height: 20),
100-
Text('Label type: ${labelType.name}'),
101-
const SizedBox(height: 10),
102-
OverflowBar(
103-
spacing: 10.0,
104-
children: <Widget>[
105-
ElevatedButton(
106-
onPressed: () {
107-
setState(() {
108-
labelType = NavigationRailLabelType.none;
109-
});
110-
},
111-
child: const Text('None'),
112-
),
113-
ElevatedButton(
114-
onPressed: () {
115-
setState(() {
116-
labelType = NavigationRailLabelType.selected;
117-
});
118-
},
119-
child: const Text('Selected'),
120-
),
121-
ElevatedButton(
122-
onPressed: () {
123-
setState(() {
124-
labelType = NavigationRailLabelType.all;
125-
});
126-
},
127-
child: const Text('All'),
128-
),
129-
],
100+
Semantics(
101+
container: true,
102+
child: Column(
103+
children: <Widget>[
104+
Text('Label type: ${labelType.name}'),
105+
const SizedBox(height: 10),
106+
SegmentedButton<NavigationRailLabelType>(
107+
segments: const <ButtonSegment<NavigationRailLabelType>>[
108+
ButtonSegment<NavigationRailLabelType>(
109+
value: NavigationRailLabelType.none,
110+
label: Text('None'),
111+
),
112+
ButtonSegment<NavigationRailLabelType>(
113+
value: NavigationRailLabelType.selected,
114+
label: Text('Selected'),
115+
),
116+
ButtonSegment<NavigationRailLabelType>(
117+
value: NavigationRailLabelType.all,
118+
label: Text('All'),
119+
),
120+
],
121+
selected: <NavigationRailLabelType>{labelType},
122+
onSelectionChanged: (Set<NavigationRailLabelType> newSelection) {
123+
setState(() {
124+
labelType = newSelection.single;
125+
});
126+
},
127+
),
128+
],
129+
),
130130
),
131131
const SizedBox(height: 20),
132-
Text('Group alignment: $groupAlignment'),
133-
const SizedBox(height: 10),
134-
OverflowBar(
135-
spacing: 10.0,
136-
children: <Widget>[
137-
ElevatedButton(
138-
onPressed: () {
139-
setState(() {
140-
groupAlignment = -1.0;
141-
});
142-
},
143-
child: const Text('Top'),
144-
),
145-
ElevatedButton(
146-
onPressed: () {
147-
setState(() {
148-
groupAlignment = 0.0;
149-
});
150-
},
151-
child: const Text('Center'),
152-
),
153-
ElevatedButton(
154-
onPressed: () {
155-
setState(() {
156-
groupAlignment = 1.0;
157-
});
158-
},
159-
child: const Text('Bottom'),
160-
),
161-
],
132+
Semantics(
133+
container: true,
134+
child: Column(
135+
children: <Widget>[
136+
Text('Group alignment: $groupAlignment'),
137+
const SizedBox(height: 10),
138+
SegmentedButton<double>(
139+
segments: const <ButtonSegment<double>>[
140+
ButtonSegment<double>(value: -1.0, label: Text('Top')),
141+
ButtonSegment<double>(value: 0.0, label: Text('Center')),
142+
ButtonSegment<double>(value: 1.0, label: Text('Bottom')),
143+
],
144+
selected: <double>{groupAlignment},
145+
onSelectionChanged: (Set<double> newSelection) {
146+
setState(() {
147+
groupAlignment = newSelection.single;
148+
});
149+
},
150+
),
151+
],
152+
),
162153
),
163154
const SizedBox(height: 20),
164155
OverflowBar(

‎packages/material_ui/test_apps/a11y_assessments/lib/use_cases/radio_list_tile.dart‎

Lines changed: 23 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -44,21 +44,29 @@ class _MainWidgetState extends State<_MainWidget> {
4444
Widget build(BuildContext context) {
4545
return Scaffold(
4646
appBar: AppBar(title: Semantics(headingLevel: 1, child: Text('$pageTitle Demo'))),
47-
body: ListView(
48-
children: <Widget>[
49-
RadioListTile<SingingCharacter>(
50-
title: const Text('Lafayette'),
51-
value: SingingCharacter.lafayette,
52-
groupValue: _value,
53-
onChanged: _onChanged,
54-
),
55-
RadioListTile<SingingCharacter>(
56-
title: const Text('Jefferson'),
57-
value: SingingCharacter.jefferson,
58-
groupValue: _value,
59-
onChanged: _onChanged,
60-
),
61-
],
47+
body: RadioGroup<SingingCharacter>(
48+
groupValue: _value,
49+
onChanged: _onChanged,
50+
child: Column(
51+
crossAxisAlignment: CrossAxisAlignment.start,
52+
children: <Widget>[
53+
const Padding(padding: EdgeInsets.all(16), child: Text('Singing character')),
54+
Expanded(
55+
child: ListView(
56+
children: const <Widget>[
57+
RadioListTile<SingingCharacter>(
58+
title: Text('Lafayette'),
59+
value: SingingCharacter.lafayette,
60+
),
61+
RadioListTile<SingingCharacter>(
62+
title: Text('Jefferson'),
63+
value: SingingCharacter.jefferson,
64+
),
65+
],
66+
),
67+
),
68+
],
69+
),
6270
),
6371
);
6472
}

‎packages/material_ui/test_apps/a11y_assessments/test/drawer_test.dart‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,4 +27,24 @@ void main() {
2727
await tester.pumpAndSettle();
2828
expect(findHeadingLevelOnes, findsOne);
2929
});
30+
31+
testWidgets('drawer exposes the selected item', (WidgetTester tester) async {
32+
await pumpsUseCase(tester, DrawerUseCase());
33+
34+
final ScaffoldState state = tester.firstState(find.byType(Scaffold));
35+
state.openEndDrawer();
36+
await tester.pumpAndSettle();
37+
38+
await tester.tap(find.text('Profile'));
39+
await tester.pumpAndSettle();
40+
41+
expect(
42+
tester.getSemantics(find.text('Profile')),
43+
containsSemantics(label: 'Profile', hasSelectedState: true, isSelected: true),
44+
);
45+
expect(
46+
tester.getSemantics(find.text('Messages')),
47+
containsSemantics(label: 'Messages', hasSelectedState: true, isSelected: false),
48+
);
49+
});
3050
}

‎packages/material_ui/test_apps/a11y_assessments/test/navigation_drawer_test.dart‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
// found in the LICENSE file.
44

55
import 'package:a11y_assessments/use_cases/navigation_drawer.dart';
6+
import 'package:flutter/semantics.dart';
67
import 'package:flutter_test/flutter_test.dart';
78
import 'package:material_ui/material_ui.dart';
89

@@ -27,4 +28,16 @@ void main() {
2728
await tester.pumpAndSettle();
2829
expect(findHeadingLevelOnes, findsOne);
2930
});
31+
32+
testWidgets('navigation drawer header is a heading', (WidgetTester tester) async {
33+
await pumpsUseCase(tester, NavigationDrawerUseCase());
34+
35+
final ScaffoldState state = tester.firstState(find.byType(Scaffold));
36+
state.openEndDrawer();
37+
await tester.pumpAndSettle();
38+
39+
final SemanticsNode header = tester.getSemantics(find.text('Header'));
40+
expect(header, containsSemantics(isHeader: true));
41+
expect(header.getSemanticsData().headingLevel, 2);
42+
});
3043
}

‎packages/material_ui/test_apps/a11y_assessments/test/navigation_rail_test.dart‎

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
// found in the LICENSE file.
44

55
import 'package:a11y_assessments/use_cases/navigation_rail.dart';
6+
import 'package:flutter/semantics.dart';
67
import 'package:flutter_test/flutter_test.dart';
78
import 'package:material_ui/material_ui.dart';
89

@@ -44,4 +45,57 @@ void main() {
4445
await tester.pump();
4546
expect(findTrailing, findsNothing);
4647
});
48+
49+
testWidgets('navigation rail options are grouped with their labels', (WidgetTester tester) async {
50+
await pumpsUseCase(tester, NavigationRailUseCase());
51+
await tester.pumpAndSettle();
52+
53+
final SemanticsNode labelTypeGroup = find.semantics
54+
.byLabel(RegExp('^Label type'))
55+
.evaluate()
56+
.single;
57+
expect(
58+
descendantSemanticsLabels(labelTypeGroup).where((String label) => label.isNotEmpty),
59+
<String>['None', 'Selected', 'All'],
60+
);
61+
62+
final SemanticsNode groupAlignmentGroup = find.semantics
63+
.byLabel(RegExp('^Group alignment'))
64+
.evaluate()
65+
.single;
66+
expect(
67+
descendantSemanticsLabels(groupAlignmentGroup).where((String label) => label.isNotEmpty),
68+
<String>['Top', 'Center', 'Bottom'],
69+
);
70+
});
71+
72+
testWidgets('navigation rail options expose the selected state', (WidgetTester tester) async {
73+
await pumpsUseCase(tester, NavigationRailUseCase());
74+
await tester.pumpAndSettle();
75+
76+
expect(
77+
tester.getSemantics(find.text('All')),
78+
containsSemantics(hasSelectedState: true, isSelected: true),
79+
);
80+
expect(
81+
tester.getSemantics(find.text('None')),
82+
containsSemantics(hasSelectedState: true, isSelected: false),
83+
);
84+
expect(
85+
tester.getSemantics(find.text('Top')),
86+
containsSemantics(hasSelectedState: true, isSelected: true),
87+
);
88+
89+
await tester.tap(find.text('None'));
90+
await tester.pumpAndSettle();
91+
92+
expect(
93+
tester.getSemantics(find.text('None')),
94+
containsSemantics(hasSelectedState: true, isSelected: true),
95+
);
96+
expect(
97+
tester.getSemantics(find.text('All')),
98+
containsSemantics(hasSelectedState: true, isSelected: false),
99+
);
100+
});
47101
}

‎packages/material_ui/test_apps/a11y_assessments/test/radio_list_tile_test.dart‎

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
// found in the LICENSE file.
44

55
import 'package:a11y_assessments/use_cases/radio_list_tile.dart';
6+
import 'package:flutter/semantics.dart';
67
import 'package:flutter_test/flutter_test.dart';
78

89
import 'test_utils.dart';
@@ -20,4 +21,44 @@ void main() {
2021
await tester.pumpAndSettle();
2122
expect(findHeadingLevelOnes, findsOne);
2223
});
24+
25+
testWidgets('radio buttons are in a labeled radio group', (WidgetTester tester) async {
26+
await pumpsUseCase(tester, RadioListTileUseCase());
27+
await tester.pumpAndSettle();
28+
29+
final SemanticsFinder findRadioGroup = find.semantics.byPredicate(
30+
(SemanticsNode node) => node.getSemanticsData().role == SemanticsRole.radioGroup,
31+
);
32+
expect(findRadioGroup, findsOne);
33+
34+
final SemanticsNode radioGroup = findRadioGroup.evaluate().single;
35+
expect(radioGroup.label, 'Singing character');
36+
expect(descendantSemanticsLabels(radioGroup), containsAll(<String>['Lafayette', 'Jefferson']));
37+
});
38+
39+
testWidgets('radio group controls the selected radio', (WidgetTester tester) async {
40+
await pumpsUseCase(tester, RadioListTileUseCase());
41+
await tester.pumpAndSettle();
42+
43+
expect(
44+
tester.getSemantics(find.text('Lafayette')),
45+
containsSemantics(hasCheckedState: true, isChecked: true),
46+
);
47+
expect(
48+
tester.getSemantics(find.text('Jefferson')),
49+
containsSemantics(hasCheckedState: true, isChecked: false),
50+
);
51+
52+
await tester.tap(find.text('Jefferson'));
53+
await tester.pumpAndSettle();
54+
55+
expect(
56+
tester.getSemantics(find.text('Lafayette')),
57+
containsSemantics(hasCheckedState: true, isChecked: false),
58+
);
59+
expect(
60+
tester.getSemantics(find.text('Jefferson')),
61+
containsSemantics(hasCheckedState: true, isChecked: true),
62+
);
63+
});
2364
}

‎packages/material_ui/test_apps/a11y_assessments/test/test_utils.dart‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
// found in the LICENSE file.
44

55
import 'package:a11y_assessments/use_cases/use_cases.dart';
6+
import 'package:flutter/semantics.dart';
67
import 'package:flutter_test/flutter_test.dart';
78
import 'package:material_ui/material_ui.dart';
89

@@ -17,3 +18,13 @@ Future<void> pumpsUseCase(WidgetTester tester, UseCase useCase) async {
1718
),
1819
);
1920
}
21+
22+
List<String> descendantSemanticsLabels(SemanticsNode node) {
23+
final labels = <String>[];
24+
node.visitChildren((SemanticsNode child) {
25+
labels.add(child.label);
26+
labels.addAll(descendantSemanticsLabels(child));
27+
return true;
28+
});
29+
return labels;
30+
}

0 commit comments

Comments
 (0)