| [ Web Proxy ] |
| Viewing: https://docs.flutter.dev/cookbook/design/fonts | [Back] [Original] |
docs.flutter.dev uses cookies from Google to deliver and enhance the quality of its services and to analyze traffic.
How to use custom fonts.
Although Android and iOS offer high quality system fonts, designers want support for custom fonts. You might have a custom-built font from a designer, or perhaps you downloaded a font from Google Fonts.
A typeface is the collection of glyphs or shapes that comprise a given style of lettering. A font is one representation of that typeface at a given weight or variation. Roboto is a typeface and Roboto Bold is a font.
Flutter lets you apply a custom font across an entire app or to individual widgets. This recipe creates an app that uses custom fonts with the following steps.
You don't need to follow each step as you go. The guide offers completed example files at the end.
This guide makes the following presumptions:
custom_fonts.
If you haven't completed these steps yet, do so before continuing
with this guide.
vi. You can substitute any text editor for vi.
Windows users should use the appropriate commands and paths when
performing the steps.
Your choice of font should be more than a preference. Consider which file formats work with Flutter and how the font could affect design options and app performance.
Flutter supports the following font formats:
.ttc.ttf.otf
Flutter does not support fonts in the Web Open Font Format,
.woff and .woff2, on desktop platforms.
Few sources agree on what a font file type is or which uses less space. The key difference between font file types involves how the format encodes the glyphs in the file. Most TrueType and OpenType font files have similar capabilities as they borrowed from each other as the formats and fonts improved over time.
Which font you should use depends on the following considerations.
Research what options a given font offers, like more than one weight or style per font file, variable font capability, the availability of multiple font files for a multiple font weights, or more than one width per font.
Choose the typeface or font family that meets the design needs of your app.
To learn how to get direct access to over 1,000 open-sourced font families, check out the google_fonts package.
Watch on YouTube in a new tab: "google_fonts | Flutter package of the week"
To learn about another approach to using custom fonts that allows you to re-use one font over multiple projects, check out Export fonts from a package.
To work with a font, import its font files into your Flutter project.
To import font files, perform the following steps.
If necessary, to match the remaining steps in this guide,
change the name of your Flutter app to custom_fonts.
mv /path/to/my_app /path/to/custom_fonts
Navigate to the root of your Flutter project.
cd /path/to/custom_fonts
Create a fonts directory at the root of your Flutter project.
mkdir fonts
Move or copy the font files in a fonts or assets
folder at the root of your Flutter project.
cp ~/Downloads/*.ttf ./fonts
The resulting folder structure should resemble the following:
custom_fonts/
|- fonts/
|- Raleway-Regular.ttf
|- Raleway-Italic.ttf
|- RobotoMono-Regular.ttf
|- RobotoMono-Bold.ttf
After you've downloaded a font,
include a font definition in the pubspec.yaml file.
This font definition also specifies which font file should be used to
render a given weight or style in your app.
pubspec.yaml fileTo add font files to your Flutter app, complete the following steps.
Open the pubspec.yaml file at the root of your Flutter project.
vi pubspec.yaml
Paste the following YAML block after the flutter declaration.
fonts:
- family: Raleway
fonts:
- asset: fonts/Raleway-Regular.ttf
- asset: fonts/Raleway-Italic.ttf
style: italic
- family: RobotoMono
fonts:
- asset: fonts/RobotoMono-Regular.ttf
- asset: fonts/RobotoMono-Bold.ttf
weight: 700
This pubspec.yaml file defines the italic style for the
Raleway font family as the Raleway-Italic.ttf font file.
When you set style: TextStyle(fontStyle: FontStyle.italic),
Flutter swaps Raleway-Regular with Raleway-Italic.
The family value sets the name of the typeface.
You use this name in the fontFamily
property of a TextStyle
object.
The value of an asset is a relative path from the pubspec.yaml file
to the font file.
These files contain the outlines for the glyphs in the font.
When building the app,
Flutter includes these files in the app's asset bundle.
Different typefaces implement font files in different ways. If you need a typeface with a variety of font weights and styles, choose and import font files that represent that variety.
When you import a font file that doesn't include either multiple fonts
within it or variable font capabilities,
don't use the style or weight property to adjust how they display.
If you use those properties on a regular font file,
Flutter attempts to simulate the look.
The visual result will look quite different from using the correct font file.
Flutter doesn't infer a font's weight or style from its file name.
To use a specific weight or style, you must explicitly declare the weight
or style property for that font file in your pubspec.yaml.
The weight property specifies the weight of the outlines in
the file as an integer multiple of 100, between 100 and 900.
These values correspond to the FontWeight
and can be used in the
fontWeight
property of a TextStyle
object.
In the pubspec.yaml shown in this guide,
you defined RobotoMono-Bold as the 700 weight of the font family.
To use the RobotoMono-Bold font that you added to your app,
set fontWeight to FontWeight.w700 in your TextStyle
widget.
If you hadn't added RobotoMono-Bold to your app and declared its weight,
Flutter attempts to make the regular font look bold.
The text then might appear to be somewhat darker.
You can't use the weight property to override the weight of the font.
You can't set RobotoMono-Bold to any other weight than 700.
If you set TextStyle(fontFamily: 'RobotoMono', fontWeight: FontWeight.w900),
the displayed font would still render as however bold RobotoMono-Bold
looks.
The style property specifies whether the glyphs in the font file display as
either italic or normal.
These values correspond to the FontStyle.
You can use these styles in the fontStyle
property
of a TextStyle
object.
In the pubspec.yaml shown in this guide,
you defined Raleway-Italic as being in the italic style.
To use the Raleway-Italic font that you added to your app,
set style: TextStyle(fontStyle: FontStyle.italic).
Flutter swaps Raleway-Regular with Raleway-Italic when rendering.
If you hadn't added Raleway-Italic to your app and declared its style,
Flutter attempts to make the regular font look italic.
The text then might appear to be leaning to the right.
You can't use the style property to override the glyphs of a font.
If you set TextStyle(fontFamily: 'Raleway', fontStyle: FontStyle.normal),
the displayed font would still render as italic.
The regular style of an italic font is italic.
To apply a font to text, you can set the font as the app's default font
in its theme.
To set a default font, set the fontFamily property in the app's theme.
Match the fontFamily value to the family name declared in the
pubspec.yaml file.
The result would resemble the following code.
return MaterialApp(
title: 'Custom Fonts',
// Set Raleway as the default app font.
theme: ThemeData(fontFamily: 'Raleway'),
home: const MyHomePage(),
);
To learn more about themes, check out the Using Themes to share colors and font styles recipe.
To apply the font to a specific widget like a Text widget,
provide a TextStyle
to the widget.
For this guide,
try to apply the RobotoMono font to a single Text widget.
Match the fontFamily value to the family name declared in the
pubspec.yaml file.
The result would resemble the following code.
child: Text(
'Roboto Mono sample',
style: TextStyle(fontFamily: 'RobotoMono'),
),
If a TextStyle
object specifies a weight or style without a
corresponding font file, the engine uses a generic file for the font
and attempts to extrapolate outlines for the requested weight and style.
Avoid relying on this capability. Import the proper font file instead.
Download the Raleway and RobotoMono font files from Google Fonts.
pubspec.yaml fileOpen the pubspec.yaml file at the root of your Flutter project.
vi pubspec.yaml
Replace its contents with the following YAML.
name: custom_fonts
description: An example of how to use custom fonts with Flutter
dependencies:
flutter:
sdk: flutter
dev_dependencies:
flutter_test:
sdk: flutter
flutter:
fonts:
- family: Raleway
fonts:
- asset: fonts/Raleway-Regular.ttf
- asset: fonts/Raleway-Italic.ttf
style: italic
- family: RobotoMono
fonts:
- asset: fonts/RobotoMono-Regular.ttf
- asset: fonts/RobotoMono-Bold.ttf
weight: 700
uses-material-design: true
main.dart fileOpen the main.dart file in the lib/ directory of your Flutter project.
vi lib/main.dart
Replace its contents with the following Dart code.
import 'package:flutter/material.dart';
void main() => runApp(const MyApp());
class MyApp extends StatelessWidget {
const MyApp({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Custom Fonts',
// Set Raleway as the default app font.
theme: ThemeData(fontFamily: 'Raleway'),
home: const MyHomePage(),
);
}
}
class MyHomePage extends StatelessWidget {
const MyHomePage({super.key});
@override
Widget build(BuildContext context) {
return Scaffold(
// The AppBar uses the app-default Raleway font.
appBar: AppBar(title: const Text('Custom Fonts')),
body: const Center(
// This Text widget uses the RobotoMono font.
child: Text(
'Roboto Mono sample',
style: TextStyle(fontFamily: 'RobotoMono'),
),
),
);
}
}
The resulting Flutter app should display the following screen.
Unless stated otherwise, the documentation on this site reflects Flutter 3.44.7. Page last updated on 2026-05-05. View source or report an issue.
| Web Proxy Viewer | New URL | Original Page |