Sub topic 7
Mapping Weights and Italic Styles to Font Files
This chapter assumes that you know basic Dart syntax, can build a simple Flutter widget tree, and can edit pubspec.yaml. You should also be comfortable with YAML indentation and have run flutter pub get before.
A font family is a group of related font files that share a design. For example, the Raleway family can contain regular, bold, and italic files. Flutter needs a mapping between each file and its weight or style. That mapping lets Flutter choose the correct file when your code requests a TextStyle.
Declaring font variants
Declare the family under the flutter section of pubspec.yaml. Each file needs an asset entry. Add weight: 700 for bold and style: italic for italic files.
flutter:
fonts:
- family: Raleway
fonts:
- asset: assets/fonts/Raleway-Regular.ttf
- asset: assets/fonts/Raleway-Bold.ttf
weight: 700
- asset: assets/fonts/Raleway-Italic.ttf
style: italicThe family name is Raleway. It is the name that you use later in Dart code. It does not need to match the file name, but using the same name usually makes the configuration easier to understand.
A font entry without a weight or style is the normal, upright version. Flutter treats it as weight 400, which is the conventional value for regular text.
The weight property uses values from 100 through 900. These values describe the thickness of the glyphs, or the visible shapes used to draw characters.
| Font weight | Common name | Typical use |
|---|---|---|
| 400 | Regular | Body text |
| 500 | Medium | Emphasis or labels |
| 600 | Semibold | Subheadings |
| 700 | Bold | Strong emphasis |
| 900 | Black | Display headings |
The font file must actually contain the weight you declare. Setting weight: 700 does not make a regular font file bold. It only tells Flutter that the file is the bold member of the family.
Requesting a variant in Dart
After declaring the family, select it with fontFamily. You can apply a font to one widget with TextStyle.
import 'package:flutter/material.dart';
class FontExamples extends StatelessWidget {
const FontExamples({super.key});
@override
Widget build(BuildContext context) {
return const Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
'Regular text',
style: TextStyle(
fontFamily: 'Raleway',
fontWeight: FontWeight.w400,
),
),
Text(
'Bold text',
style: TextStyle(
fontFamily: 'Raleway',
fontWeight: FontWeight.w700,
),
),
Text(
'Italic text',
style: TextStyle(
fontFamily: 'Raleway',
fontStyle: FontStyle.italic,
),
),
],
);
}
}Flutter combines the fontFamily, fontWeight, and fontStyle values to find the best matching file. The first widget requests Raleway at weight 400. The second requests Raleway at weight 700. The third requests the italic Raleway file.
If a requested variant is missing, Flutter may use another registered variant or synthesize a visual effect. For example, it might slant an upright font to imitate italics or draw a regular font more heavily to imitate bold. A real font variant usually looks better because its letter shapes were designed for that style.
Combining weight and italic style
Weight and style are independent properties. A font family can contain a bold italic file, which should be declared with both properties.
flutter:
fonts:
- family: Raleway
fonts:
- asset: assets/fonts/Raleway-Regular.ttf
- 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: italicThis declaration allows Flutter to choose Raleway-BoldItalic.ttf for the following style:
const Text(
'Important note',
style: TextStyle(
fontFamily: 'Raleway',
fontWeight: FontWeight.w700,
fontStyle: FontStyle.italic,
),
)Flutter first looks for a matching family. It then considers the requested weight and italic state. If an exact combination does not exist, it chooses the closest available option. This is why declaring every variant that your design uses is safer than relying on fallback behavior.
Applying variants through a theme
If most of an app uses the same family, set it in ThemeData. Individual widgets can still override the weight or style.
import 'package:flutter/material.dart';
class FontThemeApp extends StatelessWidget {
const FontThemeApp({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(
theme: ThemeData(
fontFamily: 'Raleway',
textTheme: const TextTheme(
bodyMedium: TextStyle(fontWeight: FontWeight.w400),
titleLarge: TextStyle(fontWeight: FontWeight.w700),
),
),
home: const Scaffold(
body: Center(
child: Text(
'A themed heading',
style: TextStyle(fontStyle: FontStyle.italic),
),
),
),
);
}
}The theme supplies Raleway to the text. The heading requests weight 700 through titleLarge, while the widget-level style adds italics. If the bold italic file is declared, Flutter can use it.
Common mapping errors
A font that appears to be ignored usually has a configuration problem rather than a rendering problem.
- The family name in
TextStyledoes not exactly match thefamilyvalue inpubspec.yaml. Names are case-sensitive. - The
weightproperty is attached to the wrong file. - The italic file is missing
style: italic. - A bold italic file is declared only as
weight: 700, so Flutter treats it as upright. - The YAML indentation is invalid.
- The app was not restarted after changing font declarations. Run
flutter pub get, then perform a full restart if the new file is not recognized. - The file path or filename differs from the path on disk.
- The selected font file does not contain the expected glyphs, causing some characters to use a fallback font.
When debugging, test one known family and one known weight first. Then add italic and additional weights one at a time. Inspect the rendered text at a large size, where differences between real and synthesized variants are easier to see.
Exercise
You have Raleway-BoldItalic.ttf. What should its YAML entry contain, and which TextStyle requests it?
Show answer
The entry should contain weight: 700 and style: italic. The matching style uses fontFamily: 'Raleway', fontWeight: FontWeight.w700, and fontStyle: FontStyle.italic.