Sub topic 16
Adding custom icons
Assume you know basic Dart syntax, Flutter widgets, StatelessWidget, the build method, and how to edit pubspec.yaml. You should also know how to use a Material icon such as Icons.home.
Material includes many icons, but an application may need a brand mark, a product-specific symbol, or an icon that Material does not provide. A custom icon font stores these symbols as glyphs. A glyph is a character-shaped drawing inside a font file. Flutter can draw one glyph with IconData.
Registering an icon font
Place the font file in your project, for example:
assets/fonts/app_icons.ttfRegister it under flutter.fonts:
flutter:
uses-material-design: true
fonts:
- family: AppIcons
fonts:
- asset: assets/fonts/app_icons.ttfThe family value is important. You must use the same value in IconData. Run flutter pub get after changing pubspec.yaml. A malformed indentation in this file can prevent Flutter from loading the font.
Creating IconData
Each glyph has a Unicode code point. Icon-font tools often assign custom icons code points in the private-use range, such as 0xe900. The exact value comes from the font's export or CSS file.
Create a Dart file to keep the definitions in one place:
import 'package:flutter/widgets.dart';
abstract final class AppIcons {
static const compass = IconData(
0xe900,
fontFamily: 'AppIcons',
);
static const package = IconData(
0xe901,
fontFamily: 'AppIcons',
);
}The code point and font family must both match the font. If either is wrong, Flutter may show an empty square or the wrong symbol.
Use the definition like any other icon:
import 'package:flutter/material.dart';
import 'app_icons.dart';
class ShippingCard extends StatelessWidget {
const ShippingCard({super.key});
@override
Widget build(BuildContext context) {
return const Card(
child: ListTile(
leading: Icon(
AppIcons.package,
semanticLabel: 'Package',
),
title: Text('Package ready for shipping'),
),
);
}
}Icon accepts the usual color, size, and semanticLabel properties. IconData describes how to draw the glyph. The Icon widget provides its place in the widget tree and its accessibility information.
Direction and theming
An icon that changes direction, such as an arrow, may need to mirror in right-to-left languages:
const IconData(
0xe902,
fontFamily: 'AppIcons',
matchTextDirection: true,
)Use IconTheme when a group should share styling:
const IconTheme(
data: IconThemeData(color: Colors.indigo, size: 28),
child: Row(
children: [
Icon(AppIcons.compass),
Icon(AppIcons.package),
],
),
)Custom icons do not automatically match Material symbols. Check their visual weight, baseline, and size beside nearby text. An icon font is usually best for simple, scalable symbols. Use an image or a custom-drawn widget for detailed illustrations.
Exercise
An icon appears as an empty square after you add a custom font. What should you check first?
Show answer
Check that the font asset path and family name in pubspec.yaml are correct, that the code point belongs to the font, and that you ran flutter pub get or restarted the application after changing the asset configuration.