ไธญๆๆๆกฃ | English
A Dart/Flutter code generator for iconfont.cn icons. Convert iconfont.cn icons to Flutter widgets with SVG rendering, supporting multi-color icons and null safety.
- ๐ Command Line Tool - Simple command-line interface, no build_runner needed
- ๐จ Multi-color Support - Render multi-color icons with custom color support
- ๐ฆ Pure Components - No font files needed, uses SVG rendering for smaller bundle size
- ๐ Automated Generation - Automatically fetch latest icons from iconfont.cn and generate Dart code
- ๐ก๏ธ Null Safety - Full Dart null safety support
- โก Easy Installation - Global installation via dart pub global activate
Install the command-line tool globally:
dart pub global activate flutter_iconfont_generator
Add to your pubspec.yaml
:
dependencies:
flutter:
sdk: flutter
flutter_svg: ^2.0.0
dev_dependencies:
flutter_iconfont_generator: ^2.0.0
Add iconfont configuration to your pubspec.yaml
:
# IconFont configuration
iconfont:
symbol_url: "//at.alicdn.com/t/font_xxx.js" # Get from iconfont.cn
save_dir: "./lib/iconfont" # Output directory
trim_icon_prefix: "icon" # Remove icon name prefix
default_icon_size: 18 # Default icon size
null_safety: true # Enable null safety
After global installation:
# Generate icons using pubspec.yaml configuration
iconfont_generator
# Generate with custom parameters
iconfont_generator --url "//at.alicdn.com/t/font_xxx.js" --output lib/icons
# Show verbose output
iconfont_generator --verbose
# Show help
iconfont_generator --help
If not globally installed:
# Run from your project root
dart run flutter_iconfont_generator:iconfont_generator
# Or if you added it as a dev dependency
dart run flutter_iconfont_generator
import 'package:your_app/iconfont/iconfont.dart';
// Basic usage
IconFont(IconNames.home)
// With size
IconFont(IconNames.user, size: 24)
// With color
IconFont(IconNames.settings, size: 32, color: '#ff0000')
// With multiple colors (for multi-color icons)
IconFont(IconNames.logo, size: 48, colors: ['#ff0000', '#00ff00', '#0000ff'])
# Basic usage
iconfont_generator
# Custom options
iconfont_generator \
--url "//at.alicdn.com/t/c/font_4937193_3aohv86wocr.js" \
--output lib/icons \
--prefix icon \
--size 24 \
--verbose
# Options:
# -h, --help Show usage help
# -v, --verbose Show verbose output
# -c, --config Path to config file (default: pubspec.yaml)
# -u, --url IconFont symbol URL (overrides config)
# -o, --output Output directory (overrides config)
# -p, --prefix Icon prefix to trim (overrides config)
# -s, --size Default icon size (overrides config)
// Using custom color
IconFont(
IconNames.alipay,
size: 32,
color: 'ff0000', // Without # prefix
)
// Multi-color icons
IconFont(
IconNames.colorful_icon,
size: 32,
colors: ['ff0000', '00ff00', '0000ff'],
)
Option | Type | Default | Description |
---|---|---|---|
symbol_url |
String | - | Required. JavaScript URL from iconfont.cn |
save_dir |
String | ./lib/iconfont |
Output directory for generated files |
trim_icon_prefix |
String | icon |
Prefix to remove from icon names |
default_icon_size |
int | 18 |
Default size for icons |
null_safety |
bool | true |
Enable null safety in generated code |
- Go to iconfont.cn
- Create or open your icon project
- Go to project settings (้กน็ฎ่ฎพ็ฝฎ)
- Find "FontClass/Symbolๅ็ผ" section
- Copy the JavaScript URL (should look like
//at.alicdn.com/t/c/font_xxx_xxx.js
)
"No iconfont configuration found"
- Make sure you have the
iconfont:
section in yourpubspec.yaml
"Please configure a valid symbol_url"
- Check that your symbol URL is from iconfont.cn and is accessible
- The URL should start with
//at.alicdn.com/
"No icons found in the SVG content"
- Verify your symbol URL is correct
- Check that your iconfont project has icons
- Try accessing the URL in a browser
Permission denied
- Make sure you have write permissions to the output directory
"Conflicting outputs" - Build conflicts with other generators
- If you encounter conflicts like "Both xxx and flutter_iconfont_generator:iconfont_builder may output", create or modify your
build.yaml
file to limit the builder scope:
targets:
$default:
builders:
flutter_iconfont_generator:iconfont_builder:
enabled: true
generate_for:
- lib/iconfont/**
- lib/**/iconfont.dart
options:
include:
- "lib/iconfont/**"
- "lib/**/iconfont.dart"
- Alternatively, run the command-line tool instead of build_runner:
# Use command-line tool to avoid build conflicts
iconfont_generator
Use verbose mode to see detailed information:
iconfont_generator --verbose
Check out the example app for a complete implementation showing all features.
Contributions are welcome! Please feel free to submit a Pull Request.
This project is licensed under the MIT License - see the LICENSE file for details.
- Thanks to iconfont.cn for providing excellent icon service
- Inspired by similar tools in the React ecosystem
- Multi-color Support - SVG naturally supports multi-color rendering
- Smaller Bundle Size - Only includes used icons, no redundant data
- Better Compatibility - No dependency on system fonts, better cross-platform consistency
- Code as Icons - Icons exist as Dart code, easier for version control and management
- Pure Dart Implementation - Leverages Dart ecosystem, no additional runtime environment needed
- Compile-time Generation - Icons are determined at compile time, better runtime performance
- Type Safety - Provides type-safe icon references through enums
- On-demand Loading - Only generates icons actually used in the project
Contributions are welcome! Please feel free to submit a Pull Request.
- Fork this repository
- Create your feature branch (
git checkout -b feature/AmazingFeature
) - Commit your changes (
git commit -m 'Add some AmazingFeature'
) - Push to the branch (
git push origin feature/AmazingFeature
) - Open a Pull Request
This project is licensed under the MIT License - see the LICENSE file for details.
- iconfont.cn - Alibaba Vector Icon Library
- Flutter SVG - Flutter SVG rendering plugin
- build_runner - Dart code generation tool
This project is a pure Dart refactored version based on flutter-iconfont-cli. Since the original repository is no longer maintained, we use this new repository to continue maintaining and developing this project.
If this project helps you, please give it a โญ๏ธ Star!