Sub topic 7
Mapping weights and italic styles to font files
This chapter covers how Flutter picks a font file for a piece of text. It assumes you can already declare a font family in pubspec.yaml and apply it with fontFamily. It also assumes you are comfortable with YAML indentation and with running flutter pub get. If any of that is unfamiliar, review the chapters on registering a font family and using fonts in a widget first.
By the end you will be able to:
- Declare several files under one family and tell Flutter which weight and style each file holds.
- Predict which file a
TextStyleselects. - Recognize the failure modes: missing weights, mismatched families, and stale builds.
One family, many files
A font family is usually shipped as several files. Each file holds one design: regular, bold, italic, and so on. Flutter does not guess what a file contains from its name, so Raleway-Bold.ttf is not treated as bold just because of the filename. You say so in pubspec.yaml, using two optional properties on each asset entry:
weightis an integer from 100 to 900 in steps of 100.styleis eithernormaloritalic.
The family name, the weight values, and the style values together form a lookup table. At runtime, a TextStyle with fontFamily, fontWeight, and fontStyle is matched against that table.
flutter:
fonts:
- family: Raleway
fonts:
- asset: assets/fonts/Raleway-Regular.ttf
- asset: assets/fonts/Raleway-Medium.ttf
weight: 500
- asset: assets/fonts/Raleway-Bold.ttf
weight: 700
- asset: assets/fonts/Raleway-Italic.ttf
style: italic
- asset: assets/fonts/Raleway-BoldItalic.ttf
weight: 700
style: italicEach entry resolves like this:
| File | Declared weight | Declared style |
|---|---|---|
| `Raleway-Regular.ttf` | 400 (default) | normal (default) |
| `Raleway-Medium.ttf` | 500 | normal (default) |
| `Raleway-Bold.ttf` | 700 | normal (default) |
| `Raleway-Italic.ttf` | 400 (default) | italic |
| `Raleway-BoldItalic.ttf` | 700 | italic |
An entry with no weight is treated as regular, which is 400. An entry with no style is treated as normal. So the first file in the example needs no extra properties.
The weight scale
FontWeight in Dart has nine constants. They map directly to the numbers you write in pubspec.yaml.
| Dart constant | Number in `pubspec.yaml` | Common name |
|---|---|---|
| `FontWeight.w100` | 100 | Thin |
| `FontWeight.w200` | 200 | Extra Light |
| `FontWeight.w300` | 300 | Light |
| `FontWeight.w400` (also `FontWeight.normal`) | 400 | Regular |
| `FontWeight.w500` | 500 | Medium |
| `FontWeight.w600` | 600 | Semi Bold |
| `FontWeight.w700` (also `FontWeight.bold`) | 700 | Bold |
| `FontWeight.w800` | 800 | Extra Bold |
| `FontWeight.w900` | 900 | Black |
The number is the only thing that links the two sides. The weight: 700 in YAML and FontWeight.w700 in Dart are the same slot. The words "Bold" or "SemiBold" in a filename have no effect. If your font distributor names a file Raleway-SemiBold.ttf, you must write weight: 600 for it yourself.
Values such as weight: 650 or weight: bold are not valid. The tool that processes pubspec.yaml rejects weights outside the 100 to 900 multiples of 100, so the build reports an error instead of silently ignoring them.
Using the declared variants in code
Once the table is declared, you select a row from Dart. You never refer to a file path in widget code. You name the family, and Flutter picks the file.
This program shows each declared combination side by side. It needs the YAML above and the five font files in assets/fonts/.
import 'package:flutter/material.dart';
void main() => runApp(const WeightDemo());
class WeightDemo extends StatelessWidget {
const WeightDemo({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(
theme: ThemeData(fontFamily: 'Raleway'),
home: Scaffold(
appBar: AppBar(title: const Text('Raleway variants')),
body: ListView(
padding: const EdgeInsets.all(16),
children: const [
Text('Regular 400', style: TextStyle(fontSize: 24)),
Text(
'Medium 500',
style: TextStyle(fontSize: 24, fontWeight: FontWeight.w500),
),
Text(
'Bold 700',
style: TextStyle(fontSize: 24, fontWeight: FontWeight.w700),
),
Text(
'Italic 400',
style: TextStyle(fontSize: 24, fontStyle: FontStyle.italic),
),
Text(
'Bold italic 700',
style: TextStyle(
fontSize: 24,
fontWeight: FontWeight.w700,
fontStyle: FontStyle.italic,
),
),
],
),
),
);
}
}Setting fontFamily in ThemeData makes Raleway the default for the whole app. The individual TextStyle objects only change weight and style. Because the family comes from the theme, these Text widgets pick up the family through the default text style. The Text widget merges its own style with the inherited one.
Flutter also exposes Text('...').style through shortcuts such as Theme.of(context).textTheme.titleLarge. Many Material text styles set their own weights. For example, some label and title styles use w500. If your family has no 500 file, those widgets will render with whichever file Flutter judges closest. Check any weights your theme's text styles use before you decide which files to ship.
How Flutter chooses a file
The engine has to resolve three inputs (family, weight, style) to one file. The process looks roughly like this.
Exact matches are predictable. Fallbacks are not something you should rely on. When there is no exact weight, the engine picks the nearest declared weight using rules modeled on CSS font matching. One practical consequence is that if you declare only 400 and 700 and ask for w600, you get one of those two files, and it will not be a true semi-bold. The same applies to w300 or w800. The text still renders, so you may not notice the mismatch until a designer does.
The practical rule is to declare a file for every weight you actually use in code. Search your project for FontWeight.w and for FontWeight.bold, and check the theme's text styles as well.
Italics
Italic matching works the same way as weight matching, with one extra concern: italic and weight combine. A font family that has Raleway-Bold.ttf and Raleway-Italic.ttf does not automatically have a bold italic. If you request FontWeight.w700 with FontStyle.italic and there is no file declared with both weight: 700 and style: italic, Flutter must choose something else. Depending on the platform and engine, you may see bold upright text, italic regular text, or an algorithmically slanted version. None of these is the designer's intended glyph shapes.
If you want true bold italics, ship the file and declare both properties on that one entry, as in the Raleway-BoldItalic.ttf entry above.
Some fonts are shipped as a pair of files, one upright and one italic, each covering several weights. That is a different packaging style. Flutter's pubspec.yaml model expects one weight per file entry, so a font file that contains many weights needs the variable-font approach described below.
Declaring one file for two roles
A single file may be listed more than once under different entries, but this is rarely useful. A more common situation is the reverse: you only own a regular and a bold file, but a design asks for medium text. You have three honest options.
- Buy or download the medium file and declare it with
weight: 500. - Change the design to use the weights you have.
- Accept that
w500will resolve to the closest declared weight, and check that the result looks acceptable.
Do not "fix" the gap by declaring Raleway-Bold.ttf a second time with weight: 500. The text will render in bold at medium positions, which makes your type scale harder to reason about. The design intent is gone and the file is bundled twice in your declarations.
Multiple families in one app
Each family entry is independent. Different weights in different families are different tables. You can declare a body family and a display family together.
flutter:
fonts:
- family: Raleway
fonts:
- asset: assets/fonts/Raleway-Regular.ttf
- asset: assets/fonts/Raleway-Bold.ttf
weight: 700
- family: Merriweather
fonts:
- asset: assets/fonts/Merriweather-Regular.ttf
- asset: assets/fonts/Merriweather-Italic.ttf
style: italicSetting fontFamily: 'Merriweather' on a heading then uses only the Merriweather table. If you ask for FontWeight.w700 there, Merriweather has no 700 file, so Flutter falls back within that family. It does not borrow Raleway's bold file.
The family string is a label you choose. It does not need to match the name stored inside the font file. It must match exactly, including capitalization, between pubspec.yaml and every fontFamily argument in Dart.
Variable fonts
A variable font stores a range of weights (and sometimes widths or slants) in a single file. Flutter's TextStyle has a fontVariations parameter that sets values on those axes.
import 'package:flutter/material.dart';
class VariableWeightText extends StatelessWidget {
const VariableWeightText({super.key});
@override
Widget build(BuildContext context) {
return const Text(
'Variable weight',
style: TextStyle(
fontFamily: 'RalewayVariable',
fontSize: 28,
fontVariations: [FontVariation('wght', 650)],
),
);
}
}The family is declared with a single file and no weight property:
flutter:
fonts:
- family: RalewayVariable
fonts:
- asset: assets/fonts/Raleway-Variable.ttfHere 650 is allowed because it is an axis value inside the font, and it is not a pubspec.yaml weight. Variable font support depends on the Flutter version and the rendering backend. Test on every platform you ship to. Italic is often a separate variable file, so you may still need a second declaration with style: italic.
Troubleshooting weights and styles
These are the failures you are most likely to hit. Each one has a quick check.
| Symptom | Likely cause | Check |
|---|---|---|
| Bold text looks the same as regular | No file declared with `weight: 700`, or the declared bold file is wrong | Open `pubspec.yaml` and confirm the entry has `weight: 700` and the right filename |
| Text falls back to the default system font | `fontFamily` does not match `family` exactly | Compare the two strings character by character |
| Build error about the font entry | `weight` is not a multiple of 100 between 100 and 900, or indentation is wrong | Fix the YAML and rerun `flutter pub get` |
| Italic text looks only slanted, not designed | No file declared with `style: italic` | Add the italic file and declare it |
| Changes to fonts do not appear | The app was only hot reloaded | Stop the app and run it again so the bundle is rebuilt |
| Semi-bold looks like regular or bold | No 600 file, so the nearest weight was used | Declare a real 600 file, or avoid `w600` |
Hot reload does not reload font and asset declarations reliably. After you change the fonts section, stop the app and start it again.
Choosing which files to ship
Every font file adds to the size of your app. A family with nine weights and nine italics is eighteen files. Most apps use two to four of them. A sensible process is:
- List the text styles your design uses, each with a weight and a style.
- Collapse near-duplicates, for example by using only 400 and 700 for body text.
- Declare exactly those files.
- Check the theme's built-in text styles and either override them or add the weights they request.
This keeps the bundle small and keeps the resolution table predictable.
Exercise
Your pubspec.yaml declares the Raleway family with Raleway-Regular.ttf (no extra properties) and Raleway-Bold.ttf (weight: 700). A designer asks for italic bold headings, and you write Text('Title', style: TextStyle(fontFamily: 'Raleway', fontWeight: FontWeight.w700, fontStyle: FontStyle.italic)). What do you need to add to get a true bold italic, and what exact entry would you write?
Show answer
You need a bold italic font file, for example Raleway-BoldItalic.ttf, placed in assets/fonts/. Add an entry under the Raleway family that sets both properties: "- asset: assets/fonts/Raleway-BoldItalic.ttf", then "weight: 700" and "style: italic" on the following lines at the same indentation as "asset". Then stop and rerun the app. Without this file, Flutter has no bold italic design and will use another file or a synthesized slant, depending on the platform.
Exercise
A teammate writes "weight: 600" for Raleway-SemiBold.ttf, but in Dart the app only ever uses FontWeight.w700 for emphasis. Text never shows the semi-bold file. Why, and what are two ways to fix it?
Show answer
The weight number is the only link between the two sides, and the Dart code requests 700 while the file is declared as 600. With no 700 file declared, the engine picks the closest declared weight, so the 600 file may be used, or the regular file, depending on what else is declared. The result is not a true bold. Fix 1: change the Dart code to FontWeight.w600 wherever the semi-bold look is wanted. Fix 2: declare a real bold file with weight: 700 and keep using FontWeight.w700 for emphasis.
Exercise
You change the weight of a font entry in pubspec.yaml from 700 to 600 and hot reload. The text does not change. What should you do?
Show answer
Stop the running app and start it again, so that the font declarations are rebuilt into the app bundle. Hot reload does not reliably pick up changes to the fonts section of pubspec.yaml. Run flutter pub get first if the tooling has not already done so.