小菜在学习过程中通常会需要大量的小图标,而多数的 Icon 都是经过设计同学优化过的,而如何采用类似系统 Icons 方式,此时可以通过自传类似字体库的 ttf 图标库的方式来完成;
ACE_ICON
小菜推荐两个网站可以用来生成图标库;
1. 生成 ACE_ICON.ttf 图标库
小菜以 https://www.fluttericon.com/ 为例来生成图标库;在这个图标库中有很多成套的图标,在这里可以选中需要的图标,也可以将本地生成的 .svg 文件拖拽进库中;
其中设置完图标库名称后,通过工具选择下载不同的文件,包括 ttf 图标库、config 配置文件以及 mapping 映射文件;
2. 集成 ttf 图标库
- 将 ACE_ICON.ttf 库文件添加到 project 资源文件中;
- 在 pubspec.yaml 文件中声明 ACE_ICON.tt 文件库;声明方式与字体库等一致,注意 family 与 font 名称一致;在此可以声明引入多个资源库文件;之后 pub get 即可;
fonts:
- family: DancingScript
fonts:
- asset: assets/DancingScript-Regular.ttf
- family: ACE_ICON
fonts:
- asset: images/ACE_ICON.ttf
- 将 mapping 文件添加到 project 中,用于映射 ACE_ICON 图标库中各 Icons 文件;
class ACE_ICON {
ACE_ICON._();
static const _kFontFam = 'ACE_ICON';
static const String _kFontPkg = null;
static const IconData emo_happy = IconData(0xe800, fontFamily: _kFontFam, fontPackage: _kFontPkg);
static const IconData emo_wink = IconData(0xe801, fontFamily: _kFontFam, fontPackage: _kFontPkg);
static const IconData emo_unhappy = IconData(0xe802, fontFamily: _kFontFam, fontPackage: _kFontPkg);
static const IconData emo_sleep = IconData(0xe803, fontFamily: _kFontFam, fontPackage: _kFontPkg);
static const IconData emo_thumbsup = IconData(0xe804, fontFamily: _kFontFam, fontPackage: _kFontPkg);
static const IconData emo_devil = IconData(0xe805, fontFamily: _kFontFam, fontPackage: _kFontPkg);
static const IconData emo_surprised = IconData(0xe806, fontFamily: _kFontFam, fontPackage: _kFontPkg);
static const IconData emo_tongue = IconData(0xe807, fontFamily: _kFontFam, fontPackage: _kFontPkg);
static const IconData emo_coffee = IconData(0xe808, fontFamily: _kFontFam, fontPackage: _kFontPkg);
static const IconData emo_sunglasses = IconData(0xe809, fontFamily: _kFontFam, fontPackage: _kFontPkg);
static const IconData emo_displeased = IconData(0xe80a, fontFamily: _kFontFam, fontPackage: _kFontPkg);
static const IconData emo_beer = IconData(0xe80b, fontFamily: _kFontFam, fontPackage: _kFontPkg);
static const IconData emo_grin = IconData(0xe80c, fontFamily: _kFontFam, fontPackage: _kFontPkg);
static const IconData emo_angry = IconData(0xe80d, fontFamily: _kFontFam, fontPackage: _kFontPkg);
static const IconData emo_saint = IconData(0xe80e, fontFamily: _kFontFam, fontPackage: _kFontPkg);
static const IconData emo_cry = IconData(0xe80f, fontFamily: _kFontFam, fontPackage: _kFontPkg);
static const IconData emo_shoot = IconData(0xe810, fontFamily: _kFontFam, fontPackage: _kFontPkg);
static const IconData emo_squint = IconData(0xe811, fontFamily: _kFontFam, fontPackage: _kFontPkg);
static const IconData emo_laugh = IconData(0xe812, fontFamily: _kFontFam, fontPackage: _kFontPkg);
static const IconData emo_wink2 = IconData(0xe813, fontFamily: _kFontFam, fontPackage: _kFontPkg);
static const IconData book_story = IconData(0xe814, fontFamily: _kFontFam, fontPackage: _kFontPkg);
static const IconData book_type = IconData(0xe815, fontFamily: _kFontFam, fontPackage: _kFontPkg);
static const IconData book_user = IconData(0xe816, fontFamily: _kFontFam, fontPackage: _kFontPkg);
static const IconData crown = IconData(0xe844, fontFamily: _kFontFam, fontPackage: _kFontPkg);
static const IconData crown_plus = IconData(0xe845, fontFamily: _kFontFam, fontPackage: _kFontPkg);
static const IconData crown_minus = IconData(0xe846, fontFamily: _kFontFam, fontPackage: _kFontPkg);
}
3. 图标库 IconData
集成之后,使用方式与默认的 Icon 一致,直接获取 IconData 即可,对于 Icon 的颜色尺寸等其他绘制也不会有影响;
List aceIconData = [
ACE_ICON.book_story, ACE_ICON.book_type, ACE_ICON.book_user, ACE_ICON.crown,
ACE_ICON.crown_minus, ACE_ICON.crown_plus, ACE_ICON.emo_angry, ACE_ICON.emo_beer,
ACE_ICON.emo_coffee, ACE_ICON.emo_cry, ACE_ICON.emo_devil, ACE_ICON.emo_displeased,
ACE_ICON.emo_grin, ACE_ICON.emo_happy, ACE_ICON.emo_laugh, ACE_ICON.emo_saint,
ACE_ICON.emo_shoot, ACE_ICON.emo_sleep, ACE_ICON.emo_squint, ACE_ICON.emo_sunglasses,
ACE_ICON.emo_surprised, ACE_ICON.emo_thumbsup, ACE_ICON.emo_tongue,
ACE_ICON.emo_unhappy, ACE_ICON.emo_wink, ACE_ICON.emo_wink2
];
return SliverPadding(
padding: const EdgeInsets.all(8.0),
sliver: SliverGrid(
gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
crossAxisCount: 6, mainAxisSpacing: 8.0, crossAxisSpacing: 8.0, childAspectRatio: 1.0),
delegate: SliverChildBuilderDelegate(
(context, index) => Container(
decoration: BoxDecoration(color: Colors.grey.withOpacity(0.2), borderRadius: BorderRadius.circular(3.0)),
child: Center(child: Icon(dataList[index], color: Colors.blue.withOpacity(0.6)))),
childCount: dataList.length)));
4. 异常注意
小菜在测试过程中,会显示如下图所示,Icon 资源展示不出来,其原因是小菜在 pubspec.yaml 文件声明时换行空格导致的,注意在引入所有资源库文件时,都应严格遵守 pubspec.yaml 声明格式;
小菜此次仅是对于日常资源库和命令行的简单应用,熟能生巧,仅限于日常积累;如有错误,请多多指导!
来源: 阿策小和尚