Sub topic 6
Creating the Dark Color Scheme
Dark mode uses a color scheme designed for dark surfaces. It is more than changing the background to black. Material 3 adjusts surface colors, text colors, and accent colors so that the interface remains readable and visually balanced.
This chapter assumes basic Dart null safety, Flutter widget trees, MaterialApp, Scaffold, and BuildContext.
How a dark `ColorScheme` works
A ColorScheme is a set of named colors with specific jobs. For example, primary is used for important actions, while onPrimary is used for content placed on a primary-colored surface.
In a dark scheme, the background and surface roles are dark. Their matching on... roles remain light enough for readable text and icons.
| Role | Purpose in a dark scheme |
|---|---|
| `surface` | Main cards, sheets, and other containers |
| `onSurface` | Text and icons placed on `surface` |
| `primary` | Main accents and actions |
| `onPrimary` | Content placed on `primary` |
| `surfaceContainer` | A slightly raised surface for grouped content |
Do not choose each color independently. Related roles must have enough contrast and should look like one system.
Generate a dark scheme from a seed
Use ColorScheme.fromSeed to generate related colors from one brand color. Pass Brightness.dark for the dark version.
import 'package:flutter/material.dart';
void main() {
runApp(const ThemedApp());
}
class ThemedApp extends StatelessWidget {
const ThemedApp({super.key});
@override
Widget build(BuildContext context) {
const seedColor = Color(0xFF6750A4);
final darkScheme = ColorScheme.fromSeed(
seedColor: seedColor,
brightness: Brightness.dark,
);
return MaterialApp(
theme: ThemeData(
colorScheme: ColorScheme.fromSeed(seedColor: seedColor),
),
darkTheme: ThemeData(
colorScheme: darkScheme,
brightness: Brightness.dark,
),
themeMode: ThemeMode.dark,
home: const HomePage(),
);
}
}
class HomePage extends StatelessWidget {
const HomePage({super.key});
@override
Widget build(BuildContext context) {
final colors = Theme.of(context).colorScheme;
return Scaffold(
appBar: AppBar(title: const Text('Dark theme')),
body: Center(
child: Card(
color: colors.surfaceContainer,
child: Padding(
padding: const EdgeInsets.all(24),
child: Text(
'Readable dark surfaces',
style: TextStyle(color: colors.onSurface),
),
),
),
),
);
}
}themeMode: ThemeMode.dark selects the dark scheme for this example. In a complete app, ThemeMode.system usually lets the device choose between theme and darkTheme.
Avoid common dark-mode errors
Do not use pure black everywhere
A completely black background can make cards and dialogs difficult to distinguish. Material 3 commonly uses several dark neutral tones. Slight differences between surfaces create depth without adding borders everywhere.
Do not reuse light-theme colors blindly
A bright accent that works on a white background may be too intense on a dark background. Generate a separate dark scheme and inspect its primary, secondary, and container roles.
Use matching `on...` colors
If a widget uses primaryContainer, its content should normally use onPrimaryContainer. This relationship is clearer and safer than manually selecting white or black.
Check the result on real surfaces
Test more than the main page. Inspect text, disabled controls, dialogs, menus, input fields, cards, and selected states. Check both light and dark schemes because a color can pass contrast checks in one scheme and fail in the other.
Exercise
Why should a dark theme use a separate ColorScheme instead of simply applying a dark background to the light theme?
Show answer
A separate dark ColorScheme adjusts surfaces, accents, and matching on-colors together, improving contrast and preserving a consistent visual hierarchy.