Sub topic 15
Styling icons with IconTheme
An Icon widget draws one glyph from an icon font. Setting color and size on every icon works for a handful of icons, but it breaks down in a toolbar, a list, or a whole app. IconTheme lets you set those values once for a subtree. This chapter covers how icons find their style, how to override it at each level, and the mistakes that usually cause "my icon ignores my theme" bugs.
This chapter assumes you can write a StatelessWidget, you know Icon(Icons.home) and Theme.of(context), and you know that widgets inherit data from ancestors through BuildContext.
How an icon decides how it looks
IconTheme is an inherited widget. It holds an IconThemeData object, and any Icon below it reads that object during build. The Icon widget does this:
- It checks its own constructor arguments (
size,color,fill,weight, and so on). - For any argument that is
null, it looks up the nearestIconThemeabove it. - Material widgets such as
MaterialAppandScaffoldinsert anIconThemebuilt fromThemeData.iconTheme, so there is always one to find. - Anything still unset falls back to framework defaults. The default size is 24 logical pixels.
The lookup is per property. An icon can take its size from a local IconTheme and its color from ThemeData, because each property is resolved separately.
Setting a default for a subtree
The simplest use wraps a group of icons in an IconTheme. Here is a complete, runnable version of a small toolbar:
import 'package:flutter/material.dart';
void main() => runApp(const MaterialApp(home: Scaffold(body: Toolbar())));
class Toolbar extends StatelessWidget {
const Toolbar({super.key});
@override
Widget build(BuildContext context) {
return const IconTheme(
data: IconThemeData(color: Colors.indigo, size: 28),
child: Row(
children: [
Icon(Icons.home),
Icon(Icons.search),
Icon(Icons.favorite, color: Colors.red),
],
),
);
}
}The first two icons are indigo and 28 pixels. The third sets color: Colors.red itself, so it is red, but it still takes the size 28 from the theme. An explicit argument always wins, and only for that property.
IconTheme.merge versus a plain IconTheme
A plain IconTheme(data: ...) replaces the inherited data for its subtree. Any property you leave null in your new IconThemeData is then null for everything below, and icons resolve it from defaults rather than from the app theme. This often surprises people.
IconTheme.merge combines your data with the ancestor's data. Values you set override. Values you leave out are inherited.
import 'package:flutter/material.dart';
class PromoBanner extends StatelessWidget {
const PromoBanner({super.key});
@override
Widget build(BuildContext context) {
return IconTheme.merge(
data: const IconThemeData(size: 40),
child: const Row(
children: [
Icon(Icons.local_offer),
Icon(Icons.star),
],
),
);
}
}Here the icons are 40 pixels, and their color still comes from the app's ThemeData.iconTheme. If you used plain IconTheme with only size: 40, the color would no longer come from the app theme. Prefer merge when you want to change one or two properties.
Setting icon style app-wide
Put an iconTheme in ThemeData to style every icon in the app:
import 'package:flutter/material.dart';
void main() {
runApp(
MaterialApp(
theme: ThemeData(
colorSchemeSeed: Colors.teal,
iconTheme: const IconThemeData(size: 26, color: Colors.teal),
),
home: const Scaffold(
body: Center(child: Icon(Icons.settings)),
),
),
);
}IconThemeData has more than color and size:
| Property | Effect |
|---|---|
| `color` | Fill color of the glyph |
| `size` | Width and height in logical pixels |
| `opacity` | Multiplies the color's alpha, from 0.0 to 1.0 |
| `shadows` | A list of `Shadow` objects drawn behind the glyph |
| `fill`, `weight`, `grade`, `opticalSize` | Axes for variable icon fonts such as Material Symbols. Fonts without those axes ignore them. |
| `applyTextScaling` | If true, the icon size grows with the user's text scale setting |
The applyTextScaling property is relevant to accessibility. By default icons do not scale with the user's font size. Setting it to true in the theme keeps icons proportional to text for users who enlarge their fonts.
Icons that style themselves
Several Material widgets provide their own IconTheme or their own icon properties. When an icon inside one of them looks wrong, the cause is usually one of these:
AppBarhasiconThemefor the leading and title-area icons andactionsIconThemefor the action buttons. IfactionsIconThemeis null, the actions useiconTheme.IconButtontakesiconSizeandcolor. These apply to the icon it wraps, so you do not need to style the innerIcon.ListTilehasiconColor, which colors its leading and trailing icons.ThemeData.primaryIconThemestyles icons drawn on the primary color in older-style widgets.
import 'package:flutter/material.dart';
class ThemedBar extends StatelessWidget implements PreferredSizeWidget {
const ThemedBar({super.key});
@override
Size get preferredSize => const Size.fromHeight(kToolbarHeight);
@override
Widget build(BuildContext context) {
return AppBar(
title: const Text('Inbox'),
iconTheme: const IconThemeData(color: Colors.white),
actionsIconTheme: const IconThemeData(color: Colors.amber, size: 30),
backgroundColor: Colors.indigo,
actions: [
IconButton(icon: const Icon(Icons.search), onPressed: () {}),
IconButton(icon: const Icon(Icons.more_vert), onPressed: () {}),
],
);
}
}Common errors
- The theme has no effect on one icon. That icon sets
colororsizeitself. Remove the argument or accept the override. - The color resets after wrapping in
IconTheme. You used plainIconThemeand leftcolornull. Switch toIconTheme.merge. - Using
IconTheme.of(context)in the samebuildthat creates theIconTheme. Thecontextthere is above the new theme, so you read the old one. Wrap the reading widget in aBuilder, or move it into its own widget. - An
IconButtonicon ignores the surroundingIconTheme.IconButtonapplies its owniconSizeandcolor, which take priority. Set them on the button or throughIconButtonTheme. - Icons do not match text at large font sizes. Icons ignore text scaling unless
applyTextScalingis true.
Choosing a level
| Situation | Best place to set the style |
|---|---|
| Every icon in the app should share a size or color | `ThemeData.iconTheme` |
| One section needs a different look | `IconTheme.merge` around that section |
| App bar icons differ from the body | `AppBar.iconTheme` and `actionsIconTheme` |
| A single icon is an exception | Arguments on that `Icon` |
Keep as much styling as possible at the top and use local overrides sparingly. This keeps the app consistent, and a design change then needs one edit.
Exercise
A widget wraps a Row in IconTheme(data: IconThemeData(size: 40), child: ...), and the Row's icons turn from the app's teal to a default dark color. Why does this happen, and how do you keep the teal color while changing only the size?
Show answer
A plain IconTheme replaces the inherited IconThemeData for its subtree. The color is null in the new data, so the icons no longer pick up the teal from ThemeData.iconTheme. Use IconTheme.merge(data: IconThemeData(size: 40), child: ...) instead. It overrides size and inherits color from the ancestor.
Exercise
An IconTheme sets color: Colors.indigo, size: 28. It contains Icon(Icons.favorite, color: Colors.red). What color and size does the icon have?
Show answer
The icon is red and 28 pixels. The explicit color argument overrides the theme for that property, and the null size argument falls back to the theme's 28.