On this page14 sections
  1. Prerequisites
  2. Connection With Previous Lectures
  3. Learning Objectives
  4. 1. A buildTheme Function
  5. 2. textTheme
  6. 3. Component Themes
  7. Code Examples
  8. Common Beginner Mistakes
  9. Best Practices
  10. Mini Challenge
  11. Practice Questions
  12. Practice Answers
  13. Key Takeaways
  14. Full Code (Copy-Paste Reference)

Lecture 002: Text & Component Themes

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

A colour scheme sets the palette. Component themes set how each kind of widget looks: every AppBar centred, every Card rounded, 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 ...ThemeData classes (CardThemeData)

1. A buildTheme Function

Themes grow quickly. Keep them out of MaterialApp in their own function:

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

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

dart
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),
  ),
),
PropertyStyles every…Class
appBarThemeAppBarAppBarTheme
cardThemeCardCardThemeData
elevatedButtonThemeElevatedButtonElevatedButtonThemeData
inputDecorationThemeTextField decorationInputDecorationThemeData
floatingActionButtonThemeFloatingActionButtonFloatingActionButtonThemeData

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

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

dart
ThemeData(cardTheme: const CardTheme(elevation: 0))
code
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

  1. Why move the theme into a buildTheme() function?
  2. If you set only fontWeight for headlineSmall, what happens to its size?
  3. Which property styles every Card, and which class does it take?
  4. What does foregroundColor control in AppBarTheme?
  5. A single card needs a different colour. Where do you set it?

Practice Answers

  1. Themes get long; a function keeps MaterialApp readable and lets themes be reused.
  2. It keeps the default size. Your style is merged with the default.
  3. cardTheme, with a CardThemeData.
  4. The colour of the title text and icons.
  5. On that Card itself (Card(color: ...)); the theme still styles all the others.

Key Takeaways

  • textTheme adjusts named text styles; changes merge with the defaults.
  • Component themes (appBarTheme, cardTheme, elevatedButtonTheme…) style every widget of that type.
  • Current Flutter uses ...ThemeData classes such as CardThemeData.
  • 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

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