IconButton class Null safety

A Material Design icon button.

An icon button is a picture printed on a Material widget that reacts to touches by filling with color (ink).

Icon buttons are commonly used in the AppBar.actions field, but they can be used in many other places as well.

If the onPressed callback is null, then the button will be disabled and will not react to touch.

Requires one of its ancestors to be a Material widget.

The hit region of an icon button will, if possible, be at least kMinInteractiveDimension pixels in size, regardless of the actual iconSize, to satisfy the touch target size requirements in the Material Design specification. The alignment controls how the icon itself is positioned within the hit region.

This sample shows an IconButton that uses the Material icon "volume_up" to increase the volume.

To create a local project with this code sample, run:
flutter create --sample=material.IconButton.1 mysample

Icon sizes

When creating an icon button with an Icon, do not override the icon's size with its Icon.size parameter, use the icon button's iconSize parameter instead. For example do this:

IconButton(iconSize: 72, icon: Icon(Icons.favorite), ...)

Avoid doing this:

IconButton(icon: Icon(Icons.favorite, size: 72), ...)

If you do, the button's size will be based on the default icon size, not 72, which may produce unexpected layouts and clipping issues.

Adding a filled background

Icon buttons don't support specifying a background color or other background decoration because typically the icon is just displayed on top of the parent widget's background. Icon buttons that appear in AppBar.actions are an example of this.

It's easy enough to create an icon button with a filled background using the Ink widget. The Ink widget renders a decoration on the underlying Material along with the splash and highlight InkResponse contributed by descendant widgets.

In this sample the icon button's background color is defined with an Ink widget whose child is an IconButton. The icon button's filled background is a light shade of blue, it's a filled circle, and it's as big as the button is.

To create a local project with this code sample, run:
flutter create --sample=material.IconButton.2 mysample

Material Design 3 introduced new types (standard and contained) of IconButtons. The default IconButton is the standard type, and contained icon buttons can be produced by configuring the IconButton widget's properties.

Material Design 3 also treats IconButtons as toggle buttons. In order to not break existing apps, the toggle feature can be optionally controlled by the isSelected property.

If isSelected is null it will behave as a normal button. If isSelected is not null then it will behave as a toggle button. If isSelected is true then it will show selectedIcon, if it false it will show the normal icon.

This sample shows creation of IconButton widgets for standard, filled, filled tonal and outlined types, as described in: https://m3.material.io/components/icon-buttons/overview
To create a local project with this code sample, run:
flutter create --sample=material.IconButton.3 mysample

This sample shows creation of IconButton widgets with toggle feature for standard, filled, filled tonal and outlined types, as described in: https://m3.material.io/components/icon-buttons/overview
To create a local project with this code sample, run:
flutter create --sample=material.IconButton.4 mysample

See also:

Inheritance

Constructors

IconButton({Key? key, double? iconSize, VisualDensity? visualDensity, EdgeInsetsGeometry padding = const EdgeInsets.all(8.0), AlignmentGeometry alignment = Alignment.center, double? splashRadius, Color? color, Color? focusColor, Color? hoverColor, Color? highlightColor, Color? splashColor, Color? disabledColor, required VoidCallback? onPressed, MouseCursor? mouseCursor, FocusNode? focusNode, bool autofocus = false, String? tooltip, bool enableFeedback = true, BoxConstraints? constraints, ButtonStyle? style, bool? isSelected, Widget? selectedIcon, required Widget icon})
Creates an icon button.
const

Properties

