Sub topic 3
Mapping weights and italic styles to font files
This chapter assumes you know Dart syntax, can build a widget tree, and can edit pubspec.yaml and run flutter pub get.
Each fonts entry in pubspec.yaml maps one file to one face. The weight key sets the numeric weight from 100 to 900. The style key marks an italic file. When you omit weight, Flutter treats the file as normal weight (400).
flutter:
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: italicIn code, FontWeight and FontStyle select the face. FontWeight.w700 picks the 700 file, and FontStyle.italic picks the italic file.
import 'package:flutter/material.dart';
class Samples extends StatelessWidget {
const Samples({super.key});
@override
Widget build(BuildContext context) {
return const Column(
children: [
Text('Regular', style: TextStyle(fontFamily: 'Raleway')),
Text(
'Bold',
style: TextStyle(fontFamily: 'Raleway', fontWeight: FontWeight.w700),
),
Text(
'Italic',
style: TextStyle(fontFamily: 'Raleway', fontStyle: FontStyle.italic),
),
],
);
}
}> Flutter does not invent a missing face. If you request w700 and no 700 file is declared, Flutter uses the nearest declared weight.
Using assets and fonts that ship in packages
A package can bundle its own assets and fonts. Its pubspec.yaml must declare them. The app then names the package explicitly. For an image, pass package: to Image.asset. For a font, pass package: to TextStyle.
Image.asset('assets/logo.png', package: 'my_icons');
Text(
'Packaged font',
style: TextStyle(fontFamily: 'IconFont', package: 'my_icons'),
);Fixing missing assets and fonts that do not render
Most failures come from a mismatch between code and pubspec.yaml.
| Symptom | Likely cause | Fix |
|---|---|---|
| Image throws "Unable to load asset" | Path differs in case or spelling | Match the declared path exactly |
| Subfolder images are missing | Directory entries do not include subfolders | Declare each subfolder with its own trailing-slash entry |
| Font shows the default typeface | `fontFamily` name differs from the declared `family` | Copy the family name exactly |
| Changes do not appear | Hot reload does not reload assets or fonts | Stop the app and run it again |
Keeping bundled assets from bloating the app
Every declared file ships inside the app. Declare only the files the app uses, and avoid whole folders of unused images. Each font weight and style adds a file, so keep the font set small. Compress large images before adding them.
flowchart TD
A[Declare file in pubspec.yaml] --> B[flutter pub get and full restart]
B --> C[Widget requests asset or weight]
C --> D{Matching file declared?}
D -- Yes --> E[Asset or font renders]
D -- No --> F[Nearest face or error]Exercise
Q: Your pubspec.yaml declares Raleway regular (400) and bold (700) only. Which file does TextStyle(fontFamily: 'Raleway', fontWeight: FontWeight.w600) use?
A: The bold file. Flutter picks the nearest declared weight, and 700 is closer to 600 than 400 is.
Estimate
minutes: 12