Sub topic 4
ColorScheme and its color roles
This chapter assumes you can read a Flutter widget tree, know that ThemeData is passed to MaterialApp, and can use Theme.of(context). It covers what a ColorScheme contains and how to pick the right role for a widget. Generating schemes from a seed color and building the dark scheme come in later chapters.
What a ColorScheme is
A ColorScheme is an immutable set of named colors. Each name is a role, which describes what the color is for, not what it looks like. primary is the color for the most important interactive elements. Whether it is purple or green depends on the scheme.
You reach it through ThemeData.colorScheme. In Material 3 (the default since Flutter 3.16), Flutter's built-in widgets read their colors from this object. If you change a role, every widget that uses it changes with it.
Code that asks for a role keeps working when the theme changes. Code that asks for Colors.purple does not. That is why roles matter for dark mode: the dark scheme assigns a different color to each role, and your widgets follow.
Pairs: a color and the color on top of it
Most roles come in pairs. A background-type role is paired with an on role, which is meant for text and icons drawn on top of it.
primaryandonPrimarysecondaryandonSecondarytertiaryandonTertiaryerrorandonErrorsurfaceandonSurface
The pairing is the contract. If you fill a box with primary, draw its text in onPrimary. The scheme's author has already checked that this combination is readable. Mixing roles, such as primary text on a secondary fill, voids that guarantee.
The main role groups
| Group | Roles | Typical use |
|---|---|---|
| Accent | `primary`, `secondary`, `tertiary` | Buttons, selected states, highlights. Tertiary is for contrasting accents. |
| Containers | `primaryContainer`, `secondaryContainer`, `tertiaryContainer` and their `on...Container` partners | Tonal fills that are less loud than the accent itself, such as a selected chip or a tonal button. |
| Error | `error`, `onError`, `errorContainer`, `onErrorContainer` | Validation messages and destructive actions. |
| Surfaces | `surface`, `onSurface`, `onSurfaceVariant` | Page and card backgrounds, body text, secondary text and icons. |
| Surface containers | `surfaceContainerLowest`, `surfaceContainerLow`, `surfaceContainer`, `surfaceContainerHigh`, `surfaceContainerHighest` | Layered backgrounds that step from low to high emphasis. |
| Outlines | `outline`, `outlineVariant` | Borders, dividers. |
| Inverse | `inverseSurface`, `onInverseSurface`, `inversePrimary` | Elements that must stand out from the page, such as a SnackBar. |
| Utility | `shadow`, `scrim`, `surfaceTint` | Shadow color, the dimming layer behind modals, and the tint applied to elevated surfaces. |
Accent and container roles
primary is saturated and draws attention. primaryContainer is a softer tone of the same hue. Use the container form for larger areas, where a full-strength accent would be overwhelming. A card that highlights a selected plan, for example, should use primaryContainer with onPrimaryContainer text.
Surface roles
The surface family is where many apps go wrong. surface is the base color of screens. The five surfaceContainer* roles give you tonal layers on top of it, and they replace the old approach of showing depth with shadows alone. A navigation bar might sit on surfaceContainer, a card on surfaceContainerLow, and a dialog on surfaceContainerHigh.
Use onSurface for primary text and onSurfaceVariant for secondary text, such as captions and inactive icons.
Utility roles
scrim is the color used to dim content behind a modal sheet or drawer. shadow colors drop shadows. surfaceTint is the color Material applies over elevated surfaces. In current Material 3 defaults it is mostly inert on components that use the surface container roles, so you will rarely set it by hand.
Seeing every role
The following app prints a list of colored rows, one per pair, using the roles directly. Run it, then change the seed color and watch the rows change together.
import 'package:flutter/material.dart';
void main() => runApp(const RolesDemo());
class RolesDemo extends StatelessWidget {
const RolesDemo({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(
theme: ThemeData(
colorScheme: ColorScheme.fromSeed(
seedColor: const Color(0xFF6750A4),
),
),
home: const RolesPage(),
);
}
}
class RolesPage extends StatelessWidget {
const RolesPage({super.key});
@override
Widget build(BuildContext context) {
final c = Theme.of(context).colorScheme;
final pairs = <(String, Color, Color)>[
('primary', c.primary, c.onPrimary),
('primaryContainer', c.primaryContainer, c.onPrimaryContainer),
('secondary', c.secondary, c.onSecondary),
('secondaryContainer', c.secondaryContainer, c.onSecondaryContainer),
('tertiary', c.tertiary, c.onTertiary),
('tertiaryContainer', c.tertiaryContainer, c.onTertiaryContainer),
('error', c.error, c.onError),
('errorContainer', c.errorContainer, c.onErrorContainer),
('surface', c.surface, c.onSurface),
('surfaceContainerHigh', c.surfaceContainerHigh, c.onSurface),
('inverseSurface', c.inverseSurface, c.onInverseSurface),
];
return Scaffold(
appBar: AppBar(title: const Text('Color roles')),
body: ListView(
children: [
for (final (name, background, foreground) in pairs)
Container(
color: background,
padding: const EdgeInsets.all(16),
child: Text(name, style: TextStyle(color: foreground)),
),
],
),
);
}
}The for loop destructures each record into a name, a fill, and the matching on color. This is the pairing rule applied in code.
How roles reach widgets
Built-in widgets are wired to specific roles. A FilledButton fills with primary and draws its label in onPrimary. A Card uses a surface container role. You do not set these yourself, which is why changing the scheme restyles an entire app with no further edits.
Using roles in your own widgets
Read the scheme once at the top of build, then pick roles by meaning.
import 'package:flutter/material.dart';
class PlanTile extends StatelessWidget {
const PlanTile({super.key, required this.title, required this.selected});
final String title;
final bool selected;
@override
Widget build(BuildContext context) {
final colors = Theme.of(context).colorScheme;
final fill = selected ? colors.primaryContainer : colors.surfaceContainerLow;
final text = selected ? colors.onPrimaryContainer : colors.onSurface;
return Container(
padding: const EdgeInsets.all(16),
decoration: BoxDecoration(
color: fill,
borderRadius: BorderRadius.circular(12),
border: Border.all(
color: selected ? colors.primary : colors.outlineVariant,
),
),
child: Text(title, style: TextStyle(color: text)),
);
}
}The tile contains no hex values and no Brightness check. It looks right in both light and dark themes because both schemes define the same roles.
Building a ColorScheme by hand
ColorScheme.fromSeed is the usual way to get a scheme, but the plain constructor exists too. It requires brightness plus these pairs: primary, onPrimary, secondary, onSecondary, error, onError, surface and onSurface. The remaining roles are optional and fall back to derived values, so a hand-built scheme is easy to get subtly wrong.
A more practical technique is to start from a generated scheme and override only what you must with copyWith.
import 'package:flutter/material.dart';
ThemeData buildTheme() {
final base = ColorScheme.fromSeed(seedColor: const Color(0xFF6750A4));
final scheme = base.copyWith(
error: const Color(0xFFB3261E),
onError: Colors.white,
);
return ThemeData(colorScheme: scheme);
}Override roles in pairs. If you change error and leave onError alone, the pair may no longer be readable.
Common mistakes
- Using a fill role with the wrong text role.
TextinonSurfaceon aprimarybackground can fail contrast. Always use the matchingonrole. - Reaching for
Colors.*or hex values. They do not change with the theme, so they usually break in one of the two modes. - Using deprecated names.
background,onBackgroundandsurfaceVariantare deprecated in recent Flutter versions. Usesurface,onSurfaceand thesurfaceContainer*roles instead. - Using
primaryfor large areas. It is designed for small, interactive elements. Use a container role for big regions. - Reading
Theme.of(context)in the wrong place. If you call it in a widget above theMaterialApp, there is no theme to find. Read it from abuildmethod below the app widget.
Choosing a role: a quick procedure
- Decide the element's importance. A main action is
primary. A supporting action issecondaryor a container role. A background layer is a surface role. - Pick the fill role from that decision.
- Pick the
onpartner for any text or icon drawn over it. - For borders and dividers, use
outlinefor strong edges andoutlineVariantfor subtle ones. - If a built-in widget already covers the need, let it pick its own roles and override through a component theme instead.
Exercise
A card shows a selected item. You fill it with colors.secondaryContainer and draw its title in colors.onSurface. What should you change, and why?
Show answer
Change the title color to colors.onSecondaryContainer. Each fill role is paired with an on role that the scheme guarantees is readable on it. onSurface is only guaranteed against the surface roles, so in some seeds or in dark mode the title may have poor contrast on secondaryContainer.
Exercise
Which roles would you use for a page background, a card that sits on it, and a dialog that sits above the card, and which role gives the card's secondary caption text?
Show answer
Use colors.surface for the page, colors.surfaceContainerLow for the card, and colors.surfaceContainerHigh for the dialog, since the surfaceContainer roles step up in emphasis. The caption uses colors.onSurfaceVariant.