Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flutter padding for all widgets?

I'm trying to add some padding to the top and bottom of some text and icons in a Card widget. I found that flutter has an easy way to do this for containers, but can't seem to find a way to do this with other widgets (except for wrapping them in a container). This is the code I've got so far:

  body: new Container(     padding: new EdgeInsets.all(10.0),     child: new Column(       crossAxisAlignment: CrossAxisAlignment.stretch,       children: <Widget> [         new Card(           color: Colors.white70,           child: new Container(             padding: new EdgeInsets.all(10.0),             child: new Column(               mainAxisAlignment: MainAxisAlignment.spaceBetween,               crossAxisAlignment: CrossAxisAlignment.center,               children: <Widget> [ //Padding between these please                 new Text("I love Flutter", style: new TextStyle(fontSize: 20.0, fontWeight: FontWeight.bold)),                 new Icon(Icons.favorite, color: Colors.redAccent, size: 50.0)               ]             )           )         )       ]     )   ) 

So in my children of column, I'd like to add some padding to the top and bottom without having to insert a new Container. Is this possible?

like image 523
Bram Vanbilsen Avatar asked May 18 '17 16:05

Bram Vanbilsen


People also ask

How does padding work in Flutter?

Padding widget in flutter does exactly what its name says, it adds padding or empty space around a widget or a bunch of widgets. We can apply padding around any widget by placing it as the child of the Padding widget.

Can padding have children Flutter?

@jonney, Padding only takes one child. If the child has a child, though, the padding will only be added around the Padding widget's direct child. Putting the Padding object into children: <Widget>[ array made it work.

How do you send a padding to a container in Flutter?

To set padding for Container widget in Flutter, set padding property wit the required EdgeInsets value. Via EdgeInsets class object, we can set padding for top, right, bottom, and left with separate values, or a set a same value for padding on all sides.


2 Answers

You can use Padding, which is a very simple Widget that just takes another Widget as a child and an EdgeInsets object like the one you are already using as padding.

This approach of "composition over inheritance" in Flutter is very intentional. You can find a recent discussion of the pros and cons on Flutter's Gitter channel.

like image 111
david.mihola Avatar answered Sep 30 '22 03:09

david.mihola


Here is a supplemental answer that provides some code. As the accepted answer states, you can use the Padding widget. Flutter is different than Android or iOS because Padding is a widget rather than a property. (It is a property of Container, but internally it is still a widget.)

This is a Text widget wrapped with a Padding widget.

Padding(   padding: const EdgeInsets.all(8.0),   child: Text("text"), ); 

See my fuller answer here.

like image 38
Suragch Avatar answered Sep 30 '22 04:09

Suragch