An IconButton lets users tap on an icon to do something. It's like a button but shows a picture instead of text.
IconButton in Flutter
IconButton(
icon: Icon(Icons.example_icon),
onPressed: () {
// action to do when tapped
},
tooltip: 'Description',
color: Colors.colorName,
iconSize: 24.0,
)The icon property is required and shows the icon image.
The onPressed property is a function that runs when the button is tapped. If it is null, the button is disabled.
IconButton(
icon: Icon(Icons.favorite),
onPressed: () {
print('Favorite tapped');
},
)IconButton( icon: Icon(Icons.delete), color: Colors.red, iconSize: 30.0, onPressed: () { print('Delete tapped'); }, tooltip: 'Delete item', )
This app shows a blue thumb-up icon button in the center. When you tap it, it prints a message in the debug console.
import 'package:flutter/material.dart'; void main() { runApp(const MyApp()); } class MyApp extends StatelessWidget { const MyApp({super.key}); @override Widget build(BuildContext context) { return MaterialApp( home: Scaffold( appBar: AppBar(title: const Text('IconButton Example')), body: Center( child: IconButton( icon: const Icon(Icons.thumb_up), color: Colors.blue, iconSize: 40.0, tooltip: 'Like', onPressed: () { debugPrint('Thumb up pressed'); }, ), ), ), ); } }
Always provide a tooltip for accessibility so screen readers can describe the button.
If onPressed is null, the button looks disabled and cannot be tapped.
You can customize the icon size and color to fit your app's style.
IconButton is a button that shows an icon instead of text.
Use icon to set the icon and onPressed for the tap action.
Adding a tooltip helps users understand the button's purpose.
Practice
IconButton widget primarily display in a Flutter app?Solution
Step 1: Understand IconButton purpose
The IconButton widget is designed to show an icon that users can tap like a button.Step 2: Compare with other widgets
Unlike TextButton or ElevatedButton, IconButton shows an icon, not text or colored background.Final Answer:
An icon that acts as a button -> Option BQuick Check:
IconButton shows icon = A [OK]
- Thinking IconButton shows text
- Confusing IconButton with ElevatedButton
- Assuming IconButton has a background color
Solution
Step 1: Identify correct callback property
The IconButton usesonPressedfor tap actions, notonTaporonClick.Step 2: Check syntax for callback function
The callback must be a function, so() { print('Clicked'); }is correct. Passingprint('Clicked')directly calls it immediately, which is wrong.Final Answer:
IconButton(icon: Icon(Icons.add), onPressed: () { print('Clicked'); }) -> Option AQuick Check:
Use onPressed with a function callback [OK]
- Using onTap or onClick instead of onPressed
- Calling print directly instead of wrapping in a function
- Missing parentheses for Icon constructor
IconButton( icon: Icon(Icons.volume_up), onPressed: null, )
Solution
Step 1: Understand onPressed null behavior
SettingonPressedto null disables the IconButton, making it unclickable.Step 2: Check for other effects
No print or tooltip is defined, so no output or tooltip appears. No error occurs because null disables the button safely.Final Answer:
The button is disabled and does not respond to taps -> Option AQuick Check:
onPressed null disables button [OK]
- Expecting onPressed null to print or show tooltip
- Thinking button triggers error when disabled
- Confusing onPressed null with empty function
IconButton(
icon: Icon(Icons.search),
onPressed: print('Search pressed'),
)Solution
Step 1: Analyze onPressed assignment
The code callsprint('Search pressed')immediately and assigns its result (void) to onPressed, which is wrong.Step 2: Correct onPressed usage
onPressed must be a function, so it should be() { print('Search pressed'); }or() => print('Search pressed').Final Answer:
onPressed should be a function, not a function call -> Option CQuick Check:
onPressed needs a function, not direct call [OK]
- Calling print directly instead of wrapping in a function
- Forgetting parentheses on Icon constructor
- Thinking tooltip is mandatory
Solution
Step 1: Use correct tooltip property
The property to show a tooltip on IconButton istooltip, nothintorlabel.Step 2: Use correct tap callback
The tap callback must be assigned toonPressedas a function, notonTapor a direct call.Step 3: Verify syntax correctness
IconButton(icon: Icon(Icons.add), tooltip: 'Add item', onPressed: () => print('Added')) usestooltipandonPressedwith a function, so it is correct.Final Answer:
IconButton(icon: Icon(Icons.add), tooltip: 'Add item', onPressed: () => print('Added')) -> Option DQuick Check:
tooltip + onPressed function = A [OK]
- Using hint or label instead of tooltip
- Using onTap instead of onPressed
- Calling print directly instead of wrapping in function
Ready to prove it?
Pass the test and this pattern moves unknown
