Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flutter - How to change IconButtons size with Theme

I have a Row with multiple IconButtons and I need to change their color and size. I managed to change the color, but I'm not able to change the icons size.

IconTheme(
                  data: IconThemeData(size: 48.0, color: Colors.yellow),
                  child: Row(
                    mainAxisSize: MainAxisSize.max,
                    mainAxisAlignment: MainAxisAlignment.spaceAround,
                    children: <Widget>[
                      IconButton(
                        icon: Icon(Icons.delete),
                        onPressed: () => null,
                      ),
                      IconButton(
                        icon: Icon(Icons.file_upload),
                        onPressed: () => _addPhoto(false),
                      ),
                      IconButton(
                        icon: Icon(Icons.camera_alt),
                        onPressed: () => _addPhoto(true),
                      ),
                    ],
                  ),
                ),

If I set the size within the IconButtons with iconSize it works, but with IconTheme it doesn't.

How can I fix it?

like image 623
matte_colo Avatar asked Jul 31 '19 10:07

matte_colo


People also ask

How do you change the size of an Icon in Flutter?

You can increase the size of Icon to a required value by assigning the size property with specific double value.

How do I change the Icon button on Flutter?

Steps to change icon button color in Flutter Here is the step by step instructions: Locate the file where you have placed the IconButton widget. Inside the IconButton widget, add the color parameter and assign the color of your choice. Run the App.

How do you change Icon color and size in Flutter?

Here's how you do it:Step 1: Locate the MaterialApp widget. Step 2: Inside the MaterialApp, add the theme parameter with ThemeData class assigned. Step 3: Inside the ThemeData add the iconTheme parameter and then assign the IconThemeData . Step 4:Inside the IconThemeData add the color parameter and set its color.

How do I use Icon theme Flutter?

You can use the IconTheme class. new IconTheme( data: new IconThemeData( color: Colors. blue), child: new Icon(Icons.


2 Answers

As defined in the official docs, link here:

This property must not be null. It defaults to 24.0. The size given here is passed down to the widget in the icon property via an IconTheme. Setting the size here instead of in, for example, the Icon.size property allows the IconButton to size the splash area to fit the Icon. If you were to set the size of the Icon using Icon.size instead, then the IconButton would default to 24.0 and then the Icon itself would likely get clipped.

Therefore, IconButton needs to be given the iconSize property as this overrides the IconTheme size property. If you want your button to have size derived from IconTheme then you should make your custom IconButton which sets the iconSize for you. For example:

class CustomIconButton extends StatelessWidget {
  CustomIconButton({Key key, this.onPressed, this.icon});

  final Function onPressed;
  final Icon icon;

  @override
  Widget build(BuildContext context) {
    IconThemeData iconThemeData = IconTheme.of(context);
    return IconButton(
        onPressed: onPressed, iconSize: iconThemeData.size, icon: icon);
  }
}
like image 104
Shubham Bhardwaj Avatar answered Sep 24 '22 22:09

Shubham Bhardwaj


Use iconTheme of ThemeData like so and all your icons will be size 35 with the below code:

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      theme: ThemeData(
          iconTheme: IconThemeData(
            size: 35.0,
        ),
      ),
      home: HomePage(),
    );
  }
}
like image 27
BenBITDesign Avatar answered Sep 22 '22 22:09

BenBITDesign