Sub topic 8
Typography with TextTheme
This chapter assumes basic Dart syntax and null safety, Flutter widget trees, MaterialApp, Scaffold, and BuildContext. It focuses on typography, so the color scheme and theme mode are treated as already configured.
Why use TextTheme?
TextTheme is a group of named text styles for common interface roles. Instead of choosing a font size separately for every Text widget, you assign each piece of text a semantic role.
For example, a page title is usually a headlineSmall, while supporting content is usually bodyMedium. The role communicates the purpose of the text and keeps the app consistent.
| Text style | Typical use |
|---|---|
| `displayLarge` | Very large marketing or dashboard text |
| `headlineSmall` | Page or section heading |
| `titleLarge` | App bar or card title |
| `bodyLarge` | Important reading content |
| `bodyMedium` | Normal paragraphs |
| `labelLarge` | Button or control label |
Material 3 uses updated typography roles. Older examples may use names such as headline5 or bodyText2. Those names belong to older Flutter APIs. Prefer the current names such as headlineSmall and bodyMedium.
Reading styles from the active theme
A widget should read typography through Theme.of(context). This gives it the TextTheme belonging to the nearest active Theme.
sequenceDiagram
participant M as MaterialApp
participant T as ThemeData
participant W as Widget
M->>T: Provides active TextTheme
T-->>W: Theme.of(context).textTheme
W->>W: Selects headlineSmall or bodyMedium
W-->>M: Renders styled textcaption: How a widget obtains typography from the active theme alt: A sequence showing MaterialApp providing ThemeData, a widget reading the active TextTheme, and the widget rendering a selected text style
import 'package:flutter/material.dart';
void main() {
runApp(const TypographyApp());
}
class TypographyApp extends StatelessWidget {
const TypographyApp({super.key});
@override
Widget build(BuildContext context) {
const seed = Color(0xFF6750A4);
return MaterialApp(
title: 'Typography example',
theme: ThemeData(
useMaterial3: true,
colorScheme: ColorScheme.fromSeed(seedColor: seed),
textTheme: const TextTheme(
headlineSmall: TextStyle(
fontSize: 28,
fontWeight: FontWeight.bold,
),
bodyLarge: TextStyle(
fontSize: 18,
height: 1.5,
),
),
),
home: const TypographyPage(),
);
}
}
class TypographyPage extends StatelessWidget {
const TypographyPage({super.key});
@override
Widget build(BuildContext context) {
final textTheme = Theme.of(context).textTheme;
return Scaffold(
appBar: AppBar(title: const Text('Typography')),
body: Padding(
padding: const EdgeInsets.all(24),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text('A clear heading', style: textTheme.headlineSmall),
const SizedBox(height: 12),
Text(
'Body text uses a shared role instead of a one-off font size.',
style: textTheme.bodyLarge,
),
],
),
),
);
}
}The height property controls line height. A value of 1.5 means the line height is about one and a half times the font size. More line spacing can improve reading comfort in paragraphs.
Customize without losing the theme
A complete TextTheme is not always needed. You can start with the theme's existing style and change only one property:
final baseStyle = Theme.of(context).textTheme.bodyMedium;
final emphasizedStyle = baseStyle?.copyWith(
fontWeight: FontWeight.w600,
);The ?. matters because many TextTheme properties are nullable. It preserves the rest of the active style, including values supplied by the light or dark theme.
Use theme-level customization for rules shared across the application. Use copyWith for a local variation, such as emphasizing one phrase. Avoid hard-coding unrelated font sizes throughout the widget tree.
Exercise
Exercise
Which style would you choose for a normal paragraph, and how would you make one paragraph slightly bolder without replacing its whole style?
Show answer
Use bodyMedium or bodyLarge, depending on the paragraph's importance. Read the style from Theme.of(context).textTheme, then call copyWith(fontWeight: FontWeight.w600).