IconButton Class in Flutter with Example
Last Updated :
16 Jun, 2022
The most different button class provided in flutter is the IconButton Class. In this tutorial, we will walk you through the implementation and properties of the IconButton class for flutter in detail. An icon button allows users to take actions like searching, navigation, adding, etc, by simply pressing the button. This class does not have a regular button with some text in it but an icon in the form of a button. The icon serves as an identifier. To use an icon button you need to import the material component package for flutter i.e. "package:flutter/material.dart".
Constructor
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,
required Widget icon
}
)
Parameters
An IconButton has majorly two parameters:
1. icon: T
This represents the button's label
IconButton(
icon: Icon(Icons.code),
),
2. onPressed: This represents the action to be executed when the button is pressed.
IconButton(
onPressed: () => Navigator.of(context)
.push(MaterialPageRoute(builder: (context) => const NewScreen())),
icon: Icon(Icons.code),
),
Properties
- autofocus: gives a boolean value corresponding to the initial focus of the button.
- alignment: defines how the icon is positioned within the IconButton.
- focusNode: represents the focus node of the widget
- color: gives the color of the Icon inside the button.
- constraints: represents the optional size constraints for the button.
- disabledColor: represents the color to show when the widget is disabled.
- enableFeedback: tells whether detected gestures should provide acoustic and/or haptic feedback.
- focusColor: represents the color of the button when it is in focus.
- hashCode: gives the hash code for this object.
- highlightColor: represents the button's secondary color (optional) when it is pressed.
- hoverColor: represents the Icon color while hovering over the Icon
- iconSize: gives the icon’s size inside the button.
- key: Controls how one widget replaces another widget in the tree.
- splashColor: The color of the ripple effect produced when the user presses the button.
- splashRadius: The splash radius.
- tooltip: Text to represent the action when the button is pressed.
- visualDensity: Defines how compact the icon button’s layout will be.
- mouseCursor: The type of cursor to show when it hovers over the widget.
- padding: The padding to provide to the Icon.
- runtimeType: A representation of the runtime type of the object.
Methods
- build(BuildContext context): describes the part of the user interface represented by this widget
- createElement(): creates a StatelessElement to manage this widget's location in the tree.
- debugDescribeChildren(): returns a list of DiagnosticsNode objects describing this node's children
- debugFillProperties() : add additional properties associated with the node
- toDiagnosticsNode(): returns a debug representation of the object that is used by debugging tools.
- toString(): a string representation of this object.
- toStringDeep(): returns a string representation of this node and its descendants.
- toStringShallow(): returns a one-line detailed description of the object
- toStringShort(): a short, textual description of this widget.
Adjusting Icon Color
IconButton(
color: Colors.green,
onPressed: () {},
icon: Icon(Icons.code),
),
Output:
Adjusting the Background Color
CircleAvatar(
backgroundColor: Colors.green,
child: IconButton(
color: Colors.white,
onPressed: () {},
icon: Icon(Icons.code),
),
),
Output:
Changing SplashColor
CircleAvatar(
backgroundColor: Colors.green,
child: IconButton(
color: Colors.white,
onPressed: () {},
splashColor: Colors.yellow,
icon: Icon(Icons.code),
),
),
Output:
Adjusting Radius of Splash Color
CircleAvatar(
backgroundColor: Colors.green,
child: IconButton(
color: Colors.white,
onPressed: () {},
splashColor: Colors.yellowAccent,
splashRadius: 50,
icon: Icon(Icons.code),
),
),
Output:
Adjusting the Highlight Color
CircleAvatar(
backgroundColor: Colors.green,
child: IconButton(
color: Colors.white,
onPressed: () {},
splashColor: Colors.yellowAccent,
splashRadius: 50,
highlightColor: Colors.red,
icon: Icon(Icons.code),
),
),
Output:
Let us understand these with the help of an example
Example
Dart
import 'package:flutter/material.dart';
void main() {
runApp(HomeApp());
}
class HomeApp extends StatefulWidget {
HomeApp({Key? key}) : super(key: key);
@override
State<HomeApp> createState() => _HomeAppState();
}
class _HomeAppState extends State<HomeApp> {
@override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
home: Scaffold(
appBar: AppBar(
backgroundColor: Colors.green,
title: const Text('GeeksforGeeks'),
),
body: const FirstScreen()));
}
}
class FirstScreen extends StatelessWidget {
const FirstScreen({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return Container(
child: Center(
child: CircleAvatar(
backgroundColor: Colors.green,
child: IconButton(
color: Colors.white,
onPressed: () => Navigator.of(context)
.push(MaterialPageRoute(builder: (context) => const NewScreen())),
splashColor: Colors.yellowAccent,
splashRadius: 50,
highlightColor: Colors.red,
icon: Icon(Icons.code),
),
),
));
}
}
class NewScreen extends StatefulWidget {
const NewScreen({Key? key}) : super(key: key);
@override
State<NewScreen> createState() => _NewScreenState();
}
class _NewScreenState extends State<NewScreen> {
TextEditingController textEditingController = TextEditingController();
@override
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
backgroundColor: Colors.green,
title: const Text('New Screen'),
),
body: Center(child: Text('This is your new screen')),
);
}
}
Output:
Similar Reads
Scaffold class in Flutter with Examples
The Scaffold is a class in flutter that provides many widgets or we can say APIs. The Scaffold will expand or occupy the whole available space in device screen .The class Hierarchy is as follows:Object â³ Diagnosticable â³ Diagnosticable Tree â³ Widget â³ StateFul Widget â³ ScaffoldThe constructor of the
8 min read
TextButton Class in Flutter Material Library with Examples
Text Button Class in Flutter is a material component button widgets with no border by default. It is just a regular button with some text written as the label. TextButton class is a replacement for the deprecated FlatButton class. It is undeprecated and performs all the functions of a FlatButton. To
4 min read
Icon Class in Flutter
Icon class in Flutter is used to show specific icons in our app. Instead of creating an image for our icon, we can simply use the Icon class for inserting an icon in our app. For using this class you must ensure that you have set uses-material-design: true in the pubsec.yaml file of your object.Synt
2 min read
Flutter - IconButton Widget
Flutter comes with different types of buttons like TextButton, ElevatedButton, OutlinedButton, etc. But most of the buttons are text-based. In this article, we are going to see how to implement the Flutter IconButton. It is one of the most widely used buttons in the flutter library. First, as the na
3 min read
Flutter - Loading Kit Widget with Example
In every android application, you have seen the loading bars, which are used when something is loading such as loading data, loading the screen, and fetching the data. The same flutter gives you a widget Flutter loading kit. We simply use the widget and use it in our project. A sample video is given
2 min read
FlatButton Class in Flutter Material Library with Example
Flutter FlatButton Class has been officially deprecated and should not be used. We can use TextButton class instead to achieve the same results. In the example below we have code that uses the TextButton class to achieve the same results as that of its deprecated counterpart (i.e. FlatButton). FlatB
5 min read
Raised Button Class in Flutter Material Library with Example
Raised button class comes with a slight elevation in its structure on tapping by the user. They have several properties like text color, shape, padding, button color, etc. These are rectangular in shape by default which cannot be altered and have UI along their z-axis. It is a deprecated class, and
4 min read
FloatingActionButton Class in Flutter Material Library with Example
The Floating Action Button is the most unique type of button widget provided in flutter. It is a widget that floats on the screen over other widgets. It appears as a circular icon on the screen with an icon in its center as its child. It is by default placed at the bottom-right corner of the screen.
4 min read
Flutter - Divider Widget with Example
Divider widget is used when you have multiple child and want to separate by the line or divider. A sample image is given below to get an idea about what we are going to do in this article. Â How to Use?Divider( height: 100, color: Colors.green, thickness: 1, indent : 10, endIndent : 10, ),Step By Ste
3 min read
Custom Paint Widget in Flutter
Flutter gives developers the full freedom to use every pixel on the screen and draw custom shapes. This has been one of the key selling points of Flutter. To achieve this, we use the CustomPainter class. In this article, we'll try to create the GeeksforGeeks Logo using Flutter's CustomPaint widget.
5 min read