Sub topic 10
Letting users switch between light, dark and system
Assume you know basic Dart null safety, can build Flutter widget trees, and understand MaterialApp, Scaffold, and BuildContext. This chapter focuses on changing the active theme while the app is running.
How `ThemeMode` selects a theme
MaterialApp accepts three related properties:
themeis the lightThemeData.darkThemeis the darkThemeData.themeModechooses which one is active.
ThemeMode.light always uses theme. ThemeMode.dark uses darkTheme. ThemeMode.system follows the operating system's light or dark setting. If the app uses ThemeMode.dark but does not provide darkTheme, Flutter uses the light theme with its brightness adjusted, which is usually less complete than defining a real dark theme.
The value of themeMode must be state that can change. When it changes, MaterialApp must rebuild.
Build a runtime switcher
The following example uses a ValueNotifier. A ValueNotifier stores one value and notifies listeners when that value changes. ValueListenableBuilder listens to it and rebuilds MaterialApp.
Keep the notifier above MaterialApp, because the settings control must be able to change the object that owns the app theme.
import 'package:flutter/material.dart';
void main() {
runApp(const App());
}
final themeModeNotifier = ValueNotifier<ThemeMode>(ThemeMode.system);
class App extends StatelessWidget {
const App({super.key});
@override
Widget build(BuildContext context) {
const seed = Color(0xFF6750A4);
return ValueListenableBuilder<ThemeMode>(
valueListenable: themeModeNotifier,
builder: (context, mode, child) {
return MaterialApp(
title: 'Theme switcher',
theme: ThemeData(
colorScheme: ColorScheme.fromSeed(seedColor: seed),
),
darkTheme: ThemeData(
colorScheme: ColorScheme.fromSeed(
seedColor: seed,
brightness: Brightness.dark,
),
),
themeMode: mode,
home: const SettingsPage(),
);
},
);
}
}
class SettingsPage extends StatelessWidget {
const SettingsPage({super.key});
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: const Text('Settings')),
body: Center(
child: SegmentedButton<ThemeMode>(
segments: const [
ButtonSegment(
value: ThemeMode.light,
label: Text('Light'),
),
ButtonSegment(
value: ThemeMode.system,
label: Text('System'),
),
ButtonSegment(
value: ThemeMode.dark,
label: Text('Dark'),
),
],
selected: {themeModeNotifier.value},
onSelectionChanged: (selection) {
themeModeNotifier.value = selection.first;
},
),
),
);
}
}SegmentedButton reports a set because it can support multiple selected values. This example allows only one selection, so it reads selection.first.
The page does not manually repaint its children. Flutter rebuilds MaterialApp, updates the inherited Theme widget, and then rebuilds widgets that read the theme.
Common errors
A frequent mistake is changing a local variable without rebuilding MaterialApp:
ThemeMode mode = ThemeMode.dark;Changing mode later does not notify Flutter. Use setState, a ValueNotifier, or another state-management solution.
Another mistake is placing the switcher outside the widget tree that owns the state. Put shared theme state above MaterialApp, or pass a callback down to the settings page.
Theme changes may animate. You can control the transition with themeAnimationDuration and themeAnimationCurve on MaterialApp. Keep the duration short enough that the interface still feels responsive.
Exercise
Why does changing a ThemeMode field alone fail to update the app, and what makes the example rebuild MaterialApp?
Show answer
A plain field does not notify Flutter. ValueNotifier notifies ValueListenableBuilder, which rebuilds MaterialApp with the new themeMode.