Sub topic 16
Adding custom icons
The built-in Icons class covers common needs. Sooner or later a design calls for a brand mark, a domain-specific symbol such as a barbell or a lab flask, or a set of icons that must match one visual style. This chapter shows how to ship those as an icon font and use them through the same Icon widget you already know.
This chapter assumes you have read the chapters on the Icon widget, IconTheme, and custom fonts in pubspec.yaml. You should be comfortable with StatelessWidget, const constructors, and named parameters.
How an icon font works
An icon font is an ordinary font file (usually .ttf) in which each glyph is a pictogram instead of a letter. Each glyph is mapped to a Unicode code point, usually in the Private Use Area (U+E000 to U+F8FF), so it never collides with real characters.
Flutter's Icon widget does not need an image. It takes an IconData, which is a code point plus a font family name. At paint time, Icon draws that one character with that font, at the size and color it resolves from its own arguments or from the nearest IconTheme.
Material icons work exactly this way. Icons.home is an IconData pointing at the MaterialIcons font. A custom icon is the same thing with your own font family.
Step 1: Get or build the icon font
You have three common sources:
- A design team exports an SVG set, and you convert it to a font with a generator such as FlutterIcon.com or IcoMoon. The tool produces a
.ttffile and a list of code points. - A package ships the font for you (for example, a third-party icon pack). You then skip most of this chapter and write
Icon(PackageIcons.name). - You use a font editor directly. This gives the most control but is rarely worth it.
Whichever you use, record the code point for each glyph. Generators usually print them in a mapping file or a generated Dart class. Treat that mapping as the source of truth: if you regenerate the font, the code points may change.
Icon fonts are single-color. Every glyph is a silhouette that takes whatever color the Icon is given. If an icon needs two or more colors, an icon font is the wrong tool (see the comparison table below).
Step 2: Register the font in pubspec.yaml
Place the file in your project, for example assets/fonts/AppIcons.ttf, then declare it under flutter:. This is the same fonts: block you used for text fonts.
flutter:
uses-material-design: true
fonts:
- family: AppIcons
fonts:
- asset: assets/fonts/AppIcons.ttfThe family value is the name you will use in Dart. Pick something unique. A family called Roboto would shadow or clash with other fonts.
After editing pubspec.yaml, run flutter pub get and do a full restart of the app. Hot reload does not pick up newly registered fonts, which is a common source of "my icon shows as a box" confusion.
Step 3: Wrap the code points in a Dart class
Do not scatter raw hex values through your widgets. Put them in one class with named constants.
import 'package:flutter/widgets.dart';
class AppIcons {
AppIcons._();
static const String _family = 'AppIcons';
static const IconData logo = IconData(0xe800, fontFamily: _family);
static const IconData cart = IconData(0xe801, fontFamily: _family);
static const IconData barbell = IconData(0xe802, fontFamily: _family);
}The private constructor AppIcons._() stops anyone from instantiating the class, since it only holds constants. Each field is static const, and that is not optional, as the tree-shaking section below explains.
IconData has a few other named parameters worth knowing:
fontPackage: set this when the font lives inside a package rather than your app. Flutter then looks up the font aspackages/<fontPackage>/<family>.matchTextDirection: whentrue, the glyph is mirrored in right-to-left locales. Use it for direction-sensitive icons such as "back" arrows or "send" symbols. Do not use it for logos or symmetric icons.
Step 4: Use the icons
Custom icons plug into everything that accepts an IconData: Icon, IconButton, ListTile.leading, BottomNavigationBarItem, and so on.
import 'package:flutter/material.dart';
import 'app_icons.dart';
class ToolbarDemo extends StatelessWidget {
const ToolbarDemo({super.key});
@override
Widget build(BuildContext context) {
return const IconTheme(
data: IconThemeData(color: Colors.indigo, size: 28),
child: Row(
mainAxisSize: MainAxisSize.min,
children: [
Icon(AppIcons.logo, semanticLabel: 'Company logo'),
SizedBox(width: 12),
Icon(AppIcons.cart, semanticLabel: 'Shopping cart'),
SizedBox(width: 12),
Icon(Icons.search, semanticLabel: 'Search'),
],
),
);
}
}The custom icons and the built-in Icons.search share the same size and color because all three read from the surrounding IconTheme. That is the main benefit of using Icon rather than drawing the glyph some other way. Mixing built-in and custom icons in one row is normal.
The file app_icons.dart is the class from step 3. The demo needs the AppIcons.ttf file registered as in step 2. If it is missing, Flutter draws a placeholder box instead of the glyph.
Tree shaking and the const rule
Release builds run icon tree shaking. The build tool scans your Dart code for IconData constants, then rewrites the font files so they contain only the glyphs you actually reference. This can shrink MaterialIcons from over a megabyte to a few kilobytes.
For this to work, the tool must find every IconData at build time. That is why the fields are const. If you create an IconData at runtime, for example from a code point read from JSON, the build fails with an error about non-constant IconData instances.
You have two options when you hit that error:
- Preferred: replace the dynamic value with a lookup into a
constmap of known icons, such asconst {'cart': AppIcons.cart}. - Fallback: build with
--no-tree-shake-icons. This ships the full fonts and gives up the size savings.
Common mistakes
- A box or question mark appears instead of the glyph. Usually the family name in
IconDatadoes not match thefamily:inpubspec.yaml, the asset path is wrong, or you only hot reloaded after editingpubspec.yaml. - The icon looks too big or too small. Glyph metrics depend on how the font was exported. Adjust the
sizeon theIconorIconThemeinstead of editing the font, or fix the em-box in the generator. - Codepoints changed after regenerating the font. Regenerate the Dart class from the new mapping at the same time, and review the diff.
- Icon is not announced by a screen reader. Custom glyphs have no built-in description. Give
IconasemanticLabel, or wrap it in a labeledSemantics, unless it is purely decorative.
Choosing an approach
| Approach | Colors | Scales cleanly | Tree shaken | Best for |
|---|---|---|---|---|
| Icon font with `IconData` | One, set by `Icon` | Yes | Yes | A consistent set of monochrome UI icons |
| SVG via a rendering package | Any | Yes | No | Multicolor or illustrative artwork |
| `Image.asset` with PNG | Any | Only at fixed resolutions | No | Photos and raster logos |
A rule of thumb: if the icon should change color with the theme (selected, disabled, dark mode), use an icon font. If the artwork has a fixed palette, use SVG or an image.
Exercise
Your app registers the font family AppIcons and defines static const IconData cart = IconData(0xe801, fontFamily: 'AppIcons');. In a debug run the cart icon shows as an empty box. You just added the font to pubspec.yaml and hot reloaded. Name the likely cause and the fix. Then say what error you would expect in a release build if you had instead written IconData(int.parse(code), fontFamily: 'AppIcons') with a runtime value.
Show answer
Hot reload does not load newly registered fonts, so run flutter pub get and fully restart the app. If that does not help, check that the family in pubspec.yaml and the asset path match exactly. A runtime-built IconData is not a constant, so icon tree shaking cannot find it, and the release build fails with an error about non-constant IconData instances. Fix it with a const map of known icons, or build with --no-tree-shake-icons.