alignment AlignmentGeometry
Defines how the icon is positioned within the IconButton.
final
autofocus bool
True if this widget will be selected as the initial focus when no other node in its scope is currently focused.
final
color Color?
The color to use for the icon inside the button, if the icon is enabled. Defaults to leaving this up to the icon widget.
final
constraints BoxConstraints?
Optional size constraints for the button.
final
disabledColor Color?
The color to use for the icon inside the button, if the icon is disabled. Defaults to the ThemeData.disabledColor of the current Theme.
final
enableFeedback bool
Whether detected gestures should provide acoustic and/or haptic feedback.
final
focusColor Color?
The color for the button when it has the input focus.
final
focusNode FocusNode?
An optional focus node to use as the focus node for this widget.
final
hashCode int
The hash code for this object.
nonVirtual">@nonVirtualread-onlyinherited
highlightColor Color?
The secondary color of the button when the button is in the down (pressed) state. The highlight color is represented as a solid color that is overlaid over the button color (if any). If the highlight color has transparency, the button color will show through. The highlight fades in quickly as the button is held down.
final
hoverColor Color?
The color for the button when a pointer is hovering over it.
final
icon Widget
The icon to display inside the button.
final
iconSize double?
The size of the icon inside the button.
final
isSelected bool?
The optional selection state of the icon button.
final
key Key?
Controls how one widget replaces another widget in the tree.
finalinherited
mouseCursor MouseCursor?
The cursor for a mouse pointer when it enters or is hovering over the button.
final
onPressed VoidCallback?
The callback that is called when the button is tapped or otherwise activated.
final
padding EdgeInsetsGeometry
The padding around the button's icon. The entire padded icon will react to input gestures.
final
runtimeType Type
A representation of the runtime type of the object.
read-onlyinherited
selectedIcon Widget?
The icon to display inside the button when isSelected is true. This property can be null. The original icon will be used for both selected and unselected status if it is null.
final
splashColor Color?
The primary color of the button when the button is in the down (pressed) state. The splash is represented as a circular overlay that appears above the highlightColor overlay. The splash overlay has a center point that matches the hit point of the user touch event. The splash overlay will expand to fill the button area if the touch is held for long enough time. If the splash color has transparency then the highlight and button color will show through.
final
splashRadius double?
The splash radius.
final
style ButtonStyle?
Customizes this button's appearance.
final
tooltip String?
Text that describes the action that will occur when the button is pressed.
final
visualDensity VisualDensity?
Defines how compact the icon button's layout will be.
final

Methods

build(BuildContext context) Widget
Describes the part of the user interface represented by this widget.
override
createElement() StatelessElement
Creates a StatelessElement to manage this widget's location in the tree.
inherited
debugDescribeChildren() List<DiagnosticsNode>
Returns a list of DiagnosticsNode objects describing this node's children.
protected">@protectedinherited
debugFillProperties(DiagnosticPropertiesBuilder properties) → void
Add additional properties associated with the node.
override
noSuchMethod(Invocation invocation) → dynamic
Invoked when a non-existent method or property is accessed.
inherited
toDiagnosticsNode({String? name, DiagnosticsTreeStyle? style}) DiagnosticsNode
Returns a debug representation of the object that is used by debugging tools and by DiagnosticsNode.toStringDeep.
inherited
toString({DiagnosticLevel minLevel = DiagnosticLevel.info}) String
A string representation of this object.
inherited
toStringDeep({String prefixLineOne = '', String? prefixOtherLines, DiagnosticLevel minLevel = DiagnosticLevel.debug}) String
Returns a string representation of this node and its descendants.
inherited
toStringShallow({String joiner = ', ', DiagnosticLevel minLevel = DiagnosticLevel.debug}) String
Returns a one-line detailed description of the object.
inherited
toStringShort() String
A short, textual description of this widget.
inherited

Operators

operator ==(Object other) bool
The equality operator.
nonVirtual">@nonVirtualinherited

Static Methods

styleFrom({Color? foregroundColor, Color? backgroundColor, Color? disabledForegroundColor, Color? disabledBackgroundColor, Color? focusColor, Color? hoverColor, Color? highlightColor, Color? shadowColor, Color? surfaceTintColor, double? elevation, Size? minimumSize, Size? fixedSize, Size? maximumSize, BorderSide? side, OutlinedBorder? shape, EdgeInsetsGeometry? padding, MouseCursor? enabledMouseCursor, MouseCursor? disabledMouseCursor, VisualDensity? visualDensity, MaterialTapTargetSize? tapTargetSize, Duration? animationDuration, bool? enableFeedback, AlignmentGeometry? alignment, InteractiveInkFeatureFactory? splashFactory}) ButtonStyle
A static convenience method that constructs an icon button ButtonStyle given simple values. This method is only used for Material 3.