Sub topic 8
Typography with TextTheme
Every Text widget in a Material 3 app gets its size, weight and spacing from a small set of named styles. These styles live in TextTheme, and ThemeData holds the TextTheme. This chapter covers the 15 type roles, how to read them, how to override them without breaking dark mode, and how text scaling affects your layouts.
This chapter assumes you can already build a MaterialApp with theme and darkTheme, and that you know Theme.of(context) and ColorScheme from the earlier chapters.
The Material 3 type scale
Material 3 defines five groups of text styles, each in large, medium and small sizes. That gives 15 roles. Each role is a field on TextTheme, such as headlineMedium or bodySmall.
| Group | Roles | Default sizes (logical pixels) | Typical use |
|---|---|---|---|
| Display | displayLarge, displayMedium, displaySmall | 57, 45, 36 | Hero text, large numbers |
| Headline | headlineLarge, headlineMedium, headlineSmall | 32, 28, 24 | Screen and section headings |
| Title | titleLarge, titleMedium, titleSmall | 22, 16, 14 | App bar titles, list item titles, cards |
| Body | bodyLarge, bodyMedium, bodySmall | 16, 14, 12 | Paragraphs and most plain text |
| Label | labelLarge, labelMedium, labelSmall | 14, 12, 11 | Buttons, chips, captions |
Pick a role by its meaning, not by how big it looks. A card heading is a titleMedium even if you later decide to make titles larger. When you change the role's style in one place, every use follows.
Reading text styles in widgets
Read the text theme through Theme.of(context). Each field is nullable (TextStyle?), so use ?.copyWith when you adjust a style.
import 'package:flutter/material.dart';
class SectionHeader extends StatelessWidget {
const SectionHeader({super.key, required this.text});
final String text;
@override
Widget build(BuildContext context) {
final theme = Theme.of(context);
return Padding(
padding: const EdgeInsets.symmetric(vertical: 8),
child: Text(
text,
style: theme.textTheme.titleMedium?.copyWith(
color: theme.colorScheme.primary,
),
),
);
}
}The color comes from colorScheme, not from a literal like Colors.indigo. That choice keeps the text readable when the dark scheme is active.
A plain Text('hello') with no style normally inherits bodyMedium, because Scaffold and other Material widgets set that as the default text style. Text outside a Material ancestor falls back to a different default, which is why unstyled text in a bare Center can look wrong.
Overriding roles in ThemeData
Pass a TextTheme to ThemeData(textTheme: ...) and set only the roles you want to change. Flutter merges your styles into the default Material 3 typography. Roles you leave out keep their default size, weight and spacing.
import 'package:flutter/material.dart';
const _seed = Color(0xFF6750A4);
ThemeData buildTheme(Brightness brightness) {
return ThemeData(
colorScheme: ColorScheme.fromSeed(
seedColor: _seed,
brightness: brightness,
),
textTheme: const TextTheme(
headlineMedium: TextStyle(fontWeight: FontWeight.w700),
titleLarge: TextStyle(fontWeight: FontWeight.w600),
bodyMedium: TextStyle(height: 1.5),
),
);
}Using one builder function for both modes guarantees that the light and dark themes have the same text structure:
MaterialApp(
theme: buildTheme(Brightness.light),
darkTheme: buildTheme(Brightness.dark),
themeMode: ThemeMode.system,
home: const Placeholder(),
)Do not hard-code text colors in the theme
The default text theme picks a color that matches the brightness, dark text on a light surface and light text on a dark surface. If you write TextStyle(color: Colors.black87) into your textTheme, that color replaces the default in both themes. Your dark theme then shows near-black text on a dark background.
Changing the font family
The simplest way to change the font everywhere is the fontFamily argument on ThemeData. First, declare the font files in pubspec.yaml:
flutter:
fonts:
- family: Inter
fonts:
- asset: assets/fonts/Inter-Regular.ttf
- asset: assets/fonts/Inter-Medium.ttf
weight: 500
- asset: assets/fonts/Inter-Bold.ttf
weight: 700Then pass the family name:
ThemeData(
colorScheme: ColorScheme.fromSeed(seedColor: const Color(0xFF6750A4)),
fontFamily: 'Inter',
)If your weights are missing from the declaration, Flutter picks the nearest declared weight, which can make w600 and w700 look identical. Declare each weight you actually use.
To change the family on an existing text theme, use apply. It also lets you scale every size or recolor the groups at once.
final base = ThemeData(
colorScheme: ColorScheme.fromSeed(seedColor: const Color(0xFF6750A4)),
);
final scaled = base.copyWith(
textTheme: base.textTheme.apply(fontFamily: 'Inter', fontSizeFactor: 1.1),
);The google_fonts package offers a similar helper, for example GoogleFonts.interTextTheme(base.textTheme). It downloads fonts at runtime by default, so bundle the files for apps that must work offline.
Previewing the whole scale
A type scale page is a quick way to check how your overrides look in both brightness modes. This is a complete app you can run.
import 'package:flutter/material.dart';
void main() => runApp(const TypeDemoApp());
class TypeDemoApp extends StatelessWidget {
const TypeDemoApp({super.key});
static ThemeData _build(Brightness brightness) {
return ThemeData(
colorScheme: ColorScheme.fromSeed(
seedColor: const Color(0xFF6750A4),
brightness: brightness,
),
textTheme: const TextTheme(
headlineMedium: TextStyle(fontWeight: FontWeight.w700),
bodyMedium: TextStyle(height: 1.5),
),
);
}
@override
Widget build(BuildContext context) {
return MaterialApp(
theme: _build(Brightness.light),
darkTheme: _build(Brightness.dark),
themeMode: ThemeMode.system,
home: const TypeScalePage(),
);
}
}
class TypeScalePage extends StatelessWidget {
const TypeScalePage({super.key});
@override
Widget build(BuildContext context) {
final t = Theme.of(context).textTheme;
final styles = <String, TextStyle?>{
'displayLarge': t.displayLarge,
'displayMedium': t.displayMedium,
'displaySmall': t.displaySmall,
'headlineLarge': t.headlineLarge,
'headlineMedium': t.headlineMedium,
'headlineSmall': t.headlineSmall,
'titleLarge': t.titleLarge,
'titleMedium': t.titleMedium,
'titleSmall': t.titleSmall,
'bodyLarge': t.bodyLarge,
'bodyMedium': t.bodyMedium,
'bodySmall': t.bodySmall,
'labelLarge': t.labelLarge,
'labelMedium': t.labelMedium,
'labelSmall': t.labelSmall,
};
return Scaffold(
appBar: AppBar(title: const Text('Type scale')),
body: ListView(
padding: const EdgeInsets.all(16),
children: [
for (final entry in styles.entries)
Padding(
padding: const EdgeInsets.only(bottom: 12),
child: Text(entry.key, style: entry.value),
),
],
),
);
}
}Switch your device between light and dark to confirm that the text color flips on its own. That is the sign you have not hard-coded colors.
Text scaling and accessibility
Users can raise the system font size. Flutter applies that setting through a TextScaler in MediaQuery, and every Text widget scales automatically. Your layouts need to survive this.
- Do not give a
Texta fixed-heightContainer. At a larger scale the text overflows or clips. Let the content decide the height, or useminHeightconstraints. - Allow text to wrap. Use
maxLineswithoverflow: TextOverflow.ellipsisonly where losing the end of the text is acceptable. - Test at the largest scale. In a widget test or a preview, wrap the app in
MediaQuerywithtextScaler: const TextScaler.linear(2.0).
You can limit scaling for a single region, such as a dense toolbar, with MediaQuery.withClampedTextScaling:
MediaQuery.withClampedTextScaling(
minScaleFactor: 1.0,
maxScaleFactor: 1.5,
child: const Text('Compact label'),
)Use clamping sparingly. It stops people who need large text from getting it, so apply it only where a layout truly cannot grow, and keep the cap generous.
Common errors
- Text is invisible in dark mode. A hard-coded
colorintextThemeor in a widget. UsecolorSchemeroles instead. - A style change has no effect. You edited a role the widget does not use.
ListTiletitles usebodyLarge,AppBartitles usetitleLarge, and buttons uselabelLarge. Check which role the component reads, or change it with a component theme. - A null error on a text style. Roles are
TextStyle?. Use?.copyWith, or give a fallback with?? const TextStyle(). - An assertion about interpolating TextStyles when the theme changes. Flutter animates between the light and dark themes, and it cannot interpolate styles whose
inheritflags differ. Build both themes with the same function so the styles have the same shape. - Sizes look different on each platform. Material 3 typography selects font families by platform. Set
fontFamilyexplicitly when you need identical text everywhere.
Exercise
Your app's dark theme shows dark gray body text on a dark background, but the light theme looks fine. Your ThemeData contains textTheme: TextTheme(bodyMedium: TextStyle(color: Colors.black87, height: 1.5)), and the same ThemeData is used for both modes except for the ColorScheme brightness. What is the cause, and how do you fix it?
Show answer
The explicit color: Colors.black87 overrides the default text color that Flutter would pick for the brightness, so it applies in dark mode too. Remove color from the shared textTheme and keep only height: 1.5. If you need a specific color, read it from Theme.of(context).colorScheme (for example onSurface) where the text is built, or define a separate TextTheme for each brightness.
Exercise
A card has SizedBox(height: 48, child: Text('Order summary', style: ...titleMedium)). At the largest system font size, the text is clipped. Name two changes that fix it without ignoring the user's font setting.
Show answer
Remove the fixed height, or turn it into a minimum with ConstrainedBox(constraints: BoxConstraints(minHeight: 48)), so the card grows with the text. Also allow the text to wrap, with no maxLines: 1, or set maxLines high enough and use overflow: TextOverflow.ellipsis only if cutting the end is acceptable.