Skip to content

🌟: 增加透明度属性 #15

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
147 changes: 147 additions & 0 deletions README_DART.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
# Flutter IconFont CLI - Dart 版本

基于 Dart 重构的 iconfont.cn 图标转换工具,使用 build_runner 进行代码生成。

## 主要特性

- ✅ 纯 Dart 实现,无需 Node.js
- ✅ 通过 `pubspec.yaml` 配置参数
- ✅ 使用 `build_runner` 进行代码生成
- ✅ 支持 null safety
- ✅ 支持多色图标
- ✅ 命令行工具和构建器两种使用方式

## 配置说明

在 `pubspec.yaml` 中添加配置:

```yaml
dependencies:
flutter:
sdk: flutter
flutter_svg: ^2.0.0
http: ^0.13.0
xml: ^6.0.0
path: ^1.8.0

dev_dependencies:
flutter_test:
sdk: flutter
build_runner: ^2.3.0
source_gen: ^1.2.0
analyzer: ^5.0.0
build: ^2.3.0
yaml: ^3.1.0

# IconFont 配置
iconfont:
symbol_url: "//at.alicdn.com/t/font_8d5l8fzk5b87iudi.js" # 从 iconfont.cn 获取
save_dir: "./lib/iconfont" # 输出目录
trim_icon_prefix: "icon" # 移除图标名前缀
default_icon_size: 18 # 默认图标大小
null_safety: true # 是否使用 null safety
```

## 使用方法

### 方法一:使用 build_runner(推荐)

1. 添加配置到 `pubspec.yaml`
2. 运行代码生成:

```bash
# 一次性生成
dart run build_runner build

# 监听模式(文件变化时自动重新生成)
dart run build_runner watch

# 强制重新生成
dart run build_runner build --delete-conflicting-outputs
```

### 方法二:使用命令行工具

```bash
# 直接运行生成器
dart run bin/iconfont_generator.dart

# 或者安装后使用
dart pub global activate --source path .
iconfont_generator
```

## 生成的代码使用

```dart
import 'package:your_app/iconfont/iconfont.dart';

// 基本使用
IconFont(IconNames.home)

// 指定大小和颜色
IconFont(
IconNames.user,
size: 24,
color: '#ff0000',
)

// 多色图标
IconFont(
IconNames.colorful_icon,
size: 32,
colors: ['#ff0000', '#00ff00', '#0000ff'],
)

// 使用字符串(动态)
IconFont('home', size: 20)
```

## 从 TypeScript 版本迁移

1. 删除 `node_modules` 和 `package.json`
2. 更新 `pubspec.yaml` 配置
3. 运行 `dart pub get`
4. 使用 `dart run build_runner build` 生成图标

## 配置参数说明

| 参数 | 类型 | 默认值 | 说明 |
|-----|------|--------|------|
| `symbol_url` | String | - | iconfont.cn 提供的 Symbol 链接 |
| `save_dir` | String | "./lib/iconfont" | 生成文件的保存目录 |
| `trim_icon_prefix` | String | "icon" | 要移除的图标名前缀 |
| `default_icon_size` | int | 18 | 默认图标大小 |
| `null_safety` | bool | true | 是否生成 null safety 代码 |

## 故障排除

### 常见问题

1. **找不到配置**: 确保 `pubspec.yaml` 中有 `iconfont` 配置节
2. **网络错误**: 检查 `symbol_url` 是否正确
3. **生成失败**: 运行 `dart run build_runner clean` 后重试

### 调试模式

```bash
# 查看详细错误信息
dart run bin/iconfont_generator.dart --verbose
```

## 开发说明

项目结构:
```
lib/
├── src/
│ ├── config.dart # 配置模型
│ ├── fetcher.dart # 网络请求
│ ├── svg_parser.dart # SVG 解析
│ └── generator.dart # 代码生成
├── builder.dart # build_runner 构建器
└── iconfont.dart # 入口文件

bin/
└── iconfont_generator.dart # 命令行工具
```
155 changes: 155 additions & 0 deletions USAGE_GUIDE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
# Flutter IconFont CLI - Dart 版本使用指南

## 快速开始

### 1. 配置 pubspec.yaml

```yaml
dependencies:
flutter:
sdk: flutter
flutter_svg: ^2.0.0
http: ^0.13.0
xml: ^6.0.0
path: ^1.8.0

dev_dependencies:
build_runner: ^2.3.0
yaml: ^3.1.0

# IconFont 配置
iconfont:
symbol_url: "//at.alicdn.com/t/font_xxx.js" # 从 iconfont.cn 复制
save_dir: "./lib/iconfont"
trim_icon_prefix: "icon"
default_icon_size: 18
null_safety: true
```

### 2. 获取 iconfont.cn 链接

