Sub topic 8
Choosing colors with enough contrast
Assume you know basic Dart, Flutter layouts, and how to run a widget on an emulator. This chapter focuses on color contrast: how clearly text, icons, borders, and controls stand out from their background.
What contrast means
Contrast is the difference in brightness between a foreground and its background. A contrast ratio compares the relative luminance of two colors. The ratio ranges from 1:1, for identical colors, to 21:1, for black and white.
For common WCAG AA targets:
| Content | Minimum contrast ratio |
|---|---|
| Normal text | 4.5:1 |
| Large text | 3:1 |
| User-interface parts and meaningful graphics | 3:1 |
Large text usually means at least 18-point regular text or 14-point bold text. Check the exact standard when compliance matters.
Contrast is not only about body text. Check these pairs:
- Text against cards, app bars, dialogs, and input fields.
- Icons that communicate meaning.
- Button labels and their button backgrounds.
- Borders that identify fields or selected controls.
- Focus indicators and error messages.
- Disabled content, while remembering that disabled controls still need understandable states.
Do not use color as the only way to communicate information. Pair a red error color with an error icon and a message. Pair a green status color with text such as “Complete.”
Check the actual color pair
A theme color can look accessible in one context and fail in another. Check the foreground and background together, including overlays and opacity. A white label may pass on a dark button but fail on a light surface.
Flutter provides Color.computeLuminance(), which makes it possible to calculate a ratio during development:
import 'package:flutter/material.dart';
double contrastRatio(Color foreground, Color background) {
final foregroundLuminance = foreground.computeLuminance();
final backgroundLuminance = background.computeLuminance();
final lighter = foregroundLuminance > backgroundLuminance
? foregroundLuminance
: backgroundLuminance;
final darker = foregroundLuminance > backgroundLuminance
? backgroundLuminance
: foregroundLuminance;
return (lighter + 0.05) / (darker + 0.05);
}
void main() {
final ratio = contrastRatio(Colors.white, Colors.blue.shade900);
debugPrint('Contrast ratio: ${ratio.toStringAsFixed(2)}:1');
}Use a contrast checker during design review as well. Automated checks cannot judge whether a color is the only signal for a state or whether a focus indicator is easy to notice.
Use a deliberate color scheme
Define semantic colors by purpose rather than scattering color literals through widgets. ColorScheme gives components related roles such as onPrimary, onSurface, and onError. The on... color is intended for content placed on the matching color.
MaterialApp(
theme: ThemeData(
colorScheme: ColorScheme.fromSeed(
seedColor: Colors.indigo,
brightness: Brightness.light,
),
useMaterial3: true,
),
home: const Scaffold(
body: Center(
child: Text('Readable text'),
),
),
);When you customize a role, test its pair. For example, colorScheme.primary must be checked with colorScheme.onPrimary. Also test both light and dark themes, because a pair that passes in one theme may fail in the other.
A practical review flow
Exercise
A gray “Delete” label appears on a white background and fails 4.5:1 contrast. Name two fixes that preserve the meaning of the control.
Show answer
Choose a darker text color or use a larger and heavier text style that meets the applicable large-text threshold. Also keep an explicit label or icon so the action is not communicated by color alone.