Sub topic 5
Registering a Custom Font Family
This chapter assumes that you know basic Dart syntax, can build a simple Flutter widget tree, and can edit pubspec.yaml. You should also know that YAML uses indentation to show structure. You will register a font family, connect its files to that family, and prepare it for use in later widgets.
A custom font is a font file that your app bundles instead of relying only on fonts installed on the device. Flutter reads the font registration in pubspec.yaml and makes the family available to text widgets.
Prepare the font files
Obtain font files in a format supported by Flutter, such as TrueType (.ttf) or OpenType (.otf). Put the files in a predictable directory in your project:
font_demo/
assets/
fonts/
Raleway-Regular.ttf
Raleway-Bold.ttf
Raleway-Italic.ttf
lib/
main.dart
pubspec.yamlThe family name does not have to match every filename. In this example, the files belong to the Raleway family. The filename describes the file, while the family value is the name that Dart code will use later.
Flutter does not automatically discover every font file in your project. You must register each file under the flutter section of pubspec.yaml.
Add the family to `pubspec.yaml`
Add a fonts list below the flutter key. Each family entry has a family name and a nested fonts list. Each nested item identifies one font file.
name: font_demo
description: A Flutter app that bundles a custom font.
publish_to: 'none'
version: 1.0.0+1
environment:
sdk: '>=3.0.0 <4.0.0'
dependencies:
flutter:
sdk: flutter
dev_dependencies:
flutter_test:
sdk: flutter
flutter_lints: ^3.0.0
flutter:
uses-material-design: true
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 path in asset is relative to the project root, not relative to pubspec.yaml by a special rule. In a normal Flutter project, both files are at the project root, so assets/fonts/... is the correct path.
The first file is the default face for the family. The second file is registered as a bold face because it has weight 700. The third file is registered as an italic face because it has style: italic.
The fonts declaration is different from an ordinary asset declaration. You do not need to list these font files again under assets. Flutter packages registered font files as fonts when they appear under flutter.fonts.
Understand the registration model
A font family is a group of related faces. A face is one particular combination of weight and style, such as regular, bold, or italic. Flutter uses the requested TextStyle to select the closest registered face.
| Registration value | Meaning | Typical request |
|---|---|---|
| `family: Raleway` | The name of the font family | `fontFamily: 'Raleway'` |
| No weight or style | The regular face | `FontWeight.normal` |
| `weight: 700` | A bold face | `FontWeight.w700` |
| `style: italic` | An italic face | `FontStyle.italic` |
The numeric weight follows the CSS-style range from 100 to 900. Common values include 400 for normal text, 500 for medium text, 600 for semibold text, and 700 for bold text. Use the value that matches the actual font face. Do not label a regular file as weight 700 merely because its filename contains the word “Bold.”
The registration can be visualized as a lookup table:
If a requested face is not registered, Flutter may use another face from the family or synthesize a style. Synthetic styling can look different from a real font file, so register the faces that your design actually needs.
Validate the registration
After changing pubspec.yaml, run dependency resolution from the project directory:
flutter pub getThis command updates the project configuration and checks that the YAML is valid. It does not repair incorrect file paths. Confirm that every path points to a file with the exact spelling and capitalization used on disk.
Restart the application after adding a font. A hot reload may not reload font metadata reliably because the font is part of the application bundle. If a font still does not appear, stop the app and run it again.
A small test widget can help confirm that the family is available:
import 'package:flutter/material.dart';
class FontRegistrationTest extends StatelessWidget {
const FontRegistrationTest({super.key});
@override
Widget build(BuildContext context) {
return const Scaffold(
body: Center(
child: Text(
'Raleway registration test',
style: TextStyle(
fontFamily: 'Raleway',
fontSize: 24,
fontWeight: FontWeight.w700,
),
),
),
);
}
}This widget asks Flutter for the family named Raleway and the face with weight 700. Applying a font to one widget is useful for testing. You can apply the family to the whole application theme later.
Avoid common registration errors
YAML indentation is significant. The fonts key must be nested under flutter, and each asset must be nested under a family’s fonts list.
These two declarations are also easy to confuse:
familyis the name used byTextStyle.assetis the path to a font file.
For example, if the declaration says family: Raleway, code must use fontFamily: 'Raleway', even if the file is named BrandSans-Regular.ttf.
When the font does not render, check the YAML nesting, file path, file format, family spelling, and whether you fully restarted the app. Also verify that the font license allows redistribution with your application.
Exercise
A project contains assets/fonts/BrandSans-Bold.ttf, but the app declares family: BrandFont and uses fontFamily: 'BrandSans'. What should change?
Show answer
Change the Dart usage to fontFamily: 'BrandFont', or rename the declared family to BrandSans. The value used in TextStyle must exactly match the family value, not the filename.