Skip to content

Commit cc93a31

Browse files
authored
[VPAT] Fix contrast and reflow in three a11y_assessments demos (flutter#13409)
## Problem Three a11y_assessments demos fail basic accessibility checks. The badge label is white on `Colors.green`, a 2.8:1 contrast ratio, and the drawer header is white on `Colors.blue`, 3.1:1, where small text needs 4.5:1. The expansion tile page lays its tiles out in a `Column`, so at a 320x256 viewport the second and third tiles overflow and cannot be reached. ## Fix The badge drops its hardcoded colors for the theme defaults, `error` on `onError`, and the drawer header uses the theme's `primary` and `onPrimary` pair. Both pairs meet 4.5:1 in all four of the app's themes. The expansion tile body becomes a `ListView`, so the page scrolls instead of overflowing. Each change has a regression test that failed before it. Only the test app changes, so no package version or CHANGELOG update is needed. ## Demo Run the test app on web. Check the badge and drawer header colors with the DevTools color picker, and the expansion tile page in device mode at 320x256.
1 parent f895ad5 commit cc93a31

7 files changed

Lines changed: 70 additions & 10 deletions

File tree

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

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -38,12 +38,7 @@ class MainWidgetState extends State<MainWidget> {
3838
appBar: AppBar(title: Semantics(headingLevel: 1, child: Text('$pageTitle Demo'))),
3939
body: const Center(
4040
child: Badge(
41-
label: Text(
42-
'5',
43-
semanticsLabel: '5 new messages',
44-
style: TextStyle(color: Colors.white),
45-
),
46-
backgroundColor: Colors.green,
41+
label: Text('5', semanticsLabel: '5 new messages'),
4742
child: Icon(Icons.mail, semanticLabel: 'Messages'),
4843
),
4944
),

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,15 +36,16 @@ class _DrawerExampleState extends State<DrawerExample> {
3636

3737
@override
3838
Widget build(BuildContext context) {
39+
final ColorScheme colors = Theme.of(context).colorScheme;
3940
return Scaffold(
4041
appBar: AppBar(title: Semantics(headingLevel: 1, child: Text('$pageTitle Demo'))),
4142
endDrawer: Drawer(
4243
child: ListView(
4344
padding: EdgeInsets.zero,
4445
children: <Widget>[
45-
const DrawerHeader(
46-
decoration: BoxDecoration(color: Colors.blue),
47-
child: Text('Drawer Header', style: TextStyle(color: Colors.white, fontSize: 24)),
46+
DrawerHeader(
47+
decoration: BoxDecoration(color: colors.primary),
48+
child: Text('Drawer Header', style: TextStyle(color: colors.onPrimary, fontSize: 24)),
4849
),
4950
ListTile(
5051
leading: const Icon(Icons.message),

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ class _ExpansionTileExampleState extends State<ExpansionTileExample> {
3838
Widget build(BuildContext context) {
3939
return Scaffold(
4040
appBar: AppBar(title: Semantics(headingLevel: 1, child: Text('$pageTitle Demo'))),
41-
body: Column(
41+
body: ListView(
4242
children: <Widget>[
4343
const ExpansionTile(
4444
title: Text('ExpansionTile 1'),

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
import 'package:a11y_assessments/use_cases/badge.dart';
66
import 'package:flutter_test/flutter_test.dart';
7+
import 'package:material_ui/material_ui.dart';
78

89
import 'test_utils.dart';
910

@@ -20,4 +21,16 @@ void main() {
2021
await tester.pumpAndSettle();
2122
expect(findHeadingLevelOnes, findsOne);
2223
});
24+
25+
testWidgets('badge label meets the 4.5:1 text contrast ratio', (WidgetTester tester) async {
26+
await pumpsUseCase(tester, BadgeUseCase());
27+
28+
final Container background = tester.widget<Container>(
29+
find.descendant(of: find.byType(Badge), matching: find.byType(Container)),
30+
);
31+
final Color backgroundColor = (background.decoration! as ShapeDecoration).color!;
32+
final Color textColor = paintedTextColor(tester, find.text('5'));
33+
34+
expect(contrastRatio(textColor, backgroundColor), greaterThanOrEqualTo(4.5));
35+
});
2336
}

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,4 +47,18 @@ void main() {
4747
containsSemantics(label: 'Messages', hasSelectedState: true, isSelected: false),
4848
);
4949
});
50+
51+
testWidgets('drawer header meets the 4.5:1 text contrast ratio', (WidgetTester tester) async {
52+
await pumpsUseCase(tester, DrawerUseCase());
53+
54+
final ScaffoldState state = tester.firstState(find.byType(Scaffold));
55+
state.openEndDrawer();
56+
await tester.pumpAndSettle();
57+
58+
final DrawerHeader header = tester.widget<DrawerHeader>(find.byType(DrawerHeader));
59+
final Color backgroundColor = (header.decoration! as BoxDecoration).color!;
60+
final Color textColor = paintedTextColor(tester, find.text('Drawer Header'));
61+
62+
expect(contrastRatio(textColor, backgroundColor), greaterThanOrEqualTo(4.5));
63+
});
5064
}

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

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,24 @@ void main() {
2020
await tester.pumpAndSettle();
2121
expect(findHeadingLevelOnes, findsOne);
2222
});
23+
24+
testWidgets('expansion tiles stay reachable at a 320x256 viewport', (WidgetTester tester) async {
25+
tester.view.physicalSize = const Size(320, 256);
26+
tester.view.devicePixelRatio = 1.0;
27+
addTearDown(tester.view.reset);
28+
29+
await pumpsUseCase(tester, ExpansionTileUseCase());
30+
// A non-scrolling layout overflows at this size and reports it as an error.
31+
expect(tester.takeException(), isNull);
32+
33+
await tester.scrollUntilVisible(find.text('ExpansionTile 3'), 50);
34+
await tester.ensureVisible(find.text('ExpansionTile 3'));
35+
await tester.pumpAndSettle();
36+
await tester.tap(find.text('ExpansionTile 3'));
37+
await tester.pumpAndSettle();
38+
39+
await tester.ensureVisible(find.text('This is tile number 3'));
40+
await tester.pumpAndSettle();
41+
expect(find.text('This is tile number 3'), findsOne);
42+
});
2343
}

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

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,23 @@ List<String> descendantSemanticsLabels(SemanticsNode node) {
2929
return labels;
3030
}
3131

32+
/// The WCAG contrast ratio between two colors, from 1 to 21.
33+
double contrastRatio(Color a, Color b) {
34+
final double la = a.computeLuminance();
35+
final double lb = b.computeLuminance();
36+
final lighter = la > lb ? la : lb;
37+
final darker = la > lb ? lb : la;
38+
return (lighter + 0.05) / (darker + 0.05);
39+
}
40+
41+
/// The color the given text is painted with, after default styles are applied.
42+
Color paintedTextColor(WidgetTester tester, Finder text) {
43+
final RichText richText = tester.widget<RichText>(
44+
find.descendant(of: text, matching: find.byType(RichText)),
45+
);
46+
return richText.text.style!.color!;
47+
}
48+
3249
// Expects the visible snack bar with [text] to stay past its timeout, and to
3350
// close when its close icon is tapped.
3451
Future<void> expectSnackBarStaysUntilClosed(WidgetTester tester, String text) async {

0 commit comments

Comments
 (0)