1. 登录 [iconfont.cn](https://iconfont.cn)
2. 创建或选择图标项目
3. 点击"Font class" 或 "Symbol"
4. 复制生成的 JS 链接 (如: `//at.alicdn.com/t/font_xxx.js`)
5. 将链接配置到 `pubspec.yaml` 的 `symbol_url`

### 3. 生成图标代码

```bash
# 安装依赖
dart pub get

# 方法一: 使用简化生成器(推荐)
dart run bin/simple_generator.dart

# 方法二: 使用 build_runner
dart run build_runner build

# 方法三: 使用完整生成器
dart run bin/iconfont_generator.dart
```

### 4. 使用生成的图标

```dart
import 'package:your_app/iconfont/iconfont.dart';

// 基本使用
IconFont(IconNames.home)

// 设置大小和颜色
IconFont(
IconNames.user,
size: 24,
color: '#ff0000',
)

// 多色图标
IconFont(
IconNames.colorful,
size: 32,
colors: ['#ff0000', '#00ff00', '#0000ff'],
)

// 动态使用(字符串)
IconFont('home', size: 20)
```

## 配置选项

| 选项 | 类型 | 默认值 | 说明 |
|-----|------|--------|------|
| `symbol_url` | String | - | iconfont.cn 的 Symbol 或 JS 链接 |
| `save_dir` | String | "./lib/iconfont" | 生成文件保存目录 |
| `trim_icon_prefix` | String | "icon" | 要移除的图标前缀 |
| `default_icon_size` | int | 18 | 默认图标大小 |
| `null_safety` | bool | true | 是否生成 null safety 代码 |

## 常见问题

### Q: 如何更新图标?
A: 重新运行生成命令即可,会自动覆盖旧文件。

### Q: 支持多色图标吗?
A: 支持,使用 `colors` 参数传入颜色数组。

### Q: 可以动态使用图标吗?
A: 可以,传入字符串形式的图标名称。

### Q: 生成失败怎么办?
A: 检查网络连接和 `symbol_url` 是否正确。

## 从 TypeScript 版本迁移

1. 删除 `node_modules`、`package.json`、`package-lock.json`
2. 按上述步骤配置 `pubspec.yaml`
3. 运行 `dart pub get`
4. 使用 Dart 版本的生成命令

## 高级用法

### 自定义模板

你可以修改 `bin/simple_generator.dart` 中的模板函数来自定义生成的代码。

### 批量处理

```bash
# 监听文件变化自动生成
dart run build_runner watch

# 强制重新生成
dart run build_runner build --delete-conflicting-outputs
```

### 调试模式

```bash
dart run bin/simple_generator.dart --verbose
```

## 项目结构

生成后的项目结构:
```
lib/
├── iconfont/
│ └── iconfont.dart # 生成的图标文件
└── main.dart
```

## 示例项目

参考 `example/main.dart` 查看完整的使用示例。

## 问题反馈

如果遇到问题,请检查:

1. ✅ `pubspec.yaml` 配置是否正确
2. ✅ `symbol_url` 是否有效
3. ✅ 网络连接是否正常
4. ✅ 依赖是否已安装 (`dart pub get`)

更多问题请查看项目 README 或提交 Issue。
78 changes: 78 additions & 0 deletions bin/iconfont_generator.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
#!/usr/bin/env dart

import 'dart:io';
import 'package:yaml/yaml.dart';
import '../lib/src/config.dart';
import '../lib/src/fetcher.dart';
import '../lib/src/svg_parser.dart';
import '../lib/src/generator.dart';

/// Command line tool for generating iconfont
Future<void> main(List<String> arguments) async {
try {
print('🚀 Flutter IconFont Generator');
print('');

// Read configuration from pubspec.yaml
final pubspecFile = File('pubspec.yaml');
if (!await pubspecFile.exists()) {
print('❌ Error: pubspec.yaml not found in current directory');
exit(1);
}

final pubspecContent = await pubspecFile.readAsString();
final pubspec = loadYaml(pubspecContent) as Map;

final iconfontConfig = pubspec['iconfont'] as Map?;
if (iconfontConfig == null) {
print('❌ Error: No iconfont configuration found in pubspec.yaml');
print('');
print('Please add iconfont configuration to your pubspec.yaml:');
print('');
print('iconfont:');
print(' symbol_url: "your_iconfont_symbol_url_here"');
print(' save_dir: "./lib/iconfont"');
print(' trim_icon_prefix: "icon"');
print(' default_icon_size: 18');
print(' null_safety: true');
exit(1);
}

final config =
IconFontConfig.fromMap(Map<String, dynamic>.from(iconfontConfig));

if (config.symbolUrl.isEmpty || config.symbolUrl.contains('请参考README.md')) {
print('❌ Error: Please configure a valid symbol_url in pubspec.yaml');
exit(1);
}

print('📡 Fetching icons from: ${config.symbolUrl}');

// Fetch SVG content
final svgContent = await IconFontFetcher.fetchSvgContent(config.symbolUrl);

// Parse symbols
final symbols = SvgParser.parseSymbols(svgContent);

if (symbols.isEmpty) {
print('❌ Warning: No icons found in the SVG content');
return;
}

print('✅ Found ${symbols.length} icons');

// Generate code
await CodeGenerator.generateIconFont(symbols, config);

print('🎉 Successfully generated iconfont code!');
print('📁 Output directory: ${config.saveDir}');
} catch (e, stackTrace) {
print('❌ Error generating iconfont: $e');
if (arguments.contains('--verbose')) {
print('');
print('Stack trace:');
print(stackTrace);
}
exit(1);
}
}
Loading