While Android and iOS offer high quality system fonts, one of the most commonrequests from designers is to use custom fonts. For example, you may have acustom-built font from a designer, or maybe you downloaded a font fromGoogle Fonts.

Flutter works out of the box with custom fonts. You can apply fonts across anentire app or to individual Widgets.

Directions

  • Import the font files
  • Declare the font in the pubspec.yaml
  • Set a font as the default
  • Use a font in a specific Widget

1. Import the font files

In order to work with a font, you need to import the font files into theproject. It is common practice to put font files in a fonts or assetsfolder at the root of a Flutter project.

For example, if you want to import the Raleway and Roboto Mono font files intoa project, the folder structure would look like this:

  1. awesome_app/
  2. fonts/
  3. Raleway-Regular.ttf
  4. Raleway-Italic.ttf
  5. RobotoMono-Regular.ttf
  6. RobotoMono-Bold.ttf

2. Declare the font in the pubspec.yaml

Now that you have a font to work with, you need to tell Flutter where tofind it. You can do so by including a font definition in the pubspec.yaml.

  1. flutter:
  2. fonts:
  3. - family: Raleway
  4. fonts:
  5. - asset: fonts/Raleway-Regular.ttf
  6. - asset: fonts/Raleway-Italic.ttf
  7. style: italic
  8. - family: RobotoMono
  9. fonts:
  10. - asset: fonts/RobotoMono-Regular.ttf
  11. - asset: fonts/RobotoMono-Bold.ttf
  12. weight: 700

pubspec.yaml option definitions

The family determines the name of the font, which you use in thefontFamilyproperty of a TextStyleobject.

The asset is a path to the font file, relative to the pubspec.yaml file.These files contain the outlines for the glyphs in the font. When building theapp, these files are included in the app’s asset bundle.

A single font can reference many different files with different outline weightsand styles:

  • The weight property specifies the weight of the outlines in the file as aninteger multiple of 100, between 100 and 900. These values correspond to theFontWeightand can be used in thefontWeightproperty of aTextStyle object.

  • The style property specifies whether the outlines in the file areitalic or normal. These values correspond to theFontStyleand can be used in thefontStyleproperty of aTextStyle object.

3. Set a font as the default

You have two options for how to apply fonts to text: as the default fontor only within specific Widgets.

To use a font as the default, set the fontFamily property as part ofthe app’s theme. The value provided to fontFamily must match the familyname declared in the pubspec.yaml.

  1. MaterialApp(
  2. title: 'Custom Fonts',
  3. // Set Raleway as the default app font
  4. theme: ThemeData(fontFamily: 'Raleway'),
  5. home: MyHomePage(),
  6. );

For more information on themes, please view the “Using Themes to share colorsand font styles” recipe.

4. Use the font in a specific Widget

If you want to apply the font to a specific Widget, such as a Text Widget,provide a TextStyleto the Widget.

In this example, you’ll apply the RobotoMono font to a single Text Widget.Once again, the fontFamily must match the family name declared in thepubspec.yaml.

  1. Text(
  2. 'Roboto Mono sample',
  3. style: TextStyle(fontFamily: 'RobotoMono'),
  4. );

TextStyle

If a TextStyleobject specifies a weight or style for which is there is no exact font file, theengine uses one of the more generic files for the font and attempts toextrapolate outlines for the requested weight and style.

Complete example

Fonts

The Raleway and RobotoMono fonts were downloaded from GoogleFonts.

pubspec.yaml

  1. name: custom_fonts
  2. description: An example of how to use custom fonts with Flutter
  3. dependencies:
  4. flutter:
  5. sdk: flutter
  6. dev_dependencies:
  7. flutter_test:
  8. sdk: flutter
  9. flutter:
  10. fonts:
  11. - family: Raleway
  12. fonts:
  13. - asset: fonts/Raleway-Regular.ttf
  14. - asset: fonts/Raleway-Italic.ttf
  15. style: italic
  16. - family: RobotoMono
  17. fonts:
  18. - asset: fonts/RobotoMono-Regular.ttf
  19. - asset: fonts/RobotoMono-Bold.ttf
  20. weight: 700
  21. uses-material-design: true

main.dart

  1. import 'package:flutter/material.dart';
  2. void main() => runApp(MyApp());
  3. class MyApp extends StatelessWidget {
  4. @override
  5. Widget build(BuildContext context) {
  6. return MaterialApp(
  7. title: 'Custom Fonts',
  8. // Set Raleway as the default app font
  9. theme: ThemeData(fontFamily: 'Raleway'),
  10. home: MyHomePage(),
  11. );
  12. }
  13. }
  14. class MyHomePage extends StatelessWidget {
  15. @override
  16. Widget build(BuildContext context) {
  17. return Scaffold(
  18. // The AppBar uses the app-default Raleway font
  19. appBar: AppBar(title: Text('Custom Fonts')),
  20. body: Center(
  21. // This Text Widget uses the RobotoMono font
  22. child: Text(
  23. 'Roboto Mono sample',
  24. style: TextStyle(fontFamily: 'RobotoMono'),
  25. ),
  26. ),
  27. );
  28. }
  29. }

Custom Fonts Demo