On this page15 sections
- Prerequisites
- Connection With Previous Lectures
- Learning Objectives
- 1. The App We Will Build
- 2. ThemeData Lives on MaterialApp
- 3. ColorScheme.fromSeed: A Palette From One Colour
- 4. Theme.of(context): Using the Theme
- Code Examples
- Common Beginner Mistakes
- Best Practices
- Mini Challenge
- Practice Questions
- Practice Answers
- Key Takeaways
- Full Code (Copy-Paste Reference)
Lecture 001: What Is a Theme? ThemeData & ColorScheme
| Category | 1: Theme Basics |
| Lecture | 001 of 008 |
| Estimated video length | 14 min |
| Target | Flutter 3.44 · Dart 3.12 |
| Level | Beginner |
Picking a colour for every button, title and card by hand is slow, and the app ends up looking patchy. A theme describes the look of the whole app in one place. Change it once, and every screen follows.
Contents
Prerequisites · Connection · Objectives · Lesson · Code Examples · Common Mistakes · Best Practices · Mini Challenge · Practice Questions · Answers · Takeaways · Full Code
Prerequisites
- A running Flutter app:
MaterialApp,Scaffold,AppBar,Text,Column(Essential Widgets) - Dart classes and named parameters
Connection With Previous Lectures
This is the first lecture of the course. In Essential Widgets you styled widgets one by one
(color: Colors.blue). From now on, colours and text styles come from the theme instead.
Learning Objectives
- Explain what
ThemeDatais and where it goes - Build a full colour scheme from one colour with
ColorScheme.fromSeed - Read colours and text styles anywhere with
Theme.of(context) - Know the main colour roles:
primary,onPrimary,primaryContainer,secondaryContainer
1. The App We Will Build
Through the course we'll build My Notes: a simple notes screen whose look the user can change, with light and dark mode and an accent colour, all remembered after the app closes.
Create a new project:
flutter create my_notesReplace everything in lib/main.dart with the code in this lecture.
2. ThemeData Lives on MaterialApp
MaterialApp has a theme property. Whatever ThemeData you give it applies to every screen
underneath:
MaterialApp(
title: 'My Notes',
theme: ThemeData(
colorScheme: ColorScheme.fromSeed(seedColor: Colors.teal),
),
home: const HomeScreen(),
)3. ColorScheme.fromSeed: A Palette From One Colour
Choosing 30 matching colours by hand is hard. ColorScheme.fromSeed takes one seed colour and
generates a complete, matching palette (Material 3), with good contrast built in.
| Role | Used for |
|---|---|
primary | the main brand colour: key buttons, active states |
onPrimary | text and icons drawn on top of primary |
primaryContainer | a softer version, for larger areas like headers |
secondaryContainer | calm backgrounds for cards and chips |
surface / onSurface | the page background and normal text |
Every colour has an on partner (onPrimary, onSecondaryContainer) that is readable on top
of it. Always pair them.
4. Theme.of(context): Using the Theme
Any widget can ask for the current theme:
final colors = Theme.of(context).colorScheme;
final text = Theme.of(context).textTheme;Then use the roles instead of fixed colours:
Text('Today', style: text.titleLarge),
Container(
color: colors.secondaryContainer,
child: Text(
'Buy milk and bread',
style: text.bodyLarge?.copyWith(color: colors.onSecondaryContainer),
),
)textTheme holds ready-made styles: headlineSmall, titleLarge, bodyLarge, labelSmall and
more. Each can be null in theory, so use ?. before copyWith.
Code Examples
Example 1: My Notes, First Version
lib/main.dart
import 'package:flutter/material.dart';
void main() {
runApp(const NotesApp());
}
class NotesApp extends StatelessWidget {
const NotesApp({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'My Notes',
debugShowCheckedModeBanner: false,
theme: ThemeData(
colorScheme: ColorScheme.fromSeed(seedColor: Colors.teal),
),
home: const HomeScreen(),
);
}
}
class HomeScreen extends StatelessWidget {
const HomeScreen({super.key});
@override
Widget build(BuildContext context) {
final colors = Theme.of(context).colorScheme;
final text = Theme.of(context).textTheme;
return Scaffold(
appBar: AppBar(
title: const Text('My Notes'),
backgroundColor: colors.primaryContainer,
),
body: Padding(
padding: const EdgeInsets.all(16),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text('Today', style: text.titleLarge),
const SizedBox(height: 8),
Container(
padding: const EdgeInsets.all(16),
decoration: BoxDecoration(
color: colors.secondaryContainer,
borderRadius: BorderRadius.circular(12),
),
child: Text(
'Buy milk and bread',
style: text.bodyLarge?.copyWith(color: colors.onSecondaryContainer),
),
),
],
),
),
floatingActionButton: FloatingActionButton(
onPressed: () {},
child: const Icon(Icons.add),
),
);
}
}What you'll see: a teal app bar, a "Today" heading, a soft teal note card and a teal action
button. Nothing uses a fixed colour except the seed. Change Colors.teal to Colors.indigo, hot
reload, and the whole app turns indigo.
Common Beginner Mistakes
1. Hard-coding colours in widgets: color: Colors.teal on each widget ignores the theme, so
changing the theme (or switching to dark mode in Lecture 003) won't touch it. Use
Theme.of(context).colorScheme....
2. Text that can't be read: primary text on a primary background disappears. Use the
matching on colour: onPrimary on primary, onSecondaryContainer on secondaryContainer.
3. Calling Theme.of(context) with the wrong context: inside NotesApp.build, the context
is above MaterialApp, so it doesn't see your theme yet. Read the theme in widgets below
MaterialApp, like HomeScreen.
Best Practices
- Keep one seed colour as your brand colour; let
fromSeeddo the rest. - Read colours by role (
primary,surface), never by name (teal). - Store
Theme.of(context).colorSchemein a localcolorsvariable to keep code short.
Mini Challenge
Add a second note card below the first that uses tertiaryContainer and
onTertiaryContainer. Then try three different seed colours and pick your favourite.
Practice Questions
- Which
MaterialAppproperty holds the app's theme? - What does
ColorScheme.fromSeedneed, and what does it produce? - Which colour should text use on a
primaryContainerbackground? - How does a widget read the current text styles?
- Why is
color: Colors.tealon a widget a problem later?
Practice Answers
theme.- One seed colour; a full matching colour scheme.
onPrimaryContainer.Theme.of(context).textTheme.- It ignores the theme, so it won't change with a new theme or in dark mode.
Key Takeaways
ThemeDataonMaterialApp.themestyles the whole app.ColorScheme.fromSeed(seedColor: ...)builds a full palette from one colour.Theme.of(context).colorSchemeand.textThemegive colours and text styles anywhere.- Pair every colour with its on colour for readable text.
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());
}
class NotesApp extends StatelessWidget {
const NotesApp({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'My Notes',
debugShowCheckedModeBanner: false,
// One seed colour generates the whole matching palette (Material 3).
theme: ThemeData(
colorScheme: ColorScheme.fromSeed(seedColor: Colors.teal),
),
home: const HomeScreen(),
);
}
}
class HomeScreen extends StatelessWidget {
const HomeScreen({super.key});
@override
Widget build(BuildContext context) {
// Theme.of(context) works here because this widget sits BELOW MaterialApp.
final colors = Theme.of(context).colorScheme;
final text = Theme.of(context).textTheme;
return Scaffold(
appBar: AppBar(
title: const Text('My Notes'),
backgroundColor: colors.primaryContainer,
),
body: Padding(
padding: const EdgeInsets.all(16),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text('Today', style: text.titleLarge),
const SizedBox(height: 8),
Container(
padding: const EdgeInsets.all(16),
decoration: BoxDecoration(
color: colors.secondaryContainer,
borderRadius: BorderRadius.circular(12),
),
child: Text(
'Buy milk and bread',
// Always pair a container colour with its "on" partner for readable text.
style: text.bodyLarge?.copyWith(color: colors.onSecondaryContainer),
),
),
],
),
),
floatingActionButton: FloatingActionButton(
onPressed: () {},
child: const Icon(Icons.add),
),
);
}
}