What is the Icon Widget?
The Icon widget in Flutter displays a graphical icon from the Material Design icon set or a custom image. It is lightweight and can be used anywhere you need a visual symbol – in buttons, list tiles, app bars, or as standalone elements. The most common source of icons is the Icons class, which contains hundreds of pre‑defined Material icons. You can also use custom icons from assets, font icons, or SVG.
Basic Usage
This will display the star icon with the default size (24 logical pixels) and the default color (usually black or the current theme's color).
Customizing Size and Color
You can adjust the icon's size and color using the size and color properties. These override the default theme values.
The Icons Class
The Icons class provides static constants for all Material Design icons. For a complete list, check the Flutter Icons documentation. Common icons include: Icons.home, Icons.search, Icons.person, Icons.settings, Icons.arrow_back, Icons.more_vert, etc.
Using Icon with Theme
If you don't specify a color, the Icon widget inherits its color from the nearest IconTheme or the default ThemeData's icon color. For example, inside an AppBar, icons are typically white, while in the body they may be black or grey.
Semantics and Accessibility
For accessibility, you should provide a semantic label (text alternative) for icons. Use the semanticLabel property. This is read by screen readers (e.g., TalkBack, VoiceOver).
Custom Icons from Assets
To use a custom icon from your assets (e.g., a PNG), you can use ImageIcon. It accepts an ImageProvider and behaves like an Icon, respecting size and color.
Remember to declare the asset in pubspec.yaml.
Using IconButton
The IconButton widget combines an icon with a touch target, providing a splash effect when tapped. It's ideal for actions like navigation, like, delete, etc.
Icon Themes with IconThemeData
You can globally define the default icon size and color using IconTheme or set it in ThemeData. For instance:
Common Mistakes
- Forgetting to provide a semantic label: Icons without text may be confusing for users with visual impairments. Always set
semanticLabel.
- Forgetting to provide a semantic label: Icons without text may be confusing for users with visual impairments. Always set
- Using
IconinsideIconButtonbut not passingonPressed:IconButtonwill be disabled, and users may not know why it doesn't respond.
- Using
- Hard‑coding icon sizes: Use relative sizes (e.g.,
Theme.of(context).iconTheme.size) to adapt to different screen sizes.
- Hard‑coding icon sizes: Use relative sizes (e.g.,
- Using PNG icons without proper resolution: For crisp display on high‑DPI screens, provide multiple resolutions (2x, 3x) or use vector icons (font icons or SVG).
- Not handling null
onPressed: IfonPressedis null, theIconButtonis disabled; ensure you either handle it or use aGestureDetectorif you need a non‑material button.
- Not handling null
Key Takeaways
- The
Iconwidget displays Material icons from theIconsclass.
- The
- Customize size and color with
sizeandcolor.
- Customize size and color with
- Use
semanticLabelfor accessibility.
- Use
- For custom images, use
ImageIcon.
- For custom images, use
- Combine with
IconButtonfor interactive icons.
- Combine with
- Leverage icon themes for consistency.
- Prefer vector‑based icons (font icons, SVG) for scalability.