Sub topic 4
ColorScheme and Its Color Roles
This chapter assumes basic Dart null safety, Flutter widget trees, MaterialApp, Scaffold, and BuildContext. You should also know that ThemeData stores values shared by widgets. A ColorScheme is the part of ThemeData that describes the app's color system.
Material 3 does not treat colors as unrelated choices such as “purple” or “gray.” It assigns each color a role. Widgets use these roles to communicate purpose and to choose a readable foreground color.
Why color roles matter
A color role has two parts:
- A container color is used for a background or surface.
- An
on...color is designed to appear on top of that background.
For example, primary is often used for a prominent button background. onPrimary is the color for the button's label or icon. Using the matching pair is safer than choosing white or black manually.
| Role | Common use | Matching foreground |
|---|---|---|
| `primary` | Main actions and selected controls | `onPrimary` |
| `secondary` | Supporting actions and accents | `onSecondary` |
| `tertiary` | Additional accent or contrast | `onTertiary` |
| `surface` | Cards, sheets, and app backgrounds | `onSurface` |
| `error` | Errors and destructive feedback | `onError` |
The primaryContainer, secondaryContainer, and tertiaryContainer roles provide softer background colors. Their matching foregrounds are onPrimaryContainer, onSecondaryContainer, and onTertiaryContainer.
Material 3 also includes surface roles such as surfaceContainer, surfaceContainerHigh, and surfaceContainerHighest. These let you show elevation and grouping without manually adding gray shades. Flutter versions differ in the exact surface roles they expose, so check the SDK version used by your project.
Reading a ColorScheme in a widget
Read the active scheme through Theme.of(context). This gives the widget the current light or dark values automatically.
import 'package:flutter/material.dart';
void main() {
runApp(const MaterialApp(home: ColorRoleExample()));
}
class ColorRoleExample extends StatelessWidget {
const ColorRoleExample({super.key});
@override
Widget build(BuildContext context) {
final colors = Theme.of(context).colorScheme;
return Scaffold(
appBar: AppBar(title: const Text('Color roles')),
body: Center(
child: Card(
color: colors.primaryContainer,
child: Padding(
padding: const EdgeInsets.all(24),
child: Column(
mainAxisSize: MainAxisSize.min,
children: [
Icon(
Icons.palette,
size: 48,
color: colors.onPrimaryContainer,
),
const SizedBox(height: 12),
Text(
'Primary container',
style: Theme.of(context).textTheme.titleLarge?.copyWith(
color: colors.onPrimaryContainer,
),
),
],
),
),
),
),
);
}
}The card uses primaryContainer as its background. Both the icon and text use onPrimaryContainer. If the app changes to a dark theme, the same role names still provide appropriate values.
Generate roles from a seed color
A seed color is a starting color from which Flutter derives a complete scheme. Use ColorScheme.fromSeed instead of manually defining every role.
final lightScheme = ColorScheme.fromSeed(
seedColor: const Color(0xFF6750A4),
);
final darkScheme = ColorScheme.fromSeed(
seedColor: const Color(0xFF6750A4),
brightness: Brightness.dark,
);The same seed keeps the brand identity consistent. The brightness argument changes the resulting tone and contrast for dark mode. You can place each scheme in a separate ThemeData and pass those themes to MaterialApp.
Contrast and accessibility
Color roles help, but they do not guarantee that every custom combination is accessible. Check text, icons, disabled states, and interactive controls in both light and dark themes. Test with large text and normal text separately because their contrast requirements differ.
Avoid reducing contrast merely to make a design look softer. A color that looks acceptable on one display may be difficult to read on another.
Exercise
A card uses secondaryContainer for its background. Which color role should its main text normally use, and why?
Show answer
Use onSecondaryContainer. It is the foreground role designed to contrast with secondaryContainer, and it adapts when the active theme changes.