-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
10b58dd
commit 0f69501
Showing
21 changed files
with
777 additions
and
145 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
|
||
import 'package:app_config/app_config.dart'; | ||
import 'package:flutter/material.dart'; | ||
|
||
|
||
|
||
class GenMessageAction extends StatelessWidget { | ||
final VoidCallback onGen; | ||
const GenMessageAction({Key? key, required this.onGen}) : super(key: key); | ||
|
||
@override | ||
Widget build(BuildContext context) { | ||
return Row( | ||
crossAxisAlignment: CrossAxisAlignment.start, | ||
children: [ | ||
Expanded(child: Text( | ||
'使用方式:\n1. 在 iconfont.cn 挑选图标,加入项目,下载压缩包。\n2. 选择 Flutter 项目地址,配置资源、产物文件位置。\n3. 点击生成代码按钮,即可生成相关代码。', | ||
style: TextStyle( | ||
color: Theme.of(context).primaryColor,fontWeight: FontWeight.bold),)), | ||
ElevatedButton( | ||
style: ElevatedButton.styleFrom( | ||
elevation: 0, | ||
shape: const StadiumBorder() | ||
), | ||
onPressed:onGen, child: Wrap( | ||
crossAxisAlignment: WrapCrossAlignment.center, | ||
spacing: 4, | ||
children: [ | ||
Icon(TolyIcon.icon_fast,size: 16,), | ||
const Text('生成代码',style: TextStyle(height: 1,fontSize: 12),), | ||
], | ||
)), | ||
], | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,148 @@ | ||
import 'dart:convert'; | ||
import 'dart:io'; | ||
import 'package:archive/archive_io.dart'; | ||
|
||
import 'icon_font_gen_config.dart'; | ||
import 'package:path/path.dart' as path; | ||
|
||
class IconFontClassParser{ | ||
|
||
void gen(IconFontGenConfig config){ | ||
final inputStream = InputFileStream(config.srcZip); | ||
// 将压缩包有用资源解压到目标文件 | ||
final archive = ZipDecoder().decodeBuffer(inputStream); | ||
for (var file in archive.files) { | ||
if (file.isFile) { | ||
if (file.name.endsWith('.ttf')) { | ||
final outputStream = OutputFileStream(config.ttfDistPath); | ||
file.writeContent(outputStream); | ||
outputStream.close(); | ||
} | ||
if (file.name.endsWith('.json')) { | ||
dynamic data = file.content; | ||
String jsonContent = utf8.decode(data); | ||
String resultCode = parser(jsonContent,config.fontFamily); | ||
File distFile = File(config.distFilePath); | ||
if(!distFile.existsSync()){ | ||
distFile.createSync(recursive: true); | ||
} | ||
distFile.writeAsStringSync(resultCode); | ||
setYaml(config); | ||
} | ||
} | ||
} | ||
} | ||
|
||
String parser(String input,String fontFamily){ | ||
dynamic map = json.decode(input); | ||
List<dynamic> glyphs = map['glyphs'] as List<dynamic>; | ||
String code = ''; | ||
for(int i=0;i<glyphs.length;i++){ | ||
String fieldName = glyphs[i]['font_class']; | ||
String unicode = glyphs[i]['unicode']; | ||
String lineCode = """static const IconData $fieldName = IconData(0x$unicode, fontFamily: "$fontFamily");\n"""; | ||
code+=lineCode; | ||
} | ||
|
||
String result = | ||
""" | ||
import 'package:flutter/widgets.dart'; | ||
// Power By 张风捷特烈--- Generated file. Do not edit. | ||
// 欢迎支持: https://github.com/toly1994328/FlutterUnit | ||
class $fontFamily { | ||
$fontFamily._(); | ||
$code | ||
} | ||
"""; | ||
return result; | ||
} | ||
|
||
// 修改 pubspec.yaml | ||
void setYaml(IconFontGenConfig config){ | ||
String familyName = config.fontFamily; | ||
String fontAssetsDist = config.yamlAssetDist; | ||
final String filePath = path.join(config.projectPath,'pubspec.yaml'); | ||
// final String filePath = r'E:\Projects\Flutter\FlutterUnit\pubspec.yaml'; | ||
|
||
File pubspecFile = File(filePath); | ||
|
||
List<String> lines = pubspecFile.readAsLinesSync(); | ||
|
||
RegExp fontsRegex = RegExp(r'^ fonts:',multiLine: true); | ||
bool hasFonts = fontsRegex.hasMatch(lines.join('\n')); | ||
|
||
if(!hasFonts){ | ||
// 当前没有 fonts 节点,需要添加到 flutter 节点下 | ||
int index = lines.indexWhere((e) => e.startsWith('flutter:')); | ||
List<String> fonts = [ | ||
' fonts:', | ||
' - family: $familyName', | ||
' fonts:', | ||
' - asset: $fontAssetsDist', | ||
]; | ||
|
||
lines.insertAll(index+1, fonts); | ||
pubspecFile.writeAsStringSync(lines.join('\n')); | ||
return; | ||
} | ||
// 存在 fonts 节点,查询 family ,有没有当前字体图标 | ||
bool hasTargetFamily = false; | ||
RegExp regExp = RegExp(r'^ +- family: +(\w+)'); | ||
|
||
for(int i=0;i<lines.length;i++){ | ||
String line = lines[i]; | ||
if(line.startsWith(regExp)){ | ||
String family = regExp.allMatches(line).first.group(1)??''; | ||
if(family == familyName){ | ||
hasTargetFamily = true; | ||
break; | ||
} | ||
} | ||
} | ||
if(!hasTargetFamily){ | ||
int index = lines.indexWhere((e) => e.startsWith(fontsRegex)); | ||
List<String> fonts = [ | ||
' - family: $familyName', | ||
' fonts:', | ||
' - asset: $fontAssetsDist', | ||
]; | ||
lines.insertAll(index+1, fonts); | ||
pubspecFile.writeAsStringSync(lines.join('\n')); | ||
return; | ||
} | ||
} | ||
|
||
// // 修改 pubspec.yaml | ||
// void setYaml(IconFontGenConfig config){ | ||
// String fontFamily = config.fontFamily; | ||
// String assets = config.assetsDist.replaceAll('\\', '/'); | ||
// | ||
// final String filePath = path.join(config.projectPath,'pubspec.yaml'); | ||
// File pubspecFile = File(filePath); | ||
// final String pubspec = pubspecFile.readAsStringSync(); | ||
// final doc = loadYaml(pubspec); | ||
// final modifiableDoc = getModifiableNode(doc); | ||
// | ||
// YamlList? fontsList = doc['flutter']['fonts'] as YamlList?; | ||
// if(fontsList == null){ | ||
// // 新文件,没有配置 fonts 节点 | ||
// modifiableDoc['flutter']['fonts'] = YamlMap.wrap({ | ||
// 'family': fontFamily, | ||
// 'fonts':YamlList.wrap([YamlMap.wrap({'asset':'$assets/iconfont.ttf'})]) | ||
// }); | ||
// }else{ | ||
// final modifiableList = getModifiableNode(fontsList); | ||
// modifiableList.removeWhere((e) => e['family'] == fontFamily); | ||
// modifiableList.add( | ||
// YamlMap.wrap({ | ||
// 'family': fontFamily, | ||
// 'fonts':YamlList.wrap([YamlMap.wrap({'asset':'$assets/iconfont.ttf'})]) | ||
// }) | ||
// ); | ||
// modifiableDoc['flutter']['fonts'] = modifiableList; | ||
// } | ||
// final targetYaml = toYamlString(modifiableDoc); | ||
// pubspecFile.writeAsStringSync(targetYaml); | ||
// } | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import 'package:path/path.dart' as path; | ||
|
||
class IconFontGenConfig { | ||
final String srcZip; | ||
final String projectPath; | ||
final String assetsDist; | ||
final String fileDist; | ||
|
||
IconFontGenConfig({ | ||
this.srcZip = '', | ||
this.projectPath = '', | ||
String? assetsDist, | ||
String? fileDist, | ||
}) : fileDist = fileDist ?? 'lib${spa}app${spa}gen${spa}toly_icon.dart', | ||
assetsDist = assetsDist ?? 'assets${spa}iconfont'; | ||
|
||
static String get spa => path.separator; | ||
|
||
String get distFilePath => path.join(projectPath, fileDist); | ||
|
||
String get distAssetsDir => path.join(projectPath, assetsDist); | ||
|
||
String get ttfDistPath => path.join(distAssetsDir, path.basenameWithoutExtension(fileDist)+".ttf"); | ||
|
||
String get yamlAssetDist => assetsDist.replaceAll('\\', '/')+"/"+path.basename(ttfDistPath); | ||
|
||
String get fontFamily => path | ||
.basenameWithoutExtension(fileDist) | ||
.split('_') | ||
.map((e) => e[0].toUpperCase() + e.substring(1,)) | ||
.join(''); | ||
|
||
factory IconFontGenConfig.fromJson(Map<String, dynamic> map) { | ||
return IconFontGenConfig( | ||
srcZip: map['srcZip'] ?? '', | ||
projectPath: map['projectPath'] ?? '', | ||
assetsDist: map["assetsDist"] ?? '', | ||
fileDist: map["fileDist"] ?? ''); | ||
} | ||
|
||
Map<String, dynamic> toJson() => { | ||
'srcZip': srcZip, | ||
'projectPath': projectPath, | ||
'assetsDist': assetsDist, | ||
'fileDist': fileDist, | ||
}; | ||
} |
Oops, something went wrong.