Sub topic 6
Using Fonts in a Widget and in the App Theme
This chapter assumes that you know basic Dart and Flutter widget trees. It also assumes that you can edit pubspec.yaml, run flutter pub get, and register a custom font family. The examples use a family named Raleway.
Registering a font makes the font available to Flutter. You still need to choose where to use it. You can apply a font to one widget, to a group of widgets, or to the whole application.
Applying a font to one widget
Use the fontFamily property of TextStyle when only one widget needs the custom font. This keeps the rest of the application unchanged.
import 'package:flutter/material.dart';
class FontExample extends StatelessWidget {
const FontExample({super.key});
@override
Widget build(BuildContext context) {
return const Scaffold(
body: Center(
child: Text(
'A custom font on one widget',
style: TextStyle(
fontFamily: 'Raleway',
fontSize: 24,
color: Colors.indigo,
),
),
),
);
}
}The value Raleway must match the family name in pubspec.yaml. It does not have to match the file name. For example, a file named Raleway-Regular.ttf can belong to the family named Raleway.
A local TextStyle can also change a standard text theme style. The copyWith method keeps the properties that you do not change.
Text(
'A heading',
style: Theme.of(context).textTheme.headlineSmall?.copyWith(
fontFamily: 'Raleway',
color: Colors.indigo,
),
)This approach is useful for a logo, a special heading, or a branded label. It can become repetitive if many widgets need the same font.
Applying a font through the app theme
Use ThemeData when most or all text in the application should use the custom family. The theme provides default values that widgets inherit.
import 'package:flutter/material.dart';
void main() {
runApp(const FontApp());
}
class FontApp extends StatelessWidget {
const FontApp({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Font theme example',
theme: ThemeData(
colorScheme: ColorScheme.fromSeed(seedColor: Colors.indigo),
fontFamily: 'Raleway',
),
home: const FontHomePage(),
);
}
}
class FontHomePage extends StatelessWidget {
const FontHomePage({super.key});
@override
Widget build(BuildContext context) {
final textTheme = Theme.of(context).textTheme;
return Scaffold(
appBar: AppBar(
title: const Text('Custom fonts'),
),
body: Padding(
padding: const EdgeInsets.all(24),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
'Welcome',
style: textTheme.headlineMedium,
),
const SizedBox(height: 12),
Text(
'This text receives Raleway from the application theme.',
style: textTheme.bodyLarge,
),
],
),
),
);
}
}The fontFamily value in ThemeData becomes the default family for text styles created by the Material theme. A widget can still override that value by providing its own TextStyle.
Theme inheritance means that a widget reads values from the nearest Theme above it in the widget tree. You can create a smaller theme for one part of the interface with Theme and ThemeData:
Theme(
data: Theme.of(context).copyWith(
textTheme: Theme.of(context).textTheme.apply(
fontFamily: 'Raleway',
),
),
child: const Text('Text inside a local theme'),
)In most applications, setting the family in the root MaterialApp theme is simpler. Use a local theme when a section has a distinct visual identity.
Choosing weights and styles
A font family can contain several files. Flutter chooses a file based on the requested fontWeight and fontStyle. The weight and style declarations must match the files registered in pubspec.yaml.
Text(
'Bold and italic text',
style: TextStyle(
fontFamily: 'Raleway',
fontWeight: FontWeight.w700,
fontStyle: FontStyle.italic,
),
)If the family has a regular file and a bold file registered as weight 700, FontWeight.w700 selects the bold file. If an italic file is registered with style: italic, FontStyle.italic selects it.
Flutter may synthesize a bold or italic appearance when the requested variant is missing. Synthesized text can look different from a real font variant. Register the variants that your design needs instead of relying on synthesis.
| Widget style | Typical purpose | Example |
|---|---|---|
| `fontWeight: FontWeight.w400` | Regular text | Body paragraphs |
| `fontWeight: FontWeight.w700` | Strong emphasis | Buttons and headings |
| `fontStyle: FontStyle.italic` | Secondary emphasis | Notes or quotations |
How theme and local styles combine
Text styles have several independent properties. A local style can change one property while retaining others from a text theme style.
Text(
'A themed heading with a local color',
style: Theme.of(context).textTheme.headlineSmall?.copyWith(
color: Colors.deepOrange,
),
)The heading keeps its theme-provided size and weight. The local style changes only the color. This is safer than rebuilding every property manually because it preserves the design system's typography.
Common errors
- The family name is misspelled. Compare the
fontFamilyvalue with thefamilyvalue inpubspec.yaml. - The application was not restarted after changing font declarations. Run
flutter pub get, then perform a full restart. - A weight is requested but not registered. Check that the requested weight matches a declared font file.
- A local style replaces more properties than intended. Prefer
copyWithwhen modifying an existing text theme style. - A widget is outside the expected
Themesubtree. Verify that it is belowMaterialAppor anotherThemewidget.
Exercise
How would you make Raleway the default font for the whole application while making one warning label red?
Show answer
Set fontFamily to 'Raleway' in MaterialApp's ThemeData, then use the warning label's text theme style with copyWith(color: Colors.red).