Sub topic 6
Adding custom fonts
Flutter's default text uses Roboto on Android and the system font on other platforms. A custom font lets you match a brand or a design file. This chapter covers how to register a font in pubspec.yaml, how Flutter picks the right file for a style, how to apply the font to one widget or the whole app, and what usually goes wrong.
It assumes you can edit pubspec.yaml, you know TextStyle and ThemeData, and you have a project that runs on a device, emulator, or the web.
How custom fonts work
A font family is a named group of font files. The family name is a label that you choose. It does not have to match the file name. Each file in the family covers one combination of weight and style, such as regular, bold, or italic. You declare the files once in pubspec.yaml. After that you refer to the family by name in Dart.
Flutter bundles the font files with your app at build time. Nothing is downloaded at run time unless you use a package such as google_fonts, which is covered at the end of this chapter.
Step 1: Add the font files
Put the files in your project. A common layout is:
assets/fonts/Lora-Regular.ttfassets/fonts/Lora-Bold.ttfassets/fonts/Lora-Italic.ttf
Flutter accepts TrueType (.ttf) and OpenType (.otf) files. Check the font's license before you ship it. Many fonts allow app embedding, but some do not.
Step 2: Declare the family in pubspec.yaml
Add a fonts list under the flutter key. Each entry names a family and lists its files.
flutter:
uses-material-design: true
assets:
- assets/images/
fonts:
- family: Lora
fonts:
- asset: assets/fonts/Lora-Regular.ttf
- asset: assets/fonts/Lora-Bold.ttf
weight: 700
- asset: assets/fonts/Lora-Italic.ttf
style: italicEach file entry can have two optional properties:
weightis an integer from 100 to 900 in steps of 100. It tells Flutter whichFontWeightthis file represents.400is normal and700is bold. If you omit it, the file is treated as normal weight.styleisitalicornormal. If you omit it, the file is treated as normal.
The weight and style values are what you tell Flutter about each file. Flutter does not read them from the font itself. If you label a bold file as weight: 400, Flutter will use it for regular text.
YAML is sensitive to indentation. Use spaces, never tabs. Every family entry sits under fonts:, and each asset line sits under that family's own fonts: list.
After you edit pubspec.yaml, run flutter pub get, or save the file if your IDE runs it for you. Then stop the app and start it again. Hot reload does not pick up new fonts or assets, so a font that "does nothing" is often just an app that was never restarted.
Step 3: Use the family in Dart
Apply the family to one widget with fontFamily:
import 'package:flutter/material.dart';
class LoraHeading extends StatelessWidget {
const LoraHeading({super.key});
@override
Widget build(BuildContext context) {
return const Text(
'Chapter One',
style: TextStyle(
fontFamily: 'Lora',
fontSize: 28,
fontWeight: FontWeight.w700,
),
);
}
}The string 'Lora' must match the family value in pubspec.yaml exactly, including case. Because the style asks for weight 700, Flutter uses Lora-Bold.ttf.
To make the font the default for the entire app, set it on the theme:
import 'package:flutter/material.dart';
void main() => runApp(const FontDemo());
class FontDemo extends StatelessWidget {
const FontDemo({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(
theme: ThemeData(fontFamily: 'Lora'),
home: const Scaffold(
body: Center(
child: Column(
mainAxisSize: MainAxisSize.min,
children: [
Text('Regular text'),
Text(
'Bold text',
style: TextStyle(fontWeight: FontWeight.w700),
),
Text(
'Italic text',
style: TextStyle(fontStyle: FontStyle.italic),
),
],
),
),
),
);
}
}ThemeData.fontFamily applies the family to every text style in the theme. Individual widgets can still override it. The three Text widgets here use the regular, bold, and italic files. None of them names the family, because they inherit it from the theme.
How Flutter chooses a file
When a TextStyle has a family, a weight, and a style, Flutter looks for the best match among the files you declared for that family.
The last step is the one that surprises people. If you ask for bold but declared only a regular file, the text engine may draw a fake bold by thickening the glyphs. The result usually looks worse than a real bold file. The same applies to italics. Declare a real file for every weight and style your design uses.
Common errors
| Symptom | Likely cause | Fix |
|---|---|---|
| Text still uses the default font | The family name in Dart differs from `family` in YAML, or the app was only hot reloaded | Match the name exactly, then restart the app |
| Build error about a missing asset | The `asset:` path is wrong, or the file is not in the project | Check the path relative to `pubspec.yaml` |
| YAML parse error | Tabs or wrong indentation | Use spaces and align the lists |
| Bold text looks smeared | No bold file was declared, so the engine faked it | Add a bold file with `weight: 700` |
| Italic text looks merely slanted | No italic file was declared | Add a file with `style: italic` |
| Some characters show as empty boxes | The font lacks those glyphs | Add a fallback family |
Fallback fonts
A font file contains a limited set of characters. If your text includes a character the font does not have, Flutter looks in the fallback list. Set one with fontFamilyFallback:
const TextStyle(
fontFamily: 'Lora',
fontFamilyFallback: ['Roboto', 'sans-serif'],
)Flutter tries each family in order for any character the primary font cannot draw. Fallback names can refer to families you declared yourself or to fonts that already exist on the platform.
Fonts shipped in a package
If a package supplies the fonts, the family is declared in that package's pubspec.yaml, and the files live in the package. To use them, pass the package argument:
const TextStyle(
fontFamily: 'Lora',
package: 'my_fonts',
)Flutter turns this into the internal name packages/my_fonts/Lora. If you declare a font in your own app and want to use it from your own code, you do not need the package argument.
Alternatives and related topics
- Variable fonts. A single file can hold a range of weights. Flutter exposes the axes through
TextStyle.fontVariations, for exampleFontVariation('wght', 650). Declare the file once, and you can request intermediate weights. - The
google_fontspackage. It fetches fonts at run time and caches them. This saves setup time, but the first display can need network access. Bundling the files, as shown above, works offline and has no first-load delay. - Icon fonts. Custom icons use the same
fontssection ofpubspec.yaml. The Custom icons chapter shows how. - Text scaling. A custom font still respects the user's font size setting. Test your layouts with a larger scale, since some fonts are wider than the default.
Exercise
You declared the family Lora with only Lora-Regular.ttf. A widget uses TextStyle(fontFamily: 'Lora', fontWeight: FontWeight.w700). What do you see, and how do you fix it?
Show answer
Flutter finds no bold file, so it uses the regular file and the engine may synthesize a fake bold, which looks heavier but not like a real bold face. Add a bold file such as Lora-Bold.ttf to the family with weight: 700, run flutter pub get, and restart the app.
Exercise
You add a font family to pubspec.yaml, hot reload the app, and the text still uses the default font. Name two checks to make.
Show answer
First, restart the app fully, because hot reload does not load new fonts. Second, confirm that the fontFamily string in Dart matches the family value in pubspec.yaml exactly, including case, and that the asset paths and YAML indentation are correct.