Skip to the workspace
Pebellslearn by building
Working with Assets  ›  Lesson

Fonts

Fonts 12 minCustom fonts are declared in pubspec.yaml as a family with one file per weight or style, and applied through fontFamily in a TextStyle or in the app theme.

flutter / Fonts

Explore each scene at your own pace.

01 · The rule

The rule

Fonts

Sprout's designer picked a friendly rounded typeface and sent two files: a regular and a bold. Dropping them into the project is not enough. Flutter needs to know that both files belong to one family, and which one is the bold.

The rule

One family, one file per weight, one name in the theme.

flutter:
  fonts:
    - family: Nunito
      fonts:
        - asset: fonts/Nunito-Regular.ttf
        - asset: fonts/Nunito-Bold.ttf
          weight: 700
        - asset: fonts/Nunito-Italic.ttf
          style: italic
  • family is the name your code will use. It does not have to match the file names.
  • Each file is one variation. weight is a number from 100 to 900 in steps of 100 (400 is regular, 700 is bold). style: italic marks an italic file.
  • A file with no weight counts as regular (400).

Apply it to one piece of text:

Text('Sprout', style: TextStyle(fontFamily: 'Nunito', fontWeight: FontWeight.bold))

or, far more often, to the whole app:

MaterialApp(theme: ThemeData(fontFamily: 'Nunito'), home: const SproutHome())

When a Text asks for FontWeight.bold, Flutter looks in the Nunito family for the file declared with weight 700. If there is none, it uses the closest weight it has, and bold may look faked or not bold at all.

An alternative is the google_fonts package, which fetches fonts by name at run time or bundles them. Declaring files yourself keeps everything offline and under your control.

Scene 1 of 5
Hover to wake the cannons. Drag to move, or use arrow keys when focused. Press Enter to fire a shot.