Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flutter TextButton Remove Padding and Inner Padding

Tags:

flutter

I just updated my code in Flutter to use TextButton instead of old FlatButton. I can't figure out how to set width and height of a button though.

I got two problems. First one is that I have this icon Button now:

TextButton.icon(
    label: Container(),
    style: TextButton.styleFrom(padding: EdgeInsets.all(0),
        backgroundColor: Colors.black26),
        icon: Icon(Icons.share, color: Theme.of(context).primaryColor),
        onPressed: () {}),

which loos like this: enter image description here

I can't figure out how to get rid of the padding on the left and the right. Although I did set the padding inside the style to zero.

My Second Problem is a button that I had like this:

ButtonTheme(
    materialTapTargetSize: MaterialTapTargetSize.shrinkWrap,
    height: 10,
    minWidth: 15,
    padding: EdgeInsets.only(top: 5, bottom: 5, right: 5, left: 5),
    child: FlatButton(
      color: Colors.white.withOpacity(0.9),
      child: <MyChild>,
      onPressed: () {},
      shape: RoundedRectangleBorder(
          borderRadius: BorderRadius.circular(12.0),
          side: BorderSide(
              color: condition
                  ? Theme.of(context).primaryColor
                  : widget.color != null
                      ? widget.color
                      : Colors.black54,
              width: 0.5)),
    ));
}

and it looked like this: enter image description here

Now I updated my code to this:

OutlinedButton(
    style: OutlinedButton.styleFrom(
      tapTargetSize: MaterialTapTargetSize.shrinkWrap,
      padding: EdgeInsets.only(top: 0, bottom: 0, right: 5, left: 5),
      shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(12.0)),
      side: BorderSide(
          width: 0.5,
          color: condition
              ? Theme.of(context).primaryColor
              : widget.color != null
                  ? widget.color
                  : Colors.black54),
      primary: Colors.white.withOpacity(0.9),
    ),
    child: <MyChild>,
    onPressed: () {})

But it looks like this now: enter image description here The padding on top/bottom is too much but I can't figure out how to minimize it.

Any advice? Thank you!

Edit: I tried to use OutlinedButtonTheme but this did not allow me to set a height etc.

like image 605
progNewbie Avatar asked Feb 20 '21 13:02

progNewbie


People also ask

How do you get rid of padding flutters?

removePadding, which uses this method to remove padding from the ambient MediaQuery. SafeArea, which both removes the padding from the MediaQuery and adds a Padding widget. removeViewInsets, the same thing but for viewInsets. removeViewPadding, the same thing but for viewPadding.

How do you remove padding from a FlatButton?

For all those who are wondering on how to remove the default padding around the text of a FlatButton , you can make use of RawMaterialButton instead and set the constraints to BoxConstraints() which will reset the default minimum width and height of button to zero.

How do you add padding to Textbutton in Flutter?

In Flutter, you can provide padding property to Button widget. To provide padding to Button, assign the padding property with EdgeInsets object. The following code snippet applies padding of 30 to all the four sides of the button. padding: EdgeInsets.


3 Answers

Flutter TextButton is the new button. Since Flutter 2.0 FlatButton is deprecated.

Example of how to use this button with custom styles. This is a back button with an icon. It has a wide pressable area and alignment to left according to design.

For inner padding just use Padding widget in the child property - it gives you consistent style for any String length.

TextButton(   onPressed: () => Navigator.pop(context),   style: TextButton.styleFrom(       padding: EdgeInsets.zero,       minimumSize: Size(50, 30),       alignment: Alignment.centerLeft),   child: Icon(     CupertinoIcons.back,     color: Colors.black,     size: 18,   ), ), 

enter image description here

like image 85
awaik Avatar answered Sep 19 '22 18:09

awaik


You need to set three attributes: minimumSize, padding and tapTargetSize

TextButton(   onPressed: (){},   child: Icon(Icons.delete, color: Colors.black54),   style: TextButton.styleFrom(     minimumSize: Size.zero,     padding: EdgeInsets.zero,     tapTargetSize: MaterialTapTargetSize.shrinkWrap,   ), ) 

Edit:

I have added some padding in my code, and it looks good.

padding: EdgeInsets.fromLTRB(10.0, 3.0, 10.0, 3.0), 
like image 34
mahfuz Avatar answered Sep 16 '22 18:09

mahfuz


Okay, I just figured it out. One need to set the minimumSize attribute.

OutlinedButton.styleFrom(
  minimumSize: Size(widthValue, heightValue),
)

This was what made my Buttons bigger than I wanted.

like image 43
progNewbie Avatar answered Sep 17 '22 18:09

progNewbie