新建项目-3.png

阿里图库

简介

Flutter 中,可以像 web 开发一样使用 iconfont,iconfont 即“字体图标”,它是将图标做成字体文件,然后通过指定不同的字符而显示不同的图片。用于显示 iconfont 的就是 Icon Widget。

iconfont 和图片相比有如下优势:

  1. 体积小:可以减小安装包大小。
  2. 矢量的:iconfont 都是矢量图标,放大不会影响其清晰度。
  3. 可以应用文本样式:可以像文本一样改变字体图标的颜色、大小对齐等。
  4. 可以通过 TextSpan 和文本混用。

实例

1. Icon

  1. class Icon extends StatelessWidget {
  2. /// Creates an icon.
  3. ///
  4. /// The [size] and [color] default to the value given by the current [IconTheme].
  5. const Icon(
  6. this.icon, {
  7. Key key,
  8. this.size, //大小
  9. this.color, //颜色
  10. this.semanticLabel, //语义标签
  11. this.textDirection, //icon文字方向
  12. }) : super(key: key);

列子:

  1. Container(
  2. width: 300,
  3. height: 300,
  4. color: Colors.lightGreenAccent,
  5. child: Icon(
  6. Icons.favorite,
  7. color: Colors.orange,
  8. size: 200,
  9. ),
  10. ),

image.png

2. IconData

  1. class IconData {
  2. const IconData(
  3. this.codePoint, { //该图标在字体中的编码
  4. this.fontFamily, //所属字体
  5. this.fontPackage, //字体所属的包
  6. this.matchTextDirection = false, //是否启用镜像,向左还是向右
  7. });

列子:

  1. Container(
  2. width: 100,
  3. height: 100,
  4. color: Colors.lightGreenAccent,
  5. child: Icon(
  6. IconData(
  7. 0xe914,
  8. fontFamily: 'MaterialIcons',
  9. matchTextDirection: true
  10. ),
  11. color: Colors.red,
  12. size: 30,
  13. textDirection: TextDirection.rtl,
  14. ),
  15. ),
  16. Container(
  17. width: 100,
  18. height: 100,
  19. color: Colors.blueGrey,
  20. child: Icon(
  21. IconData(
  22. 0xe914,
  23. fontFamily: 'MaterialIcons',
  24. matchTextDirection: true
  25. ),
  26. color: Colors.red,
  27. size: 30,
  28. textDirection: TextDirection.ltr,
  29. ),
  30. ),

image.png

3. ImageIcon

  1. class ImageIcon extends StatelessWidget {
  2. const ImageIcon(
  3. this.image, { //ImageProvider类型 用于加载具体的图片
  4. Key key,
  5. this.size, //大小
  6. this.color, //想要显示的颜色
  7. this.semanticLabel,
  8. }) : super(key: key);

ImageProvider是一个抽象类,其子类包括

  • AssetImage 资源图片
  • FileImage 文件图片
  • NetworkImage 网络图片
  • MemoryImage 内存图片

加载如Image一样

  1. ImageIcon(
  2. AssetImage("image/flutter1.png"),
  3. size: 50,//默认黑色
  4. ),
  5. ImageIcon(
  6. AssetImage("image/flutter1.png"),
  7. size: 50,
  8. color: Colors.orangeAccent,
  9. ),
  10. ImageIcon(
  11. AssetImage("image/flutter1.png"),
  12. size: 50,
  13. color: Colors.blue,
  14. ),

image.png

4. IconButton

  1. const IconButton({
  2. Key key,
  3. this.iconSize = 24.0, //默认大小
  4. this.padding = const EdgeInsets.all(8.0), //内间距
  5. this.alignment = Alignment.center, //对齐方式
  6. @required this.icon,
  7. this.color,
  8. this.focusColor,
  9. this.hoverColor,
  10. this.highlightColor, //长按后不松手时显示的颜色
  11. this.splashColor, //点击一下时闪烁的颜色
  12. this.disabledColor, //不可用时的颜色
  13. @required this.onPressed, //点击后触发的方法
  14. this.focusNode,
  15. this.autofocus = false,
  16. this.tooltip, //长按后的提示语
  17. this.enableFeedback = true,
  18. }) : assert(iconSize != null),
  19. assert(padding != null),
  20. assert(alignment != null),
  21. assert(autofocus != null),
  22. assert(icon != null),
  23. super(key: key);

例子:

  1. IconButton(
  2. iconSize: 50,
  3. icon: Icon(
  4. Icons.android,
  5. color: Colors.deepPurple,
  6. ),
  7. onPressed: () {
  8. print(234);
  9. },
  10. highlightColor: Colors.green,//长按后不松手时显示的颜色
  11. splashColor: Colors.blue,//点击一下时闪烁的颜色
  12. disabledColor: Colors.grey,//不可用时的颜色
  13. tooltip: "copy",//长按后的提示语
  14. ),

我发现这个有的时候在iOS 模拟器中不好使,先留着,以后再研究下~