Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flutter table structure

I want to get this structure:

-----------------------------------------------------------------------------------
item 1                                 item 2
item 3                                 item 4
-----------------------------------------------------------------------------------

Basically I'd need to have a Table with 2 columns with 2 rows in each column, but this is the effect I get:

Here is my code:

new Container(
          decoration: new BoxDecoration(color: Colors.grey),
          child: new Row(
            children: <Widget>[

              new Column(
                children: <Widget>[
                  new Container(
                    decoration: new BoxDecoration(color: Colors.red),
                    child: new Text("item 1"),
                  ),
                  new Container(
                    decoration: new BoxDecoration(color: Colors.amber),
                    child: new Text("item 3"),
                  ),
                ],
              ),

              new Column(
                children: <Widget>[
                  new Container(
                    decoration: new BoxDecoration(color: Colors.green),
                    child: new Text("item 2"),
                  ),
                  new Container(
                    decoration: new BoxDecoration(color: Colors.teal),
                    child: new Text("item 4"),
                  )
                ],
              )

            ],
          ),
        )

I want each column to take half of the width space available. On Android I'd use the weight property and that's it.

like image 719
Ale Avatar asked Jun 30 '18 22:06

Ale


People also ask

How do I display table data in Flutter?

Following is the syntax of a DataTable. DataTable( columns: [ DataColumn(label: ), DataColumn(label: ), ... DataColumn(label: )), ], rows: [ DataRow(cells: [ DataCell( ), DataCell( ), ... DataCell( ), ]), DataRow(cells: [ DataCell( ), DataCell( ), ... DataCell( ), ]), ...


2 Answers

I would suggest using the Table Widget for consistency and ease as nested rows and columns can get a little messy and far indented.

https://docs.flutter.io/flutter/widgets/Table-class.html

   ...
Table(children: [
  TableRow(children: [
    Text("item 1"),
    Text("item 2"),
  ]),
  TableRow(children:[
    Text("item 3"),
    Text("item 4"),
  ]),
]);
like image 175
Nd. Avatar answered Oct 21 '22 17:10

Nd.


using flex(by default it's 1) you can separate the two columns and then use the crossAxisAlignmentto align them items in the beginning : enter image description here

  new Container(
    decoration: new BoxDecoration(color: Colors.grey),
    child: new Row(
      children: <Widget>[
        Expanded(
          child: new Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: <Widget>[
              new Container(
                decoration: new BoxDecoration(color: Colors.red),
                child: new Text("item 1"),
              ),
              new Container(
                decoration: new BoxDecoration(color: Colors.amber),
                child: new Text("item 3"),
              ),
            ],
          ),
        ),
        Expanded(
          child: new Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: <Widget>[
              new Container(
                decoration: new BoxDecoration(color: Colors.green),
                child: new Text("item 2"),
              ),
              new Container(
                decoration: new BoxDecoration(color: Colors.teal),
                child: new Text("item 4"),
              )
            ],
          ),
        )
      ],
    ),
  )
like image 31
Raouf Rahiche Avatar answered Oct 21 '22 18:10

Raouf Rahiche