Flutter Themes
Hard-coding colours and font sizes in every widget quickly becomes a mess. A theme is a single place where you describe how your app looks — colours, typography, button shapes — and every Material widget picks those values up automatically. Change the theme and the whole app updates.
Setting a theme on MaterialApp
MaterialApp takes a theme (a ThemeData). The easiest way to get a harmonious Material 3 palette is ColorScheme.fromSeed: you pick one seed colour and Flutter generates matching primary, secondary, surface and error colours.
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: 'Themed app',
theme: ThemeData(
colorScheme: ColorScheme.fromSeed(seedColor: Colors.green),
useMaterial3: true,
),
home: Scaffold(
appBar: AppBar(title: const Text('Garden')),
body: Center(
child: FilledButton(onPressed: () {}, child: const Text('Water plants')),
),
floatingActionButton: FloatingActionButton(
onPressed: () {},
child: const Icon(Icons.add),
),
),
);
}
}Notice the button and FAB use green tones even though no widget mentions a colour.
Reading the theme in your widgets
Inside build, Theme.of(context) returns the nearest ThemeData. Use it instead of literal colours so your custom widgets match the rest of the app.
class InfoBanner extends StatelessWidget {
const InfoBanner({super.key, required this.message});
final String message;
@override
Widget build(BuildContext context) {
final theme = Theme.of(context);
return Container(
padding: const EdgeInsets.all(16),
color: theme.colorScheme.primaryContainer,
child: Text(
message,
style: theme.textTheme.titleMedium?.copyWith(
color: theme.colorScheme.onPrimaryContainer,
),
),
);
}
}The colour scheme follows a naming pattern: every background colour such as primary, primaryContainer or surface has an "on" partner (onPrimary, onPrimaryContainer, onSurface) meant for text and icons drawn on top of it — that guarantees readable contrast.
Text themes
theme.textTheme provides a type scale:
| Style | Typical use |
|---|---|
displayLarge … displaySmall |
Huge numbers, hero text |
headlineLarge … headlineSmall |
Screen headings |
titleLarge … titleSmall |
App bar titles, card titles |
bodyLarge … bodySmall |
Paragraph text |
labelLarge … labelSmall |
Buttons, captions |
Dark mode
Provide a second theme as darkTheme and choose with themeMode. ThemeMode.system follows the phone's setting.
MaterialApp(
theme: ThemeData(
colorScheme: ColorScheme.fromSeed(seedColor: Colors.indigo),
),
darkTheme: ThemeData(
colorScheme: ColorScheme.fromSeed(
seedColor: Colors.indigo,
brightness: Brightness.dark,
),
),
themeMode: ThemeMode.dark, // or .light / .system
home: const HomeScreen(),
);Overriding the theme for one subtree
Wrap part of the tree in a Theme widget to change it locally — for example a red "danger zone" section:
Theme(
data: Theme.of(context).copyWith(
colorScheme: ColorScheme.fromSeed(seedColor: Colors.red),
),
child: FilledButton(onPressed: () {}, child: const Text('Delete account')),
)Tip: Component themes such as
appBarTheme,cardThemeorfilledButtonThemeinsideThemeDatalet you style every widget of one kind in one place.
Test yourself
What does ColorScheme.fromSeed(seedColor: Colors.green) do?
- Makes every widget exactly the same green
- Generates a full, harmonious colour scheme from one colour
- Loads a theme from the internet
- Only changes the AppBar
How do you read the current theme inside a widget?
ThemeData()Theme.of(context)MaterialApp.themecontext.colors
Which colour should text on a primary background use?
- primary
- secondary
- onPrimary
- surface