On this page15 sections
  1. Prerequisites
  2. Connection With Previous Lectures
  3. Learning Objectives
  4. 1. The App We Will Build
  5. 2. ThemeData Lives on MaterialApp
  6. 3. ColorScheme.fromSeed: A Palette From One Colour
  7. 4. Theme.of(context): Using the Theme
  8. Code Examples
  9. Common Beginner Mistakes
  10. Best Practices
  11. Mini Challenge
  12. Practice Questions
  13. Practice Answers
  14. Key Takeaways
  15. Full Code (Copy-Paste Reference)

Lecture 001: What Is a Theme? ThemeData & ColorScheme

Category1: Theme Basics
Lecture001 of 008
Estimated video length14 min
TargetFlutter 3.44 · Dart 3.12
LevelBeginner

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 ThemeData is 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:

bash
flutter create my_notes

Replace 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:

dart
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.

RoleUsed for
primarythe main brand colour: key buttons, active states
onPrimarytext and icons drawn on top of primary
primaryContainera softer version, for larger areas like headers
secondaryContainercalm backgrounds for cards and chips
surface / onSurfacethe 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:

dart
final colors = Theme.of(context).colorScheme;
final text = Theme.of(context).textTheme;

Then use the roles instead of fixed colours:

dart
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

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 fromSeed do the rest.
  • Read colours by role (primary, surface), never by name (teal).
  • Store Theme.of(context).colorScheme in a local colors variable 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

  1. Which MaterialApp property holds the app's theme?
  2. What does ColorScheme.fromSeed need, and what does it produce?
  3. Which colour should text use on a primaryContainer background?
  4. How does a widget read the current text styles?
  5. Why is color: Colors.teal on a widget a problem later?

Practice Answers

  1. theme.
  2. One seed colour; a full matching colour scheme.
  3. onPrimaryContainer.
  4. Theme.of(context).textTheme.
  5. It ignores the theme, so it won't change with a new theme or in dark mode.

Key Takeaways

  • ThemeData on MaterialApp.theme styles the whole app.
  • ColorScheme.fromSeed(seedColor: ...) builds a full palette from one colour.
  • Theme.of(context).colorScheme and .textTheme give 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

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),
      ),
    );
  }
}