Open In App

IconButton Class in Flutter with Example

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

Methods

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




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:


Article Tags :