Sub topic 5
Building a palette with ColorScheme.fromSeed
This chapter assumes you know basic Dart and null safety, can build widget trees, and have used MaterialApp, Scaffold and BuildContext. You should also know that a ColorScheme is a set of named color roles such as primary and onPrimary. Here you will learn how one seed color becomes a whole palette, what you can tune, and where the result surprises people.
Why generate a palette
A Material 3 interface needs many related colors. Buttons, containers, surfaces, outlines and error states each need a color, and every background needs a readable foreground. Picking these by hand is slow, and the pairs often fail contrast checks.
ColorScheme.fromSeed takes one color and returns a full ColorScheme. It is based on the Material Color Utilities algorithm, the same one used on Android for dynamic color. Every role comes out already paired and tuned for readability.
import 'package:flutter/material.dart';
final ColorScheme scheme = ColorScheme.fromSeed(
seedColor: const Color(0xFF6750A4),
);The result is a normal ColorScheme. You pass it to ThemeData(colorScheme: scheme) and read it back with Theme.of(context).colorScheme.
How the seed becomes a palette
The algorithm does not simply lighten and darken your color. It converts the seed to HCT, a color space with three axes:
- Hue is the position on the color wheel, such as blue or orange.
- Chroma is how colorful the color is, from gray to vivid.
- Tone is perceived lightness, from 0 (black) to 100 (white).
From the seed's hue it builds several tonal palettes. A tonal palette is the same hue and chroma sampled at many tones. The default palettes are:
| Palette | Derived from | Used for |
|---|---|---|
| Primary | Seed hue, moderate chroma | `primary`, `primaryContainer` and their `on` colors |
| Secondary | Seed hue, lower chroma | `secondary`, `secondaryContainer` |
| Tertiary | Seed hue rotated by 60 degrees | `tertiary`, `tertiaryContainer` |
| Neutral | Seed hue, very low chroma | `surface`, `onSurface`, surface container roles |
| Neutral variant | Seed hue, low chroma | `outline`, `surfaceVariant`, `onSurfaceVariant` |
| Error | Fixed red hue | `error`, `errorContainer` |
Each role then picks a fixed tone from its palette. The tone depends on the brightness. For the primary family, the default variant uses these tones:
| Role | Light tone | Dark tone |
|---|---|---|
| `primary` | 40 | 80 |
| `onPrimary` | 100 | 20 |
| `primaryContainer` | 90 | 30 |
| `onPrimaryContainer` | 10 | 90 |
Because tones are fixed per role, the gap in lightness between primary and onPrimary is large in both brightnesses. That gap is what keeps text readable.
See it run
Reading about palettes is less useful than watching one change. The following app lets you pick a seed and shows paired roles as tiles. Each tile uses the background role for the fill and its matching on role for the text.
import 'package:flutter/material.dart';
void main() => runApp(const SeedPlayground());
class SeedPlayground extends StatefulWidget {
const SeedPlayground({super.key});
@override
State<SeedPlayground> createState() => _SeedPlaygroundState();
}
class _SeedPlaygroundState extends State<SeedPlayground> {
static const seeds = <String, Color>{
'Purple': Color(0xFF6750A4),
'Teal': Color(0xFF006A6A),
'Orange': Color(0xFFE65100),
'Green': Color(0xFF2E7D32),
};
String _name = 'Purple';
@override
Widget build(BuildContext context) {
final scheme = ColorScheme.fromSeed(seedColor: seeds[_name]!);
return MaterialApp(
theme: ThemeData(colorScheme: scheme),
home: Scaffold(
appBar: AppBar(title: const Text('Seed playground')),
body: ListView(
padding: const EdgeInsets.all(16),
children: [
Wrap(
spacing: 8,
children: [
for (final name in seeds.keys)
ChoiceChip(
label: Text(name),
selected: name == _name,
onSelected: (_) => setState(() => _name = name),
),
],
),
const SizedBox(height: 16),
RoleTile('primary', scheme.primary, scheme.onPrimary),
RoleTile('primaryContainer', scheme.primaryContainer,
scheme.onPrimaryContainer),
RoleTile('secondary', scheme.secondary, scheme.onSecondary),
RoleTile('secondaryContainer', scheme.secondaryContainer,
scheme.onSecondaryContainer),
RoleTile('tertiary', scheme.tertiary, scheme.onTertiary),
RoleTile('tertiaryContainer', scheme.tertiaryContainer,
scheme.onTertiaryContainer),
RoleTile('surface', scheme.surface, scheme.onSurface),
RoleTile('error', scheme.error, scheme.onError),
],
),
),
);
}
}
class RoleTile extends StatelessWidget {
const RoleTile(this.label, this.background, this.foreground, {super.key});
final String label;
final Color background;
final Color foreground;
@override
Widget build(BuildContext context) {
return Container(
margin: const EdgeInsets.only(bottom: 4),
padding: const EdgeInsets.all(16),
color: background,
child: Text(label, style: TextStyle(color: foreground)),
);
}
}Switch between the seeds and watch two things. The hue of every family follows the seed, and the error tile stays red. The surface tile barely changes, because it comes from the neutral palette with very low chroma.
The seed is an input, not the primary color
The most common surprise is that scheme.primary rarely equals your seed. The default variant, called tonal spot, caps primary chroma and moves the tone to 40 in light mode. A vivid brand orange comes out as a calmer, darker orange.
There are three ways to handle this, from least to most control:
- Accept the generated color. This keeps the contrast guarantees of the algorithm.
- Choose a different scheme variant so the output sits closer to the seed.
- Override individual roles after generation.
Variants and contrast level
fromSeed has two tuning parameters beyond the seed. dynamicSchemeVariant selects the algorithm's style, and contrastLevel ranges from -1.0 to 1.0, where 0.0 is the standard contrast. Both are available in recent Flutter releases, so check that your SDK has them if the analyzer reports an unknown parameter.
import 'package:flutter/material.dart';
const seed = Color(0xFF6750A4);
final tonalSpot = ColorScheme.fromSeed(seedColor: seed);
final vibrant = ColorScheme.fromSeed(
seedColor: seed,
dynamicSchemeVariant: DynamicSchemeVariant.vibrant,
);
final fidelity = ColorScheme.fromSeed(
seedColor: seed,
dynamicSchemeVariant: DynamicSchemeVariant.fidelity,
);
final highContrast = ColorScheme.fromSeed(
seedColor: seed,
contrastLevel: 1.0,
);The variants trade restraint for expressiveness:
| Variant | Effect |
|---|---|
| `tonalSpot` | The default. Calm, moderate chroma, a good fit for most apps. |
| `vibrant` | Higher chroma in the primary palette, more saturated surfaces of color. |
| `fidelity` | Keeps primary chroma closer to the seed, so brand colors stay recognizable. |
| `monochrome` | Grayscale output with no hue. |
| `neutral` | Very muted, almost gray accents. |
The contrast level moves tones apart. At 1.0 the on colors get more separation from their backgrounds, which helps low-vision users. Chapter 14 returns to this in the accessibility discussion.
Overriding individual roles
When one role must be exact, generate the scheme and then replace that role with copyWith. Replace the on color in the same step, because the generated onPrimary was tuned for the generated primary, not for yours.
import 'package:flutter/material.dart';
const brand = Color(0xFFE65100);
ColorScheme buildScheme(Brightness brightness) {
return ColorScheme.fromSeed(
seedColor: brand,
brightness: brightness,
).copyWith(
primary: brand,
onPrimary: Colors.white,
);
}This is also where overrides can hurt you. The pinned brand color is the same in light and dark mode, so it may fail contrast on a dark surface. Check both brightnesses before you ship an override. Dark schemes get their own chapter next, so for now note only that brightness is a separate argument and defaults to light.
Common errors
- Using the light scheme for dark mode. If you build one
ColorSchemeand give it to boththemeanddarkTheme, the dark theme looks light. Each brightness needs its own call withbrightnessset. - Rebuilding the scheme on every frame. Generation does real color math. Create the scheme once, in a top-level
final, astatic finalfield or a state field, not inside a frequently calledbuildmethod. The playground above regenerates only when the user taps a chip, which is acceptable. - Choosing a gray seed. A seed with near-zero chroma, such as white, black or a mid gray, produces a nearly colorless palette in tonal spot. Use the
monochromeorneutralvariant on purpose, or pick a seed with real hue. - Hard-coding colors next to a generated scheme. A
Color(0xFF...)literal in a widget ignores the scheme and breaks in dark mode. ReadTheme.of(context).colorSchemeinstead. - Treating the seed as a theme setting. Changing the seed changes every role at once. Treat it as a design decision made once, not a per-screen tweak.
Where this leads
You now have a light ColorScheme built from one seed, with options for variants, contrast and overrides. The next chapter builds the matching dark scheme from the same seed and explains how the tone choices change with Brightness.dark.
Exercise
A designer says the primary button must be exactly #E65100. You call ColorScheme.fromSeed(seedColor: const Color(0xFFE65100)) and the button is a different orange. Why does this happen, and how do you get the exact color without losing readable button text?
Show answer
The seed is only an input to the HCT-based algorithm. In the default tonalSpot variant, primary chroma is capped and the tone is fixed at 40 in light mode, so primary differs from the seed. To get the exact color, call .copyWith(primary: const Color(0xFFE65100), onPrimary: ...) on the generated scheme, and choose an onPrimary with enough contrast, for example white. Then check contrast in both light and dark schemes, since the pinned color does not adapt to brightness. DynamicSchemeVariant.fidelity is a less strict alternative that keeps the output closer to the seed.
Exercise
Your app passes the same ColorScheme.fromSeed(seedColor: seed) result to both theme and darkTheme, and dark mode looks light. What is the fix?
Show answer
Build a second scheme with ColorScheme.fromSeed(seedColor: seed, brightness: Brightness.dark) and pass it to darkTheme. The brightness argument defaults to light, and the roles take different tones depending on it.