Sub topic 3
Reading Theme Values with `Theme.of(context)`
This chapter assumes basic Dart syntax, null safety, Flutter widget trees, MaterialApp, Scaffold, and BuildContext. You should also know that a widget's build method can run again when inherited values change.
Flutter stores app-wide visual settings in a ThemeData object. MaterialApp places that theme above its child widgets. A widget can read the nearest theme with Theme.of(context) instead of storing colors and text styles locally.
How `Theme.of(context)` works
Theme.of(context) looks up the widget tree from the current BuildContext. It finds the nearest Theme ancestor and returns its ThemeData.
The returned object includes values such as:
| Theme value | What it controls |
|---|---|
| `colorScheme` | Semantic colors such as primary and surface |
| `textTheme` | Named text styles such as titles and body text |
| `brightness` | Whether the theme is light or dark |
| Component themes | Defaults for buttons, app bars, inputs, and other widgets |
Material 3 uses semantic color roles. For example, primaryContainer is a container color, while onPrimaryContainer is intended for content placed on that container. Using matching roles helps maintain readable contrast when the theme changes.
import 'package:flutter/material.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
@override
Widget build(BuildContext context) {
const seed = Color(0xFF6750A4);
return MaterialApp(
theme: ThemeData(
colorScheme: ColorScheme.fromSeed(seedColor: seed),
),
home: const Scaffold(
body: Center(child: ThemedCard()),
),
);
}
}
class ThemedCard extends StatelessWidget {
const ThemedCard({super.key});
@override
Widget build(BuildContext context) {
final theme = Theme.of(context);
final colors = theme.colorScheme;
return Container(
padding: const EdgeInsets.all(16),
color: colors.primaryContainer,
child: Text(
'Themed content',
style: theme.textTheme.headlineSmall?.copyWith(
color: colors.onPrimaryContainer,
),
),
);
}
}The nullable operator (?.) is used because a text style lookup can return null. copyWith changes only the color while preserving the rest of the selected typography role.
Context and the nearest theme
A BuildContext identifies a widget's position in the tree. The position matters. A context can read only ancestors, not widgets that are built below it.
This works because ThemedCard is below MaterialApp's theme:
MaterialApp
Theme
Scaffold
ThemedCard
Theme.of(context)A common error is calling Theme.of(context) in the same build method that creates MaterialApp and expecting it to read that MaterialApp's theme. The context belongs above the newly created MaterialApp, so it cannot see the descendant theme. Move the themed widget below MaterialApp, or use a separate widget.
Local theme overrides
A Theme widget can override values for one part of the tree. Descendants then read that local theme because it is nearer than the app-wide theme.
Use local overrides for a contained design decision, not as a replacement for the app's main theme. Hard-coded colors inside reusable widgets make dark mode and later redesigns harder.
Exercise
Why can a widget below MaterialApp read its theme with Theme.of(context), but a context used to build MaterialApp cannot read that same theme?
Show answer
Theme.of(context) searches ancestors, and the MaterialApp theme is created below the context used to build MaterialApp.