Sub topic 7
Wiring theme, darkTheme and themeMode
This chapter assumes you can write Dart with null safety, build widget trees, and use MaterialApp, Scaffold and BuildContext. It also assumes you have met ThemeData and ColorScheme.fromSeed in earlier chapters. Here you connect two ThemeData objects to MaterialApp and learn how Flutter picks between them.
The three properties
MaterialApp has three properties that decide which theme the app shows:
| Property | Type | Role |
|---|---|---|
| `theme` | `ThemeData?` | The light theme. It is also the fallback for every other case. |
| `darkTheme` | `ThemeData?` | The theme used when dark mode is active. |
| `themeMode` | `ThemeMode` | Selects the mode: `system`, `light` or `dark`. The default is `ThemeMode.system`. |
ThemeMode.system follows the device setting. ThemeMode.light and ThemeMode.dark ignore the device and force one mode. The enum has exactly these three values.
A minimal working setup
Both themes use the same seed color. Only the brightness argument differs. A helper function keeps the two themes from drifting apart.
import 'package:flutter/material.dart';
void main() => runApp(const MyApp());
const Color seed = Color(0xFF6750A4);
ThemeData buildTheme(Brightness brightness) {
return ThemeData(
colorScheme: ColorScheme.fromSeed(
seedColor: seed,
brightness: brightness,
),
);
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Themed app',
theme: buildTheme(Brightness.light),
darkTheme: buildTheme(Brightness.dark),
themeMode: ThemeMode.system,
home: const HomePage(),
);
}
}
class HomePage extends StatelessWidget {
const HomePage({super.key});
@override
Widget build(BuildContext context) {
final brightness = Theme.of(context).brightness;
return Scaffold(
appBar: AppBar(title: const Text('Material 3')),
body: Center(child: Text('Active theme: ${brightness.name}')),
);
}
}Run it, then switch your device or simulator between light and dark appearance. The text changes, and so does every surface color.
The earlier example in this tutorial wrote the two ThemeData objects out in full. The helper is the same thing with less repetition. Because useMaterial3 defaults to true in current Flutter versions, you do not need to set it.
How MaterialApp chooses a theme
MaterialApp makes this decision on every build. The inputs are themeMode, the platform brightness reported by MediaQuery, and which properties you supplied.
- It decides whether dark mode is wanted. That is true when
themeModeisdark, or when it issystemand the platform brightness is dark. - If dark mode is wanted and
darkThemeis not null, it usesdarkTheme. - Otherwise it uses
theme. - If
themeis null too, it uses a defaultThemeData.
Two optional properties, highContrastTheme and highContrastDarkTheme, take priority when the platform requests high contrast and you supplied them. This matters for the accessibility chapter.
flowchart TDStep 3 explains the most common surprise. If you set themeMode: ThemeMode.dark but forget darkTheme, you get the light theme. Flutter does not derive a dark theme for you.
Gotchas and common errors
The dark theme that looks light
ColorScheme.fromSeed defaults to Brightness.light. This code compiles and runs:
import 'package:flutter/material.dart';
final brokenDark = ThemeData(
colorScheme: ColorScheme.fromSeed(seedColor: const Color(0xFF6750A4)),
);If you pass it as darkTheme, the app switches to "dark mode" but keeps light colors. Always pass brightness: Brightness.dark to fromSeed for the dark scheme.
Mismatched brightness arguments
ThemeData also accepts a brightness argument. Do not set it to a value that disagrees with the colorScheme. In debug builds this can fail an assertion. Set the brightness on the ColorScheme and leave it off ThemeData. The helper above does this.
Hard-coded colors
Colors.white or Color(0xFF...) inside a widget ignores the active theme. The widget looks right in one mode and wrong in the other. Read colors from Theme.of(context).colorScheme instead.
Theme.of(context) in the wrong place
Theme.of(context) needs a context that sits below MaterialApp. This does not work:
import 'package:flutter/material.dart';
class BadRoot extends StatelessWidget {
const BadRoot({super.key});
@override
Widget build(BuildContext context) {
// This context is above MaterialApp, so it finds no app theme.
final color = Theme.of(context).colorScheme.primary;
return MaterialApp(
home: Scaffold(backgroundColor: color),
);
}
}The context passed to build belongs to BadRoot, which is above MaterialApp. No app theme exists at that level, so the lookup returns Flutter's default theme. Move the code into a widget under MaterialApp, or wrap it in a Builder.
Seeing changes only after a restart
Hot reload rebuilds widgets, so edited theme values normally appear at once. If you keep a ThemeData in a top-level final or a static field, edits to its initializer may not be re-evaluated on hot reload. A full restart fixes this. Building themes in a function, as buildTheme does, avoids the problem.
Theme animation
When the resolved theme changes, MaterialApp animates between the old and new ThemeData rather than jumping. Two properties control this:
themeAnimationDurationdefaults to 200 milliseconds.themeAnimationCurvedefaults toCurves.linear.
You can disable the animation by setting the duration to Duration.zero:
import 'package:flutter/material.dart';
class NoAnimationApp extends StatelessWidget {
const NoAnimationApp({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(
theme: ThemeData(colorScheme: ColorScheme.fromSeed(seedColor: Colors.teal)),
darkTheme: ThemeData(
colorScheme: ColorScheme.fromSeed(
seedColor: Colors.teal,
brightness: Brightness.dark,
),
),
themeAnimationDuration: Duration.zero,
home: const Scaffold(body: Center(child: Text('Instant switch'))),
);
}
}The animation interpolates every field in ThemeData by calling ThemeData.lerp. Custom ThemeExtension classes take part through their lerp method, which is why it must be implemented.
Overriding the theme for one subtree
The Theme widget replaces the theme for everything below it. Use it for a region that needs a different look, such as a promotional banner:
import 'package:flutter/material.dart';
class Banner extends StatelessWidget {
const Banner({super.key});
@override
Widget build(BuildContext context) {
final base = Theme.of(context);
return Theme(
data: base.copyWith(
colorScheme: ColorScheme.fromSeed(
seedColor: Colors.orange,
brightness: base.brightness,
),
),
child: const Card(child: Padding(
padding: EdgeInsets.all(16),
child: Text('Sale'),
)),
);
}
}The line brightness: base.brightness keeps the local scheme in step with the app's current mode. Without it, the banner would stay light in dark mode.
Decision criteria
- Follow the system (
ThemeMode.system) when you have no strong reason not to. It is the default, and users expect it. - Force a mode only when the design requires it, for example a media player that is always dark. In that case you can supply only
theme, built as a dark theme. - Use one seed for both themes so brand identity stays consistent. Using different seeds is allowed, but it makes the app look like two products.
- Supply
darkThemewhenever you support dark mode at all. A nulldarkThemesilently disables it.
Check your understanding
Exercise
An app sets theme to a light ThemeData, leaves darkTheme null, and sets themeMode to ThemeMode.dark. Which theme appears, and why?
Show answer
The light theme appears. With themeMode dark, MaterialApp wants the dark theme, but darkTheme is null, so it falls back to theme. Flutter never generates a dark theme automatically.
Exercise
You pass ColorScheme.fromSeed(seedColor: seed) as the colorScheme of darkTheme. In dark mode the app still shows light backgrounds. What is the fix?
Show answer
Add brightness: Brightness.dark to fromSeed. Its default is Brightness.light, so without the argument the dark theme receives a light color scheme.
Exercise
A widget calls Theme.of(context) inside the build method of the widget that creates MaterialApp, and the colors do not match the app theme. Why, and how do you fix it?
Show answer
That context is above MaterialApp, so the lookup finds no app theme and returns the default. Move the code to a widget below MaterialApp, or wrap it in a Builder to get a context beneath it.