Fluttter中,可以像Web开发一样使用iconfont,iconfont即“字体图标”,它是将图标做成字体文件,然后通过指定不同的字符而显示不同的图片。Icon假定渲染的图标为正方形, 非正方形图标可能会错误显示。
在Flutter开发中,iconfont和图片相比有如下优势:
1.体积小:可以减小安装包大小。
2.矢量的:iconfont都是矢量图标,放大不会影响其清晰度。
3.可以应用文本样式:可以像文本一样改变字体图标的颜色、大小对齐等。
4.可以通过TextSpan和文本混用。
Flutter默认包含了一套Material Design的字体图标,在pubspec.yaml文件中的配置如下:
flutter:
uses-material-design: true
代码示例:
class IconDemo extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Center(
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceAround,
children: <Widget>[
Icon(Icons.favorite, color: Colors.pink, size: 24.0),
Icon(Icons.audiotrack, color: Colors.green, size: 30.0),
Icon(Icons.beach_access, color: Colors.blue, size: 36.0),
],
),
);
}
}
运行效果图如下:










网友评论