Sub topic 7
Theming the Whole App with ThemeData
This chapter assumes you know basic Dart syntax, can run a Flutter app, and understand StatelessWidget and build. You will learn how to define one visual system for an entire Material app.
Why use an app-wide theme?
A theme is a set of visual choices that widgets can share. It can define colors, text styles, button shapes, input decoration, and other defaults. Without a theme, each screen may choose its own values, which makes an app harder to change consistently.
ThemeData is Flutter’s object for storing Material widget defaults. You usually provide it to MaterialApp:
MaterialApp(
theme: ThemeData(
colorScheme: ColorScheme.fromSeed(
seedColor: Colors.teal,
),
useMaterial3: true,
),
home: const HomePage(),
)The theme belongs above the screens that use it in the widget tree. Scaffold, AppBar, buttons, text, and other descendants can read the theme from the nearest MaterialApp.
Build a color scheme from a seed color
Material 3 uses a ColorScheme to describe related roles such as primary, surface, error, and onPrimary. A color role describes where a color belongs and which foreground color should be readable on it.
ColorScheme.fromSeed creates a coordinated scheme from one starting color. This is usually easier to maintain than manually selecting every color.
import 'package:flutter/material.dart';
void main() {
runApp(const InboxApp());
}
class InboxApp extends StatelessWidget {
const InboxApp({super.key});
@override
Widget build(BuildContext context) {
const seed = Colors.teal;
return MaterialApp(
title: 'Inbox',
theme: ThemeData(
colorScheme: ColorScheme.fromSeed(
seedColor: seed,
brightness: Brightness.light,
),
useMaterial3: true,
),
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('Inbox'),
backgroundColor: colors.primaryContainer,
),
body: Center(
child: FilledButton(
onPressed: () {},
child: const Text('Compose'),
),
),
);
}
}Theme.of(context) looks up the theme above the current widget. Reading colorScheme lets a widget use the same color roles as the rest of the app.
| Theme value | Typical purpose |
|---|---|
| `primary` | Main actions and emphasis |
| `surface` | Cards, sheets, and app backgrounds |
| `error` | Error states and destructive feedback |
| `onPrimary` | Text or icons placed on `primary` |
Support light and dark themes
A production app can provide both theme and darkTheme. themeMode decides which one Flutter uses.
MaterialApp(
theme: ThemeData(
colorScheme: ColorScheme.fromSeed(
seedColor: Colors.teal,
brightness: Brightness.light,
),
useMaterial3: true,
),
darkTheme: ThemeData(
colorScheme: ColorScheme.fromSeed(
seedColor: Colors.teal,
brightness: Brightness.dark,
),
useMaterial3: true,
),
themeMode: ThemeMode.system,
home: const HomePage(),
)ThemeMode.system follows the device setting. ThemeMode.light and ThemeMode.dark force a particular mode.
Common errors
- Defining the theme below a widget that needs it: A theme only affects widgets below it in the tree.
- Mixing Material 2 and Material 3 assumptions: Set
useMaterial3: truewhen building a Material 3 interface, then test the actual widget appearance. - Using the seed color everywhere: The seed creates a full scheme. It is not meant to replace every semantic color role.
- Forgetting dark-theme contrast: Check text, icons, disabled controls, and custom surfaces in both brightness modes.
- Reading the theme with the wrong context: Use a
BuildContextbelonging to a descendant ofMaterialApp, not a context created above it.
Exercise
Which themeMode value makes Flutter follow the device's light or dark setting?
Show answer
ThemeMode.system.