Sub topic 6
Creating the dark color scheme
This chapter assumes you can read Flutter widget code and that you already know ColorScheme.fromSeed and the theme and darkTheme parameters on MaterialApp. It covers how to build the dark ColorScheme, what changes between the light and dark versions, and the mistakes that produce a dark theme that looks wrong.
What "dark" means in a ColorScheme
A ColorScheme is a set of named color roles such as primary, surface and onSurface. It also has a brightness field, which is either Brightness.light or Brightness.dark. That field records which palette variant the scheme holds.
You do not make a dark scheme by inverting the light one. Material 3 builds each color role from a tonal palette. A tonal palette is a ramp of one hue from tone 0 (black) to tone 100 (white). A light scheme and a dark scheme pick different tones from the same ramps.
| Role | Light tone | Dark tone |
|---|---|---|
| `primary` | 40 | 80 |
| `onPrimary` | 100 | 20 |
| `primaryContainer` | 90 | 30 |
| `onPrimaryContainer` | 10 | 90 |
| `surface` | 98 | 6 |
| `onSurface` | 10 | 90 |
These tones are the defaults for the standard fromSeed variant. They can change slightly between Flutter versions.
Two things follow from the table:
- Every color keeps the seed's hue, so the dark theme still looks like the same brand.
- The dark
primaryis lighter than the lightprimary. A saturated mid-tone color on a near-black surface looks harsh and can vibrate visually. The lighter tone avoids that.
Generating the dark scheme from the seed
Pass the same seed color and set brightness: Brightness.dark:
import 'package:flutter/material.dart';
const seed = Color(0xFF6750A4);
final lightScheme = ColorScheme.fromSeed(seedColor: seed);
final darkScheme = ColorScheme.fromSeed(
seedColor: seed,
brightness: Brightness.dark,
);
void main() {
print(lightScheme.brightness); // Brightness.light
print(darkScheme.brightness); // Brightness.dark
print(lightScheme.primary == darkScheme.primary); // false
}The default for brightness is Brightness.light. If you leave the argument out, you get the light palette, whatever the device is set to.
The diagram shows how one seed feeds both schemes and how MaterialApp chooses between them.
Putting it into ThemeData
Wrap each scheme in its own ThemeData. The ThemeData takes its brightness from the ColorScheme, so you do not need to set brightness on ThemeData as well:
import 'package:flutter/material.dart';
void main() => runApp(const MyApp());
class MyApp extends StatelessWidget {
const MyApp({super.key});
@override
Widget build(BuildContext context) {
const seed = Color(0xFF6750A4);
return MaterialApp(
title: 'Themed app',
theme: ThemeData(
colorScheme: ColorScheme.fromSeed(seedColor: seed),
),
darkTheme: ThemeData(
colorScheme: ColorScheme.fromSeed(
seedColor: seed,
brightness: Brightness.dark,
),
),
themeMode: ThemeMode.system,
home: const HomePage(),
);
}
}
class HomePage extends StatelessWidget {
const HomePage({super.key});
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: const Text('Material 3')),
body: const Center(child: Text('Hello, theme')),
);
}
}With ThemeMode.system, Flutter reads the platform brightness. It uses darkTheme when the device is in dark mode and theme otherwise. If you set ThemeMode.dark, Flutter uses darkTheme regardless of the device.
If you give ThemeData both a colorScheme and a brightness, the two must agree. Passing only colorScheme keeps a single source of truth.
Avoiding duplication
The light and dark themes usually differ only in brightness. A small helper keeps them in sync, so a change to a component theme cannot reach one theme and miss the other:
import 'package:flutter/material.dart';
ThemeData buildTheme(Brightness brightness) {
final scheme = ColorScheme.fromSeed(
seedColor: const Color(0xFF6750A4),
brightness: brightness,
);
return ThemeData(
colorScheme: scheme,
appBarTheme: const AppBarTheme(centerTitle: true),
);
}
class HelperApp extends StatelessWidget {
const HelperApp({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(
theme: buildTheme(Brightness.light),
darkTheme: buildTheme(Brightness.dark),
home: const Scaffold(body: Center(child: Text('Helper'))),
);
}
}How widgets pick up the dark colors
Widgets such as Scaffold, Card and AppBar read roles from the active ColorScheme. When the theme changes, they get the new colors without any code on your side. This works only if your own widgets read colors from the theme too:
import 'package:flutter/material.dart';
class ThemedCard extends StatelessWidget {
const ThemedCard({super.key});
@override
Widget build(BuildContext context) {
final theme = Theme.of(context);
final colors = theme.colorScheme;
return Container(
padding: const EdgeInsets.all(16),
color: colors.primaryContainer,
child: Text(
'Headline',
style: theme.textTheme.headlineMedium?.copyWith(
color: colors.onPrimaryContainer,
),
),
);
}
}Both colors come from the same container pair. onPrimaryContainer is chosen to have enough contrast against primaryContainer in both brightnesses. If you mix roles from different pairs, such as primaryContainer with onSurface, you lose that guarantee.
To branch on brightness in a widget, which is rare, read it from the theme:
final isDark = Theme.of(context).brightness == Brightness.dark;Use Theme.of(context).brightness rather than MediaQuery.platformBrightnessOf(context). The platform value is the device setting. The theme value is what the app is actually showing, and the two differ whenever themeMode is forced to light or dark.
Adjusting individual dark roles
Sometimes the generated dark scheme is not what a design calls for. For example, you may want a true-black surface for OLED screens. Use copyWith on the generated scheme and leave the rest alone:
import 'package:flutter/material.dart';
ColorScheme buildOledDarkScheme(Color seed) {
final base = ColorScheme.fromSeed(
seedColor: seed,
brightness: Brightness.dark,
);
return base.copyWith(surface: Colors.black);
}Overriding a role can break its pairing. If you change surface and not onSurface, check that the text is still readable. Containers such as surfaceContainer are separate roles and will not follow the change. Override as few roles as you can, and check each one after the change.
Testing the dark theme
Run the app with the system set to dark, or force it with themeMode: ThemeMode.dark while you develop. In widget tests, you can simulate the platform setting:
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
void main() {
testWidgets('uses the dark theme when the platform is dark',
(tester) async {
tester.platformDispatcher.platformBrightnessTestValue = Brightness.dark;
addTearDown(tester.platformDispatcher.clearPlatformBrightnessTestValue);
await tester.pumpWidget(
MaterialApp(
theme: ThemeData(colorScheme: ColorScheme.fromSeed(seedColor: Colors.teal)),
darkTheme: ThemeData(
colorScheme: ColorScheme.fromSeed(
seedColor: Colors.teal,
brightness: Brightness.dark,
),
),
home: Builder(
builder: (context) => Text(
Theme.of(context).brightness.name,
textDirection: TextDirection.ltr,
),
),
),
);
expect(find.text('dark'), findsOneWidget);
});
}Common errors
- Dark theme looks identical to light.
darkThemewas built withoutbrightness: Brightness.dark, so it holds a light scheme. This produces no error. - Dark theme never appears.
themeModeisThemeMode.light, ordarkThemeisnull. WithThemeMode.systemand nodarkTheme, Flutter falls back totheme. - Hard-coded colors.
Colors.whiteorColor(0xFF...)inside a widget stays the same in both modes. This leaves white text on a white surface or the reverse. Read fromTheme.of(context).colorSchemeinstead. - Fixed-brightness component themes. A component theme built with a literal color, for example
AppBarTheme(backgroundColor: Colors.white), applies to both modes. Leave the color unset or build the theme from the scheme. - Mismatched pairs. Text with a role that does not pair with its background, such as
onPrimaryonsurface, can fail contrast in one brightness while passing in the other. - Light-only assets. Images and icons with baked-in dark ink disappear on a dark surface. Tint them with
colorScheme.onSurfaceor supply a dark variant.
Summary of the approach
- Keep one seed color.
- Call
ColorScheme.fromSeedtwice, once per brightness. - Wrap each result in a
ThemeDataand assign them tothemeanddarkTheme. - Read every color through
Theme.of(context).colorScheme. - Override single roles with
copyWithonly when a design requires it, and recheck contrast afterward.
Exercise
A teammate reports that the app stays light when the phone is switched to dark mode. Their MaterialApp sets themeMode: ThemeMode.system, theme: ThemeData(colorScheme: ColorScheme.fromSeed(seedColor: seed)), and darkTheme: ThemeData(colorScheme: ColorScheme.fromSeed(seedColor: seed)). What is wrong, and what is the fix?
Show answer
Flutter does switch to darkTheme, but darkTheme holds a light ColorScheme because fromSeed defaults to Brightness.light. The fix is to pass brightness: Brightness.dark to the ColorScheme.fromSeed call used in darkTheme.
Exercise
Why should a widget read Theme.of(context).brightness instead of MediaQuery.platformBrightnessOf(context) when it needs to know whether dark colors are showing?
Show answer
The platform brightness is the device setting. The theme brightness is what the app actually applies. If themeMode is forced to ThemeMode.light or ThemeMode.dark, the two can differ, and only the theme value matches what is on screen.
Exercise
You override the dark scheme with base.copyWith(surface: Colors.black). Name one follow-up check you should make.
Show answer
Check that text and icons drawn on the black surface still have enough contrast. onSurface was not changed, and roles such as surfaceContainer do not follow the override, so they may no longer look consistent with the new surface.