Sub topic 5
Icon buttons and tooltips
Assume you know basic Dart functions and closures, can build Flutter widget trees, and can use setState in a StatefulWidget. This chapter focuses on compact actions represented by icons.
When to use IconButton
IconButton is a Material widget for an action that can be represented clearly by an icon. Common examples include search, refresh, favorite, delete, and opening a menu.
Use a regular button with text when the action is unfamiliar, important, or easy to misunderstand. An icon alone saves space, but it also makes the interface depend on the user recognizing the symbol.
The onPressed callback runs when the user activates the button. Like other Material buttons, IconButton is disabled when onPressed is null.
import 'package:flutter/material.dart';
void main() {
runApp(const FavoritesApp());
}
class FavoritesApp extends StatefulWidget {
const FavoritesApp({super.key});
@override
State<FavoritesApp> createState() => _FavoritesAppState();
}
class _FavoritesAppState extends State<FavoritesApp> {
bool _isFavorite = false;
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: const Text('Article'),
actions: [
IconButton(
tooltip: _isFavorite
? 'Remove from favorites'
: 'Add to favorites',
icon: Icon(
_isFavorite ? Icons.favorite : Icons.favorite_border,
),
onPressed: () {
setState(() => _isFavorite = !_isFavorite);
},
),
],
),
body: const Center(child: Text('Read this article')),
),
);
}
}The icon changes when _isFavorite changes. The tooltip changes too, so it describes the action that will happen next.
Tooltips and accessibility
A tooltip is a short text hint associated with a control. On a pointer device, Flutter usually displays it when the user hovers over the icon or presses it for a moment. On touch devices, a long press can reveal it.
The tooltip also provides useful information to accessibility services. Therefore, use a specific action phrase such as Add to favorites, not a vague phrase such as Favorite.
A tooltip should explain an icon that is not self-evident. It should not replace a visible text label when the action needs additional explanation.
Size, constraints, and styling
An icon may look small, but its interactive area should be large enough to tap comfortably. IconButton supplies default constraints and padding suited to Material interaction. Avoid reducing these constraints unless you have a strong layout reason.
You can customize the icon color, size, padding, and other properties directly:
IconButton(
tooltip: 'Refresh articles',
icon: const Icon(Icons.refresh),
iconSize: 28,
color: Colors.teal,
padding: const EdgeInsets.all(12),
onPressed: () {},
)When the callback is null, Flutter applies a disabled appearance and ignores activation. Keep the button in the tree when the action is temporarily unavailable, because the disabled appearance communicates that state consistently.
Choosing an icon action
| Situation | Suitable control |
|---|---|
| A compact, familiar action | `IconButton` |
| An unfamiliar or high-risk action | Text button or button with icon and text |
| Several related choices | Menu button or visible text controls |
Exercise
What should you add to an icon-only delete button, and what should happen when deletion is unavailable?
Show answer
Add a specific tooltip such as "Delete message". Pass null to onPressed while deletion is unavailable so the button is disabled and cannot be activated.