Sub topic 5
Building a Palette with ColorScheme.fromSeed
This chapter assumes that you know basic Dart null safety, can build Flutter widget trees, and are familiar with MaterialApp, Scaffold, and BuildContext.
A Material 3 palette is a coordinated group of colors. Flutter stores this palette in a ColorScheme. Instead of choosing every color separately, you can provide one seed color and let Flutter generate related color roles.
Why use a seed color?
A seed color is the starting color for a generated palette. ColorScheme.fromSeed uses that color to calculate primary, secondary, surface, and other roles. The result is more consistent than assigning unrelated colors to individual widgets.
The generated scheme contains roles such as:
| Role | Typical use |
|---|---|
| primary | Main actions and important controls |
| onPrimary | Text or icons placed on primary |
| primaryContainer | Less prominent primary surfaces |
| onPrimaryContainer | Content placed on primaryContainer |
| surface | Cards, sheets, and page surfaces |
| error | Error messages and destructive feedback |
A color role beginning with on describes content displayed on another role. For example, onPrimary should be readable when placed over primary.
Generate a light palette
The following app creates a Material 3 theme from one purple seed color:
import 'package:flutter/material.dart';
void main() {
runApp(const PaletteApp());
}
class PaletteApp extends StatelessWidget {
const PaletteApp({super.key});
@override
Widget build(BuildContext context) {
const seedColor = Color(0xFF6750A4);
final colorScheme = ColorScheme.fromSeed(
seedColor: seedColor,
brightness: Brightness.light,
);
return MaterialApp(
title: 'Seed palette',
theme: ThemeData(
useMaterial3: true,
colorScheme: colorScheme,
),
home: const PalettePage(),
);
}
}
class PalettePage extends StatelessWidget {
const PalettePage({super.key});
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: const Text('Seed palette')),
body: Center(
child: FilledButton(
onPressed: () {},
child: const Text('Continue'),
),
),
);
}
}The seed color does not become every color in the application. Flutter derives a tonal palette and assigns colors to semantic roles. Widgets that understand Material themes can then use those roles automatically.
Generate a dark palette
Pass Brightness.dark to create a dark variant from the same seed:
final darkColorScheme = ColorScheme.fromSeed(
seedColor: const Color(0xFF6750A4),
brightness: Brightness.dark,
);The dark scheme is not simply the light scheme with every color inverted. Flutter adjusts tones so that surfaces remain dark and foreground content remains readable. Use the same seed for light and dark themes when you want one brand identity across both modes.
Avoid hard-coded palette colors
A common error is using the seed color directly throughout the widget tree. That approach ignores the role system and often produces poor contrast in dark mode. Read the active scheme from the nearest theme instead:
class StatusCard extends StatelessWidget {
const StatusCard({super.key});
@override
Widget build(BuildContext context) {
final colors = Theme.of(context).colorScheme;
return Container(
padding: const EdgeInsets.all(16),
color: colors.primaryContainer,
child: Text(
'Ready',
style: TextStyle(color: colors.onPrimaryContainer),
),
);
}
}The on-role pairing matters. For example, primaryContainer and onPrimaryContainer are designed to work together. Do not assume that primary and onSurface, or surface and onPrimary, have suitable contrast.
Common errors
- Supplying different unrelated seed colors for light and dark themes can make the brand appear inconsistent.
- Using one foreground color everywhere can make text unreadable on some surfaces.
- Mixing old individual color properties with ColorScheme roles can create conflicting results.
- Choosing colors by appearance alone can miss contrast problems. Test the actual rendered combinations.
Exercise
Why should a widget use onPrimaryContainer when its background is primaryContainer?
Show answer
The onPrimaryContainer role is the foreground color generated for content on primaryContainer, so the pair is intended to provide readable contrast.