Skip to content

Icon

Icon 用于显示 Material Design 或自定义图标。

构造函数

dart
Icon(
  IconData? icon, {                                       // 图标数据(必填)
  Key? key,                                               // 组件标识
  double? size,                                           // 图标大小(默认 24)
  Color? color,                                           // 图标颜色
  String? semanticLabel,                                  // 语义标签(用于无障碍)
  TextDirection? textDirection,                            // 图标方向(影响镜像翻转)
  List<Shadow>? shadows,                                  // 图标阴影
})

属性速查

属性类型默认值说明
iconIconData?必填图标数据
sizedouble?24图标大小
colorColor?主题色图标颜色
shadowsList<Shadow>?null图标阴影
semanticLabelString?null语义标签
textDirectionTextDirection?null图标方向
filldouble?0填充比例 0~1(Material 3)

快速示例

基本用法

dart
import 'package:flutter/material.dart';

class IconBasicExample extends StatelessWidget {
  const IconBasicExample({super.key});

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: const Text('Icon 基本用法')),
      body: Padding(
        padding: const EdgeInsets.all(16),
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: const [
            // ─── ★ size + color ──────────────
            Icon(Icons.home, size: 24, color: Colors.blue),
            // ─── ☆ size + color ──────────────
            SizedBox(height: 8),
            Icon(Icons.favorite, size: 32, color: Colors.red),
            SizedBox(height: 8),
            Icon(Icons.settings, size: 40, color: Colors.grey),
          ],
        ),
      ),
    );
  }
}

常用 Material 图标速查

分类图标
导航Icons.homeIcons.searchIcons.settingsIcons.personIcons.menu
操作Icons.addIcons.removeIcons.editIcons.deleteIcons.share
反馈Icons.checkIcons.closeIcons.infoIcons.warningIcons.errorIcons.check_circle
箭头Icons.arrow_backIcons.arrow_forwardIcons.arrow_upwardIcons.arrow_downward
媒体Icons.cameraIcons.phoneIcons.emailIcons.notifications
状态Icons.visibilityIcons.visibility_offIcons.lockIcons.lock_open
文件Icons.downloadIcons.uploadIcons.refresh
收藏Icons.favoriteIcons.starIcons.star_borderIcons.bookmark
其他Icons.calendar_todayIcons.location_onIcons.cancelIcons.logoutIcons.dark_modeIcons.light_mode

完整图标列表:https://api.flutter.dev/flutter/material/Icons-class.html

IconButton — 可点击图标

构造函数

dart
IconButton({
  Key? key,                                               // 组件标识
  double? iconSize,                                       // 图标大小(默认 24)
  VisualDensity? visualDensity,                           // 视觉密度
  EdgeInsetsGeometry padding = const EdgeInsets.all(8),   // 内边距
  EdgeInsetsGeometry? alignment,                          // 对齐方式
  double? splashRadius,                                   // 水波纹半径
  Color? color,                                           // 图标颜色
  Color? focusColor,                                      // 聚焦颜色
  Color? hoverColor,                                      // 悬浮颜色
  Color? highlightColor,                                  // 高亮颜色
  Color? splashColor,                                     // 水波纹颜色
  Color? disabledColor,                                    // 禁用颜色
  required VoidCallback? onPressed,                       // 点击回调(必填,null 则禁用)
  MouseCursor? mouseCursor,                               // 鼠标指针样式
  FocusNode? focusNode,                                   // 焦点节点
  bool autofocus = false,                                 // 是否自动聚焦
  String? tooltip,                                        // 长按提示文字
  bool? enableFeedback,                                   // 是否启用反馈
  BoxConstraints? constraints,                            // 按钮约束
  Widget? icon,                                           // 图标组件(必填)
})

属性速查

属性类型默认值说明
iconWidget?必填图标组件
onPressedVoidCallback?必填点击回调(null 则禁用)
iconSizedouble?24图标大小
colorColor?主题色图标颜色
paddingEdgeInsetsGeometryall(8)按钮内边距
constraintsBoxConstraints?null按钮约束
tooltipString?null长按提示文字
splashRadiusdouble?null水波纹半径
styleButtonStyle?null按钮样式

快速示例

dart
IconButton(
  icon: const Icon(Icons.favorite),
  color: Colors.red,
  iconSize: 32,
  onPressed: () {
    print('点击了收藏');
  },
  tooltip: '收藏',
)

图标按钮变体速查

构造方式说明
IconButton(...)标准图标按钮
IconButton.filled(...)填充背景图标按钮
IconButton.outlined(...)描边图标按钮
IconButton.tonal(...)色调图标按钮
FilledIconButton(...)Material 3 填充图标按钮
dart
// 标准 IconButton
IconButton(icon: const Icon(Icons.search), onPressed: () {})

// Material 3 变体
IconButton.filled(onPressed: () {}, icon: const Icon(Icons.delete))
IconButton.outlined(onPressed: () {}, icon: const Icon(Icons.edit))
IconButton.tonal(onPressed: () {}, icon: const Icon(Icons.share))

自定义图标

使用图标字体

dart
// 使用自定义图标字体中的图标
const Icon(
  IconData(0xe800, fontFamily: 'MyIconFont'),
  size: 24,
  color: Colors.blue,
)

在 pubspec.yaml 中声明

yaml
flutter:
  fonts:
    - family: MyIconFont
      fonts:
        - asset: fonts/MyIconFont.ttf

图标与文字组合

dart
Row(
  mainAxisSize: MainAxisSize.min,
  children: const [
    Icon(Icons.star, color: Colors.amber, size: 16),
    SizedBox(width: 4),
    Text('4.5'),
  ],
)

基于 Flutter 官方文档整理