Sub topic 5
Icon buttons and tooltips
This chapter covers IconButton, its variants, and the tooltip property. It assumes you can build a widget tree with StatelessWidget and StatefulWidget, use setState, and read basic Dart closures. It also assumes you know the Material button widgets (Elevated, Filled, Outlined, Text) and that a null onPressed disables a button.
What an icon button is
An icon button is a button whose content is an icon and no text label. Toolbars, app bars, list rows, and text fields use them because they take little space. The cost is that an icon alone can be ambiguous to sighted users. It is also invisible to screen readers unless you give it a text description.
Flutter's IconButton is the main widget for this. It follows the same rules as the other Material buttons:
- It reports a tap through
onPressed. - It is disabled when
onPressedisnull. - It paints a ripple on the nearest
Materialancestor, which aScaffoldorCardnormally provides. - It can be styled with a
ButtonStyle.
A minimal example:
import 'package:flutter/material.dart';
class FavoriteButton extends StatefulWidget {
const FavoriteButton({super.key});
@override
State<FavoriteButton> createState() => _FavoriteButtonState();
}
class _FavoriteButtonState extends State<FavoriteButton> {
bool _isFavorite = false;
@override
Widget build(BuildContext context) {
return IconButton(
tooltip: _isFavorite ? 'Remove from favorites' : 'Add to favorites',
icon: Icon(_isFavorite ? Icons.favorite : Icons.favorite_border),
onPressed: () => setState(() => _isFavorite = !_isFavorite),
);
}
}The icon and the tooltip both change with state, so the visual and the spoken description stay in agreement.
The tooltip property
The tooltip property takes a String. IconButton wraps itself in a Tooltip widget when it is set. That one string does two jobs:
- On a pointer device, hovering shows the text in a small overlay. On a touch device, a long press shows it.
- Screen readers such as TalkBack and VoiceOver announce the text as the button's label.
The second job is why you should treat tooltip as required for icon-only buttons, even when the icon seems obvious.
IconButton(
tooltip: 'Add to favorites',
icon: const Icon(Icons.favorite_border),
onPressed: () {},
)Tooltip text guidelines
- Describe the action, not the icon. Write "Delete message", not "Trash can".
- Keep it short, usually two or three words.
- Use sentence case and no trailing period.
- Update it when the action changes, as in the toggle example above.
Tooltips on other widgets
Tooltip is a general widget, so you can wrap anything with it. Use it when the child is not an IconButton, for example a custom tappable widget.
Tooltip(
message: 'Open settings',
child: InkWell(
onTap: () {},
child: const Padding(
padding: EdgeInsets.all(12),
child: Icon(Icons.settings),
),
),
)Because InkWell paints on a Material ancestor, this snippet needs a Material above it, such as the body of a Scaffold.
Disabled icon buttons
The disabled rule from the previous chapters applies unchanged. Pass null and the button greys out and ignores taps.
IconButton(
tooltip: 'Send',
icon: const Icon(Icons.send),
onPressed: _message.isEmpty ? null : _send,
)A disabled IconButton still has its tooltip in the widget tree. On some platforms it is still shown on hover. Screen readers announce the button as disabled.
Icon button variants
Material 3 defines four visual variants of icon button. Flutter provides a named constructor for each.
| Constructor | Appearance | Typical use |
|---|---|---|
| `IconButton(...)` | Icon only, no background | Toolbars, app bars |
| `IconButton.filled(...)` | Solid background in the primary color | The main icon action in a region |
| `IconButton.filledTonal(...)` | Softer, tinted background | Secondary but still prominent actions |
| `IconButton.outlined(...)` | Transparent with a border | Lower emphasis, still clearly a button |
All four take the same core parameters, so you can swap one for another without rewriting the rest.
Row(
mainAxisSize: MainAxisSize.min,
children: [
IconButton(
tooltip: 'Share',
icon: const Icon(Icons.share),
onPressed: () {},
),
IconButton.filled(
tooltip: 'Play',
icon: const Icon(Icons.play_arrow),
onPressed: () {},
),
IconButton.filledTonal(
tooltip: 'Bookmark',
icon: const Icon(Icons.bookmark_border),
onPressed: () {},
),
IconButton.outlined(
tooltip: 'Download',
icon: const Icon(Icons.download),
onPressed: () {},
),
],
)Toggle icon buttons
An icon button can hold a selected state itself, so you do not have to swap the icon by hand. Set isSelected and supply selectedIcon. Flutter shows icon when isSelected is false and selectedIcon when it is true.
import 'package:flutter/material.dart';
class BookmarkToggle extends StatefulWidget {
const BookmarkToggle({super.key});
@override
State<BookmarkToggle> createState() => _BookmarkToggleState();
}
class _BookmarkToggleState extends State<BookmarkToggle> {
bool _saved = false;
@override
Widget build(BuildContext context) {
return IconButton(
tooltip: _saved ? 'Remove bookmark' : 'Bookmark this page',
isSelected: _saved,
icon: const Icon(Icons.bookmark_border),
selectedIcon: const Icon(Icons.bookmark),
onPressed: () => setState(() => _saved = !_saved),
);
}
}Two points to remember:
isSelectedis only a display flag. The button does not flip it for you, soonPressedmust still callsetState.- With the filled and tonal variants, the selected state also changes the background color, which gives a clear on/off appearance.
Size, padding, and the touch target
An IconButton is larger than its icon. By default the icon is 24 logical pixels, and the button adds padding around it. The result meets the Material minimum touch target of 48 by 48 logical pixels.
You can adjust the pieces separately:
iconSizechanges the icon itself.paddingchanges the space between the icon and the button's edge.visualDensitymakes the button more compact or more comfortable.constraintssets minimum or maximum sizes for the button.
IconButton(
tooltip: 'Close',
iconSize: 32,
padding: const EdgeInsets.all(12),
icon: const Icon(Icons.close),
onPressed: () {},
)Shrinking the target by accident
It is easy to make a button visually tidy and too small to hit. Two common ways:
- Setting
constraints: const BoxConstraints()removes the minimum size, so the hit area shrinks to the icon plus padding. - Setting
padding: EdgeInsets.zerotogether with a smalliconSizedoes the same.
Compact buttons are sometimes justified in dense layouts, but keep the target near 48 by 48 where you can. The accessibility chapter returns to this in detail. A related option is materialTapTargetSize, which controls whether the framework expands a button's tappable area to the minimum size. Shrinking it trades accessibility for density, so make that choice on purpose.
Styling an icon button
You style an IconButton with the same ButtonStyle machinery as the other buttons. The styleFrom helper exists here as well.
IconButton(
tooltip: 'Delete',
icon: const Icon(Icons.delete),
style: IconButton.styleFrom(
foregroundColor: Colors.white,
backgroundColor: Colors.red.shade700,
disabledBackgroundColor: Colors.grey.shade300,
),
onPressed: () {},
)For state-dependent colors, use WidgetStateProperty in a ButtonStyle, exactly as for a FilledButton. For simple cases, the color and highlightColor parameters on IconButton still exist, but style is the preferred route because it covers every state in one place.
Where icon buttons appear in common widgets
Many widgets take icon buttons as slots, and the tooltip rule carries over:
AppBar.actionstakes a list of widgets, usuallyIconButtons.AppBar.leadingholds a back or menu button.ListTile.trailingoften holds a "more options" button.InputDecoration.suffixIconcan hold a clear or show-password button.
AppBar(
title: const Text('Inbox'),
actions: [
IconButton(
tooltip: 'Search',
icon: const Icon(Icons.search),
onPressed: () {},
),
IconButton(
tooltip: 'Settings',
icon: const Icon(Icons.settings),
onPressed: () {},
),
],
)Common errors
- No tooltip on an icon-only button. Sighted users guess the meaning, and screen reader users hear only "button". Add
tooltip. - A tooltip that names the icon. "Heart" does not tell anyone what happens on press. Name the action.
- A stale tooltip. If the icon changes with state, the tooltip must change with it.
- Forgetting
setStatewithisSelected. The flag changes only when your state changes. - Removing the constraints and padding. The button becomes hard to tap on a phone.
- Using a bare
Iconwith aGestureDetector. You lose the ripple, the disabled handling, the focus behavior, and the tooltip semantics. PreferIconButton. - Using
IconButtonoutside aMaterialancestor. The ripple has nowhere to paint, and the framework can report a "No Material widget found" error. Put it inside aScaffold,Card, orMaterial.
Testing an icon button
Because the tooltip is part of the widget tree, tests can find the button by it and then tap it.
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
void main() {
testWidgets('favorite button toggles its tooltip', (tester) async {
await tester.pumpWidget(
const MaterialApp(home: Scaffold(body: FavoriteButton())),
);
expect(find.byTooltip('Add to favorites'), findsOneWidget);
await tester.tap(find.byTooltip('Add to favorites'));
await tester.pump();
expect(find.byTooltip('Remove from favorites'), findsOneWidget);
});
}This test assumes the FavoriteButton class from earlier is imported into the test file.
Exercise
An app has an IconButton with Icons.delete and no tooltip. Give two problems this causes and the one-line change that fixes both.
Show answer
Sighted pointer and touch users get no text hint about what the button does, and screen reader users hear only "button" because there is no semantic label. Add a tooltip such as tooltip: 'Delete message'. It supplies both the hover or long-press text and the spoken label.
Exercise
You build a bookmark toggle with isSelected: _saved, icon, and selectedIcon, but tapping the button never changes the icon. What is most likely missing?
Show answer
The onPressed callback does not update _saved inside setState. isSelected is only a display flag, so the button will not change it. Use onPressed: () => setState(() => _saved = !_saved).
Exercise
Which IconButton constructor would you pick for a lower-emphasis icon action that still needs a visible border, and what do you pass to make it unavailable?
Show answer
Use IconButton.outlined(...). To make it unavailable, pass null as onPressed, which disables it and gives it the disabled appearance.