Sub topic 7
Wiring `theme`, `darkTheme`, and `themeMode`
This chapter assumes you know basic Dart null safety, can build Flutter widget trees, and understand MaterialApp, Scaffold, and BuildContext. You will connect two Material 3 themes to MaterialApp, then choose which theme the app displays.
How the three properties work
MaterialApp accepts three related properties:
themeis the light theme.darkThemeis the dark theme.themeModechooses between them.
ThemeMode.light always selects theme. ThemeMode.dark selects darkTheme. ThemeMode.system checks the device's current brightness and selects the matching theme.
The themes are ThemeData objects. Each one contains values such as a ColorScheme, TextTheme, and component defaults.
Configure both themes
Create a light and dark ColorScheme from the same seed color. The seed keeps the two themes visually related, while the brightness argument produces appropriate color roles for each mode.
import 'package:flutter/material.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
@override
Widget build(BuildContext context) {
const seedColor = Color(0xFF6750A4);
final lightTheme = ThemeData(
colorScheme: ColorScheme.fromSeed(
seedColor: seedColor,
),
useMaterial3: true,
);
final darkTheme = ThemeData(
colorScheme: ColorScheme.fromSeed(
seedColor: seedColor,
brightness: Brightness.dark,
),
useMaterial3: true,
);
return MaterialApp(
title: 'Theme example',
theme: lightTheme,
darkTheme: darkTheme,
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('Theme example'),
),
body: Center(
child: Text(
'The app follows the device theme.',
style: Theme.of(context).textTheme.bodyLarge,
),
),
);
}
}useMaterial3: true enables Material 3 component and color behavior. Recent Flutter versions commonly use Material 3 by default, but setting it explicitly can make the app's intent clear.
Understand selection behavior
The following table shows what the user sees for each value.
| `themeMode` | Theme selected |
|---|---|
| `ThemeMode.light` | `theme` |
| `ThemeMode.dark` | `darkTheme` |
| `ThemeMode.system` | `theme` or `darkTheme` based on device brightness |
With ThemeMode.system, Flutter listens for platform brightness changes. If the device switches from light to dark mode while the app is running, MaterialApp rebuilds with the other theme.
A common mistake is setting themeMode: ThemeMode.dark while testing and then assuming the app supports automatic switching. Use ThemeMode.system for platform-controlled behavior. Use a fixed mode when the app or a user setting must control the appearance.
Switch the mode at runtime
MaterialApp must rebuild when the selected mode changes. A stateful parent can hold the current ThemeMode and pass it to MaterialApp.
class ThemeApp extends StatefulWidget {
const ThemeApp({super.key});
@override
State<ThemeApp> createState() => _ThemeAppState();
}
class _ThemeAppState extends State<ThemeApp> {
ThemeMode mode = ThemeMode.system;
@override
Widget build(BuildContext context) {
return MaterialApp(
theme: ThemeData(
colorScheme: ColorScheme.fromSeed(
seedColor: const Color(0xFF6750A4),
),
),
darkTheme: ThemeData(
colorScheme: ColorScheme.fromSeed(
seedColor: const Color(0xFF6750A4),
brightness: Brightness.dark,
),
),
themeMode: mode,
home: SettingsPage(
mode: mode,
onModeChanged: (newMode) {
setState(() {
mode = newMode;
});
},
),
);
}
}In a larger app, keep this state above the pages that need to change it. You can later save mode with a preference store, then load it before building the app.
Exercise
Which themeMode value should you use when the app must follow the device's light or dark setting?
Show answer
Use ThemeMode.system, with a light theme and a dark darkTheme.