How to center column and row item in Flutter?

GridFlutter Layout

Grid Problem Overview


I have a small currency table. I didn't use grid. I used Column and rows. Problem is that items in rows is not showing in center as shown below in the Excel example. What widget do I have to use to make the items centered?

enter image description here

The Example codes:

return new Center(
  child: new Column(
    crossAxisAlignment: CrossAxisAlignment.center,
    children: <Widget>[
      new Row(
        mainAxisAlignment: MainAxisAlignment.center,
        children: <Widget>[
          new Padding(
            padding: const EdgeInsets.fromLTRB(15.0, 5.0, 15.0, 5.0),
            child: new Icon(
              Icons.crop_rotate,
              color: Colors.white,
            ),
          ),
          new Padding(
            padding: const EdgeInsets.fromLTRB(15.0, 5.0, 15.0, 5.0),
            child: new Text("STG", style: mainHeadTextStyle),
          ),
          new Padding(
            padding: const EdgeInsets.fromLTRB(15.0, 5.0, 15.0, 5.0),
            child: new Text("EUR", style: mainHeadTextStyle),
          ),
          new Padding(
            padding: const EdgeInsets.fromLTRB(15.0, 5.0, 15.0, 5.0),
            child: new Text("USD", style: mainHeadTextStyle),
          ),
        ],
      ),
      new Row(
        mainAxisAlignment: MainAxisAlignment.center,
        children: <Widget>[
....
....
....

Grid Solutions


Solution 1 - Grid

If you want the whole table to be Centered, use the mainAxisAlignment property of Column.

Column

mainAxisAlignment: MainAxisAlignment.center //Center Column contents vertically,
crossAxisAlignment: CrossAxisAlignment.center //Center Column contents horizontally,

Row

mainAxisAlignment: MainAxisAlignment.center //Center Row contents horizontally,
crossAxisAlignment: CrossAxisAlignment.center //Center Row contents vertically,

Solution 2 - Grid

If you want it to be like in the picture, use

mainAxisAlignment: MainAxisAlignment.spaceEvenly

Solution 3 - Grid

You can use Spacer if you want fine grain control.

Column(
  children: <Widget>[
    Spacer(), // 1st spacer
    Widget1(),
    Widget2(),
    Spacer(), // 2nd spacer
  ],
)

You can change flex too using Spacer(flex:2)

Solution 4 - Grid

use like this

  • > In Colum

    child: Column(
                mainAxisAlignment: MainAxisAlignment.center,
                crossAxisAlignment: CrossAxisAlignment.center,
                children: [...
                           .
                           ] 
    
  • > In Row

     child: Row(
        mainAxisAlignment: MainAxisAlignment.center,
        crossAxisAlignment: CrossAxisAlignment.center,
        children: [...
                           .
                           ] 
    

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionuser9239214View Question on Stackoverflow
Solution 1 - GridVinoth KumarView Answer on Stackoverflow
Solution 2 - GridAlexander SimonovView Answer on Stackoverflow
Solution 3 - GridCopsOnRoadView Answer on Stackoverflow
Solution 4 - GridSandeep PareekView Answer on Stackoverflow