Sub topic 14
Checking Contrast and Accessibility
Assume you know Dart null safety, Flutter widget trees, MaterialApp, Scaffold, and BuildContext. This chapter focuses on verifying that a Material 3 theme remains readable and usable in both light and dark modes.
Why contrast matters
Contrast is the visual difference between a foreground and its background. A text color with low contrast is difficult to read, especially for people with low vision, color-vision deficiencies, or a bright screen.
Material 3 uses semantic color roles such as primary, onPrimary, surface, and onSurface. The on prefix means that the color is intended for content displayed on the related background. For example, onPrimary is used for text on a primary button.
A generated ColorScheme provides sensible relationships between these roles, but custom colors can still create accessibility problems. Check both themes after changing seed colors, component themes, or custom ThemeExtension values.
Contrast ratios
Contrast is expressed as a ratio from 1:1 to 21:1. A larger ratio means stronger contrast.
| Content | Common minimum ratio |
|---|---|
| Normal text | 4.5:1 |
| Large text | 3:1 |
| Important icons and control boundaries | 3:1 |
Large text generally means at least 18-point regular text or 14-point bold text. Follow the accessibility standard required by your product or organization when it is stricter than these values.
Use Color.computeLuminance() to calculate relative luminance and then compare the lighter and darker colors:
import 'package:flutter/material.dart';
double contrastRatio(Color first, Color second) {
final lighter = first.computeLuminance();
final darker = second.computeLuminance();
final high = lighter > darker ? lighter : darker;
final low = lighter > darker ? darker : lighter;
return (high + 0.05) / (low + 0.05);
}
void main() {
const background = Color(0xFFFFFFFF);
const foreground = Color(0xFF6750A4);
final ratio = contrastRatio(foreground, background);
debugPrint('Contrast ratio: ${ratio.toStringAsFixed(2)}:1');
}Do not use a contrast ratio as the only accessibility test. Color should not be the sole way to communicate an error or state. Add text, icons, or another visible difference.
Check both light and dark themes
Test every important color pairing in both schemes:
- Body text on
surfaceandsurfaceContainer. - Button labels on
primary,secondary, anderror. - Icons on cards, app bars, and navigation elements.
- Links and focused controls against their surrounding backgrounds.
- Custom success, warning, and information colors.
A dark theme should use a coordinated dark ColorScheme, not a simple inversion of the light colors. Bright saturated colors can appear more intense on dark surfaces. Verify them on real devices as well as in a simulator.
Test more than color
Flutter users can increase text size, use screen readers, or navigate with a keyboard or switch device. Build layouts that tolerate these settings.
- Respect the user's text scale. Avoid fixed-height containers around text.
- Give interactive controls meaningful labels.
- Use
Semanticswhen a custom control's purpose is not clear from its child widgets. - Keep important actions large enough to tap comfortably.
- Provide visible focus indicators.
- Make error messages specific and place them near the related field.
- Test in light mode, dark mode, high text scaling, and screen-reader mode.
A component can have excellent contrast and still be inaccessible if its label is missing or its text is clipped.
Exercise
Why should a custom success color be checked separately in light and dark themes?
Show answer
The same color can have acceptable contrast on a light surface but poor contrast on a dark surface. Each foreground and background pairing must be tested in the theme where it appears.