Sub topic 7
Theming the Whole App with ThemeData
This chapter covers how one ThemeData object on MaterialApp styles every screen, and how widgets read from it. It assumes you can run a Flutter app and write a StatelessWidget with a build method. It also assumes you know Dart named parameters and have built a MaterialApp with a Scaffold and AppBar, as in the earlier chapters.
How theming reaches your widgets
A theme is a bundle of colors, text styles, and per-component defaults. You attach it once near the root of the widget tree. Any widget below it can look it up.
MaterialApp wraps its home and routes in a Theme widget. Theme is an InheritedWidget, which is a widget that makes data available to all descendants without passing it through constructors. When an AppBar builds, it asks the nearest Theme for its background color, title style, and icon color. You never pass those values by hand.
Two things follow from this design:
- Changing the theme in one place restyles the whole app.
- A widget only sees the theme from
Themewidgets that are its ancestors. This matters in the "Common errors" section.
Generating a color scheme from a seed
A ColorScheme is a named set of colors: primary, onPrimary, secondary, surface, error, and more. Each "on" color is meant to be drawn on top of its base color, such as text on a button.
You rarely define these by hand. ColorScheme.fromSeed takes one color and generates a full, harmonious palette from it:
import 'package:flutter/material.dart';
void main() => runApp(const MyApp());
class MyApp extends StatelessWidget {
const MyApp({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Demo',
theme: ThemeData(
colorScheme: ColorScheme.fromSeed(seedColor: Colors.teal),
useMaterial3: true,
),
darkTheme: ThemeData(
colorScheme: ColorScheme.fromSeed(
seedColor: Colors.teal,
brightness: Brightness.dark,
),
useMaterial3: true,
),
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('Home')),
body: const Center(child: Text('Hello, themed Flutter')),
);
}
}The Scaffold and AppBar need no color arguments. They read the generated scheme.
About useMaterial3: in current Flutter versions Material 3 is already the default, so useMaterial3: true is redundant there. Writing it explicitly keeps the code unambiguous across versions. Older projects may still have useMaterial3: false, which gives the older Material 2 look. If your app looks different from a tutorial screenshot, check this flag first.
Light, dark, and themeMode
MaterialApp takes three related parameters:
| Parameter | Purpose |
|---|---|
| `theme` | The light theme. Also used for dark mode if `darkTheme` is null. |
| `darkTheme` | The theme used when dark mode is active. |
| `themeMode` | Which theme is active: `ThemeMode.system`, `ThemeMode.light`, or `ThemeMode.dark`. |
ThemeMode.system follows the device setting and is the default. To make the dark scheme from the same seed, pass brightness: Brightness.dark to fromSeed. If you only set theme and leave darkTheme null, the app stays light even when the device is in dark mode.
To test dark mode without changing your device settings, temporarily set themeMode: ThemeMode.dark. Remember to change it back.
Reading the theme in your own widgets
Inside any build method, Theme.of(context) returns the nearest ThemeData. Use it so your custom widgets follow the theme instead of hard-coding colors:
class InfoCard extends StatelessWidget {
const InfoCard({super.key, required this.message});
final String message;
@override
Widget build(BuildContext context) {
final theme = Theme.of(context);
final scheme = theme.colorScheme;
return Container(
padding: const EdgeInsets.all(16),
decoration: BoxDecoration(
color: scheme.primaryContainer,
borderRadius: BorderRadius.circular(12),
),
child: Text(
message,
style: theme.textTheme.titleMedium?.copyWith(
color: scheme.onPrimaryContainer,
),
),
);
}
}Points to notice:
primaryContainerandonPrimaryContainerare a matched pair. Text drawn inonPrimaryContainerstays readable on aprimaryContainerbackground in both light and dark themes.textTheme.titleMediumis nullable, so you use?.copyWith.copyWithreturns a modified copy and leaves the theme's style untouched.- This widget needs no changes when the seed color or brightness changes.
A hard-coded Colors.teal.shade100 would look wrong in dark mode. Prefer scheme roles for anything that should adapt.
Styling specific components
ThemeData has a field for each component family, such as appBarTheme, floatingActionButtonTheme, elevatedButtonTheme, and cardTheme. These set defaults for every instance of that component. For the topic of this tutorial, the useful one is AppBarTheme:
ThemeData(
colorScheme: ColorScheme.fromSeed(seedColor: Colors.teal),
useMaterial3: true,
appBarTheme: const AppBarTheme(
centerTitle: true,
elevation: 0,
scrolledUnderElevation: 2,
),
floatingActionButtonTheme: const FloatingActionButtonThemeData(
shape: StadiumBorder(),
),
)Every AppBar in the app now centers its title, without each screen setting centerTitle: true. A screen can still override one value:
AppBar(
title: const Text('Left aligned here'),
centerTitle: false,
)The order of precedence, from strongest to weakest, is:
- An argument passed directly to the widget.
- The component theme, such as
appBarTheme. - The defaults derived from
colorSchemeand the Material 3 spec.
This ordering is the main reason to prefer component themes. Set the shared look once, and let individual widgets override only what is special.
Use the same approach for text. textTheme on ThemeData can be adjusted for the whole app. A common pattern is to start from the generated text theme and change only a few styles, with Theme.of(context).textTheme.apply(...) or copyWith. Do this only when you have a design reason. The defaults already meet Material accessibility guidance.
Overriding the theme for one subtree
Sometimes one section of a screen needs a different look, such as a red warning panel. Wrap that section in a Theme widget and derive its data from the current theme:
Theme(
data: Theme.of(context).copyWith(
colorScheme: ColorScheme.fromSeed(
seedColor: Colors.red,
brightness: Theme.of(context).brightness,
),
),
child: const WarningPanel(),
)Using Theme.of(context).copyWith(...) keeps everything you did not change. Writing ThemeData(...) from scratch here would throw away your appBarTheme and other customizations inside that subtree.
Pass the current brightness as shown, or the red scheme will always be light, even inside a dark app.
Note that a Builder or a new widget class is needed if WarningPanel must read the new theme. A widget can only see a Theme that is above it in the tree. The widget that creates the Theme cannot see its own override through its own context.
Common errors
Reading the theme in the same build that creates MaterialApp. The context passed to MyApp.build sits above MaterialApp, so there is no app theme above it yet. Theme.of returns the framework's default theme instead.
class MyApp extends StatelessWidget {
const MyApp({super.key});
@override
Widget build(BuildContext context) {
// Wrong: this context is above MaterialApp, so this is the default theme.
final primary = Theme.of(context).colorScheme.primary;
return MaterialApp(
theme: ThemeData(colorScheme: ColorScheme.fromSeed(seedColor: Colors.teal)),
home: Scaffold(
body: Center(child: Text('Wrong color', style: TextStyle(color: primary))),
),
);
}
}The fix is to read the theme in a widget below MaterialApp, such as HomePage, or to wrap the code in a Builder.
Hard-coded colors that break dark mode. A white Container with black text works in light mode. In dark mode it glares, and any theme-colored text inside it may be unreadable. Use scheme.surface, scheme.onSurface, and the container roles.
Dark theme never appears. Check that darkTheme is set, that themeMode is not ThemeMode.light, and that the device or emulator is actually in dark mode.
Changing the theme and seeing nothing. Theme edits normally show up after a hot reload. If they do not, make sure you edited the ThemeData that MaterialApp actually uses. Apps with several Theme widgets can hide the one you changed.
A different seed gives an unexpected look. fromSeed derives tones from the seed, so primary is often a lighter or darker shade of your color, not the exact color. If you need an exact brand color in one place, use copyWith on the scheme:
final scheme = ColorScheme.fromSeed(seedColor: const Color(0xFF00796B))
.copyWith(primary: const Color(0xFF00796B));Do this sparingly. Overriding single roles can break the contrast pairing with the matching "on" color.
Where this leads
The full set of MaterialApp features you have seen now includes title, theme, darkTheme, themeMode, and home. As the app grows, many teams move the ThemeData objects into their own file, for example lib/theme/app_theme.dart, with static light and dark getters. MaterialApp then reads theme: AppTheme.light. A later chapter on organizing files covers this.
Exercise
Your app sets only theme: ThemeData(colorScheme: ColorScheme.fromSeed(seedColor: Colors.teal)) and no darkTheme. The device is switched to dark mode, and themeMode is the default. What do users see, and what two changes make the app follow dark mode with the same teal seed?
Show answer
The app stays light. With ThemeMode.system and a null darkTheme, Flutter falls back to theme. To fix it, add a darkTheme: ThemeData(colorScheme: ColorScheme.fromSeed(seedColor: Colors.teal, brightness: Brightness.dark)). Keep themeMode: ThemeMode.system (the default, which can also be written explicitly) so the device setting chooses between the two.
Exercise
A Text widget inside a custom card uses Colors.black87 on a Colors.white background. Rewrite the two colors so the card adapts to light and dark themes, and name the ThemeData property you read them from.
Show answer
Read the scheme with final scheme = Theme.of(context).colorScheme;. Use scheme.surface (or scheme.surfaceContainer in newer versions) for the background and scheme.onSurface for the text. The colorScheme property of ThemeData supplies both, and each "on" color is designed to be readable on its matching base color in both brightness modes.
Exercise
You want every AppBar in the app to center its title, but one settings screen must left-align it. Where do you set each, and which value wins on the settings screen?
Show answer
Set appBarTheme: const AppBarTheme(centerTitle: true) in the app's ThemeData. On the settings screen, pass centerTitle: false directly to that AppBar. The argument on the widget has higher precedence than the component theme, so the settings screen is left-aligned and all other screens stay centered.
Exercise
Why does Theme.of(context).colorScheme.primary return the default Flutter color when called at the top of MyApp.build, the method that returns MaterialApp? Give two ways to get your own theme's color.
Show answer
The context in MyApp.build belongs to MyApp, which is above MaterialApp and therefore above the Theme that MaterialApp creates. Theme.of only finds ancestors. The color can be read instead in a widget built below MaterialApp, such as HomePage, or inside a Builder placed in home, whose context is below the theme.