Sub topic 2
ThemeData: the Single Source of Theme Values
This chapter assumes basic Dart null safety, Flutter widget trees, MaterialApp, Scaffold, and BuildContext. You will use those concepts to make an app whose visual rules come from one shared theme instead of scattered values.
What ThemeData provides
ThemeData is a configuration object for Material widgets. It stores values such as:
- The app's
ColorScheme - Typography through
TextTheme - Default settings for components such as app bars and buttons
- Shape, spacing, density, and visual effects
- Whether Material 3 behavior is enabled
MaterialApp places the theme in the widget tree. Material widgets below it can then use the same configuration. This makes the theme a single source of truth: changing one value can update many screens.
Flutter's current Material design system is Material 3. Set useMaterial3: true explicitly when you want to document that choice in your code. Recent Flutter versions use Material 3 by default, but an explicit value makes the app's intent clear.
import 'package:flutter/material.dart';
void main() {
runApp(const ThemeApp());
}
class ThemeApp extends StatelessWidget {
const ThemeApp({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'ThemeData example',
theme: ThemeData(
useMaterial3: true,
colorScheme: ColorScheme.fromSeed(
seedColor: const Color(0xFF6750A4),
),
textTheme: const TextTheme(
headlineSmall: TextStyle(fontWeight: FontWeight.bold),
),
),
home: const HomePage(),
);
}
}
class HomePage extends StatelessWidget {
const HomePage({super.key});
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: const Text('ThemeData')),
body: Center(
child: FilledButton(
onPressed: () {},
child: const Text('Continue'),
),
),
);
}
}The theme property receives a ThemeData object. ColorScheme.fromSeed creates a coordinated palette from one seed color. The textTheme value changes a typography role without requiring every text widget to define its own style.
How the theme reaches widgets
Flutter uses an inherited theme: a widget-tree mechanism that makes shared data available to descendant widgets. MaterialApp creates a Theme widget from its theme property. A descendant can read the nearest theme with Theme.of(context).
flowchart TD
A[MaterialApp] --> B[ThemeData]
B --> C[Theme widget]
C --> D[AppBar]
C --> E[FilledButton]
C --> F[Custom widget]
F --> G[Theme.of(context)]caption: ThemeData flows from MaterialApp to descendant widgets. alt: A flowchart shows MaterialApp creating ThemeData, which reaches Material widgets and custom widgets through the Theme widget.
Use Theme.of(context) inside build when a custom widget needs a theme value. Avoid storing a theme value permanently because the user might change themes while the app is running.
class StatusLabel extends StatelessWidget {
const StatusLabel({super.key});
@override
Widget build(BuildContext context) {
final theme = Theme.of(context);
return Text(
'Ready',
style: theme.textTheme.titleMedium?.copyWith(
color: theme.colorScheme.primary,
),
);
}
}The nullable operator ?. is useful because a typography role can be absent. It leaves the result nullable, which Text.style accepts.
> callout note > Prefer semantic theme roles such as primary, surface, and onSurface over literal colors. A role communicates how a color is used and can have a different value in another theme.
Common mistakes
A common mistake is defining colors directly in many widgets. This creates inconsistent screens and makes dark mode harder to support. Another mistake is placing a Theme.of(context) call above the MaterialApp that provides the desired theme. That lookup can only find themes in the widget's ancestor chain.
Use local Theme widgets only for a deliberate section-level override. Keep app-wide values in the ThemeData supplied by MaterialApp.
Exercise
Why should a custom widget read Theme.of(context) instead of using a hard-coded color?
Show answer
It uses the app's shared semantic values, so the widget stays consistent and can adapt when the active theme changes.