IconFontBuilder class
A build_runner Builder for generating Flutter IconFont widgets from iconfont.cn icons.
This builder automatically fetches SVG icons from iconfont.cn and generates Flutter widgets with proper type safety and null safety support.
Configuration
Add the following configuration to your pubspec.yaml
:
iconfont:
symbol_url: "//at.alicdn.com/t/font_xxx.js" # Your iconfont.cn URL
save_dir: "./lib/iconfont" # Output directory
trim_icon_prefix: "icon" # Icon prefix to remove
default_icon_size: 18 # Default icon size
null_safety: true # Enable null safety
Build Configuration
Add this builder to your build.yaml
:
targets:
$default:
builders:
flutter_iconfont_generator|iconfont_builder:
enabled: true
Usage
- Create a trigger file (e.g.,
lib/iconfont.dart
) - Run
flutter packages pub run build_runner build
- Import and use the generated icons:
import 'package:your_app/iconfont.dart';
// Simple usage
IconFont(IconNames.home)
// With custom size and color
IconFont(IconNames.user, size: 24, color: '#ff0000')
// With multiple colors for multi-color icons
IconFont(IconNames.multiColor, colors: ['#ff0000', '#00ff00', '#0000ff'])
Features
- Type Safety: Generated enum ensures compile-time icon name validation
- Null Safety: Full null safety support when enabled
- Multi-color Icons: Support for icons with multiple colors
- Customizable: Configurable icon sizes, prefixes, and output directories
- Hot Reload: Works seamlessly with Flutter hot reload
- SVG Rendering: Uses flutter_svg for high-quality vector rendering
Error Handling
The builder includes comprehensive error handling:
- Network failures when fetching icons
- Invalid SVG content
- Missing configuration
- File system errors
All errors are logged with detailed information to help with debugging.
Constructors
Properties
-
buildExtensions
→ Map<
String, List< String> > -
Defines the build extensions for this builder.
no setter
- hashCode → int
-
The hash code for this object.
no setterinherited
- runtimeType → Type
-
A representation of the runtime type of the object.
no setterinherited
Methods
-
build(
BuildStep buildStep) → Future< void> - Main build method that processes input files and generates icon font code.
-
noSuchMethod(
Invocation invocation) → dynamic -
Invoked when a nonexistent method or property is accessed.
inherited
-
toString(
) → String -
A string representation of this object.
inherited
Operators
-
operator ==(
Object other) → bool -
The equality operator.
inherited