On this page14 sections
Lecture 002: Text & Component Themes
| Category | 1: Theme Basics |
| Lecture | 002 of 008 |
| Estimated video length | 14 min |
| Target | Flutter 3.44 · Dart 3.12 |
| Level | Beginner |
A colour scheme sets the palette. Component themes set how each kind of widget looks: every
AppBarcentred, everyCardrounded, every button the same size. Style them once in the theme, and you never style them one by one again.
Contents
Prerequisites · Connection · Objectives · Lesson · Code Examples · Common Mistakes · Best Practices · Mini Challenge · Practice Questions · Answers · Takeaways · Full Code
Prerequisites
Lecture 001 (ThemeData, ColorScheme.fromSeed, Theme.of).
Connection With Previous Lectures
In Lecture 001, HomeScreen set the app bar colour itself (backgroundColor: colors.primaryContainer). Here that styling moves into the theme, so widgets stay plain.
Learning Objectives
- Move theme code into a
buildTheme()function - Customise text styles with
textTheme - Style all app bars, cards and buttons with component themes
- Know that component themes use
...ThemeDataclasses (CardThemeData)
1. A buildTheme Function
Themes grow quickly. Keep them out of MaterialApp in their own function:
ThemeData buildTheme() {
final colors = ColorScheme.fromSeed(seedColor: Colors.teal);
return ThemeData(
colorScheme: colors,
// text and component themes go here
);
}Then MaterialApp(theme: buildTheme(), ...). Creating colors first lets the component themes
below use the same palette.
2. textTheme
You only list the styles you want to change. Everything else keeps Flutter's defaults:
textTheme: const TextTheme(
headlineSmall: TextStyle(fontWeight: FontWeight.bold),
bodyLarge: TextStyle(fontSize: 17, height: 1.4),
),Your changes are merged with the defaults: headlineSmall keeps its normal size and colour
and just becomes bold.
3. Component Themes
Each widget type has its own theme property:
appBarTheme: AppBarTheme(
backgroundColor: colors.primary,
foregroundColor: colors.onPrimary, // title and icon colour
centerTitle: true,
),
cardTheme: CardThemeData(
elevation: 0,
color: colors.secondaryContainer,
shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(16)),
),
elevatedButtonTheme: ElevatedButtonThemeData(
style: ElevatedButton.styleFrom(
backgroundColor: colors.primary,
foregroundColor: colors.onPrimary,
padding: const EdgeInsets.symmetric(horizontal: 24, vertical: 14),
),
),| Property | Styles every… | Class |
|---|---|---|
appBarTheme | AppBar | AppBarTheme |
cardTheme | Card | CardThemeData |
elevatedButtonTheme | ElevatedButton | ElevatedButtonThemeData |
inputDecorationTheme | TextField decoration | InputDecorationThemeData |
floatingActionButtonTheme | FloatingActionButton | FloatingActionButtonThemeData |
Now the widgets need no styling at all: AppBar(title: Text('My Notes')), Card(child: ...).
Code Examples
Example 1: My Notes With Component Themes
lib/main.dart
import 'package:flutter/material.dart';
void main() {
runApp(const NotesApp());
}
ThemeData buildTheme() {
final colors = ColorScheme.fromSeed(seedColor: Colors.teal);
return ThemeData(
colorScheme: colors,
textTheme: const TextTheme(
headlineSmall: TextStyle(fontWeight: FontWeight.bold),
bodyLarge: TextStyle(fontSize: 17, height: 1.4),
),
appBarTheme: AppBarTheme(
backgroundColor: colors.primary,
foregroundColor: colors.onPrimary,
centerTitle: true,
),
cardTheme: CardThemeData(
elevation: 0,
color: colors.secondaryContainer,
shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(16)),
),
elevatedButtonTheme: ElevatedButtonThemeData(
style: ElevatedButton.styleFrom(
backgroundColor: colors.primary,
foregroundColor: colors.onPrimary,
padding: const EdgeInsets.symmetric(horizontal: 24, vertical: 14),
),
),
);
}
class NotesApp extends StatelessWidget {
const NotesApp({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'My Notes',
debugShowCheckedModeBanner: false,
theme: buildTheme(),
home: const HomeScreen(),
);
}
}
class HomeScreen extends StatelessWidget {
const HomeScreen({super.key});
@override
Widget build(BuildContext context) {
final text = Theme.of(context).textTheme;
final notes = ['Buy milk and bread', 'Call the plumber', 'Revise Lecture 005'];
return Scaffold(
appBar: AppBar(title: const Text('My Notes')),
body: ListView(
padding: const EdgeInsets.all(16),
children: [
Text('Today', style: text.headlineSmall),
const SizedBox(height: 12),
for (final note in notes)
Card(
child: Padding(
padding: const EdgeInsets.all(16),
child: Text(note, style: text.bodyLarge),
),
),
const SizedBox(height: 16),
ElevatedButton(onPressed: () {}, child: const Text('Add note')),
],
),
);
}
}What you'll see: a solid teal app bar with a centred white title, a bold "Today" heading,
three rounded flat note cards and a teal "Add note" button. HomeScreen contains no colours at
all; everything comes from buildTheme().
Common Beginner Mistakes
1. Using CardTheme instead of CardThemeData
ThemeData(cardTheme: const CardTheme(elevation: 0))error • The argument type 'CardTheme' can't be assigned to the parameter type 'CardThemeData?'.Older tutorials use CardTheme. Current Flutter expects CardThemeData (and
DialogThemeData, TabBarThemeData, and so on).
2. Styling the widget anyway: Card(color: Colors.white, ...) overrides the card theme for
that one card. Set it in the theme, unless one card really must be different.
3. Forgetting foregroundColor: changing appBarTheme.backgroundColor to a dark colour
without foregroundColor: colors.onPrimary can leave a dark title on a dark bar.
Best Practices
- One
buildTheme()function, ideally in its own file (lib/theme.dart) as it grows. - Build component themes from
colors.…roles, not fixed colours. - Override only the text styles you need; the defaults are well balanced.
Mini Challenge
Add an inputDecorationTheme that gives every TextField a filled, rounded border
(filled: true, border: OutlineInputBorder(...)), then add a TextField to HomeScreen with
no decoration of its own and check it picks up the style.
Practice Questions
- Why move the theme into a
buildTheme()function? - If you set only
fontWeightforheadlineSmall, what happens to its size? - Which property styles every
Card, and which class does it take? - What does
foregroundColorcontrol inAppBarTheme? - A single card needs a different colour. Where do you set it?
Practice Answers
- Themes get long; a function keeps
MaterialAppreadable and lets themes be reused. - It keeps the default size. Your style is merged with the default.
cardTheme, with aCardThemeData.- The colour of the title text and icons.
- On that
Carditself (Card(color: ...)); the theme still styles all the others.
Key Takeaways
textThemeadjusts named text styles; changes merge with the defaults.- Component themes (
appBarTheme,cardTheme,elevatedButtonTheme…) style every widget of that type. - Current Flutter uses
...ThemeDataclasses such asCardThemeData. - With a good theme, screens contain layout, not colours.
Full Code (Copy-Paste Reference)
Everything the app needs up to and including this lecture. One file so far.
lib/main.dart
import 'package:flutter/material.dart';
void main() {
runApp(const NotesApp());
}
/// Keeps MaterialApp readable as the theme grows; reused wherever a theme is needed.
ThemeData buildTheme() {
final colors = ColorScheme.fromSeed(seedColor: Colors.teal);
return ThemeData(
colorScheme: colors,
// Only the styles listed here change; everything else keeps Flutter's defaults.
textTheme: const TextTheme(
headlineSmall: TextStyle(fontWeight: FontWeight.bold),
bodyLarge: TextStyle(fontSize: 17, height: 1.4),
),
appBarTheme: AppBarTheme(
backgroundColor: colors.primary,
foregroundColor: colors.onPrimary, // title + icon colour
centerTitle: true,
),
cardTheme: CardThemeData(
elevation: 0,
color: colors.secondaryContainer,
shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(16)),
),
elevatedButtonTheme: ElevatedButtonThemeData(
style: ElevatedButton.styleFrom(
backgroundColor: colors.primary,
foregroundColor: colors.onPrimary,
padding: const EdgeInsets.symmetric(horizontal: 24, vertical: 14),
),
),
);
}
class NotesApp extends StatelessWidget {
const NotesApp({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'My Notes',
debugShowCheckedModeBanner: false,
theme: buildTheme(),
home: const HomeScreen(),
);
}
}
class HomeScreen extends StatelessWidget {
const HomeScreen({super.key});
@override
Widget build(BuildContext context) {
final text = Theme.of(context).textTheme;
final notes = ['Buy milk and bread', 'Call the plumber', 'Revise Lecture 005'];
return Scaffold(
// No colour set here - appBarTheme already styles every AppBar.
appBar: AppBar(title: const Text('My Notes')),
body: ListView(
padding: const EdgeInsets.all(16),
children: [
Text('Today', style: text.headlineSmall),
const SizedBox(height: 12),
for (final note in notes)
// No colour or shape set here either - cardTheme supplies both.
Card(
child: Padding(
padding: const EdgeInsets.all(16),
child: Text(note, style: text.bodyLarge),
),
),
const SizedBox(height: 16),
ElevatedButton(onPressed: () {}, child: const Text('Add note')),
],
),
);
}
}