Sub topic 14
Showing icons with the `Icon` widget
This chapter assumes you know basic Dart syntax, Flutter widgets, StatelessWidget, and the build method. It also assumes you have a Flutter project that runs on an emulator, device, or the web.
An icon is a small visual symbol, such as a heart, search magnifier, or home outline. Flutter displays an icon with the Icon widget. The widget receives an IconData value, which describes the glyph to draw. The built-in Icons class provides many Material Design icons.
Using a Material icon
The simplest icon needs only an icon value:
const Icon(Icons.favorite)You can set its color, size, and accessibility label directly:
import 'package:flutter/material.dart';
void main() {
runApp(const IconExampleApp());
}
class IconExampleApp extends StatelessWidget {
const IconExampleApp({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(title: const Text('Icons')),
body: const Center(
child: Icon(
Icons.favorite,
color: Colors.red,
size: 48,
semanticLabel: 'Favorite',
),
),
),
);
}
}The size value is measured in logical pixels. It controls the icon glyph's nominal height and width. The parent still controls the available space, so an icon can be clipped or constrained by its layout.
color paints the glyph. If you omit it, the icon can inherit a color from the surrounding IconTheme or another widget theme. The semanticLabel gives the icon a spoken description for screen readers.
Choosing and composing icons
Material icons are organized as static values on Icons, such as Icons.home, Icons.search, and Icons.settings. Use an icon whose meaning matches the action or status it represents.
An icon can appear beside text in a row:
const Row(
mainAxisSize: MainAxisSize.min,
children: [
Icon(Icons.download),
SizedBox(width: 8),
Text('Download'),
],
)mainAxisSize: MainAxisSize.min prevents the row from expanding to the full available width. The SizedBox adds a fixed gap between the glyph and its label.
The icon itself does not perform an action. Put it inside an interactive widget such as IconButton when the user should tap it:
IconButton(
onPressed: () {
// Start the download.
},
icon: const Icon(Icons.download),
tooltip: 'Download',
)IconButton supplies interaction behavior and uses tooltip as an accessible description. Do not make a decorative icon announce information that nearby text already communicates.
How an icon is laid out
Common errors
- Using a text character instead of a standard icon can produce different shapes across platforms.
- Choosing a decorative icon with no label is usually correct when adjacent text already explains it.
- Placing an
Iconin a tightly constrained parent can clip it. - Using an icon with a misleading meaning creates an accessibility and usability problem.
Exercise
Write an icon for a 32-pixel blue search symbol that announces “Search” to a screen reader.
Show answer
const Icon(Icons.search, color: Colors.blue, size: 32, semanticLabel: 'Search')