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: italicfamilyis the name your code will use. It does not have to match the file names.- Each file is one variation.
weightis a number from 100 to 900 in steps of 100 (400 is regular, 700 is bold).style: italicmarks an italic file. - A file with no
weightcounts 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.
The shortcut
Map every file to a weight number before you write any YAML.
List the files
Nunito-Regular.ttf,Nunito-Bold.ttf,Nunito-Italic.ttf.
The trap is declaring each file as its own family, like NunitoBold. Then FontWeight.bold on Nunito cannot find the bold file, and you end up switching family names by hand.
Drills
Answer fast — trust the rule.
Which weight number means bold?
A font file under a family has no weight line. What weight is it treated as?
Best way to make every screen in Sprout use Nunito?
The bold file was declared as a separate family NunitoBold. What does TextStyle(fontFamily: 'Nunito', fontWeight: FontWeight.bold) use?
Remember
- One family, one file per weight, one name in the theme.
- Map each file to a weight number (400, 700) or
style: italicfirst. - Trap: splitting weights into separate families.
