Sub topic 15
Styling icons with IconTheme
Assume you know basic Dart syntax, Flutter widgets, and the build method. You should also know that an Icon displays a glyph from an icon font, such as a Material icon.
Why use IconTheme?
An IconTheme provides default visual properties to all Icon widgets below it in the widget tree. This lets you style a group of icons in one place instead of repeating color and size on every icon.
The theme flows from the parent to its descendants:
If an Icon has its own color or size, that local value takes precedence over the inherited value.
Applying a shared icon style
Pass an IconThemeData object to IconTheme. The child and its descendants receive the values.
import 'package:flutter/material.dart';
void main() {
runApp(const IconThemeExample());
}
class IconThemeExample extends StatelessWidget {
const IconThemeExample({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(title: const Text('Icon theme')),
body: const Center(
child: IconTheme(
data: IconThemeData(
color: Colors.indigo,
size: 32,
),
child: Row(
mainAxisSize: MainAxisSize.min,
children: [
Icon(Icons.home),
SizedBox(width: 24),
Icon(Icons.search),
SizedBox(width: 24),
Icon(
Icons.favorite,
color: Colors.red,
),
],
),
),
),
),
);
}
}The first two icons use the inherited indigo color and size. The heart keeps the inherited size but overrides the color with red.
IconTheme affects icons in its subtree, so place it around the smallest group that needs the style. A theme around an entire screen can unintentionally change icons in buttons, menus, or other components.
IconTheme and the app theme
You can set a default icon style for the whole application with ThemeData.iconTheme:
MaterialApp(
theme: ThemeData(
iconTheme: const IconThemeData(
color: Colors.blueGrey,
size: 24,
),
),
home: const HomePage(),
)Use ThemeData.iconTheme for an application-wide default. Use a local IconTheme when one section, card, or toolbar needs a different appearance.
Some Material widgets, such as buttons, may provide their own icon styling. Check the widget's documented behavior when an inherited value does not produce the result you expect.
Common errors
- Placing
IconThemebeside, rather than above, the icons it should style. - Assuming it changes arbitrary widgets that happen to contain icons.
- Setting a local icon color and then expecting the parent theme to change it.
- Using a very large icon size without checking layout constraints.
- Relying on color alone to communicate meaning.
Icons should also have a semantic label when their meaning is not already clear from nearby text:
const Icon(
Icons.favorite,
semanticLabel: 'Favorite',
)For decorative icons, consider excluding them from semantics when an adjacent label already provides the same information.
Exercise
What color and size will these icons use: IconThemeData(color: Colors.green, size: 24), followed by Icon(Icons.home, size: 40) and Icon(Icons.search)?
Show answer
The home icon uses green and size 40. The search icon uses green and size 24 because it does not override either inherited property.