Flutter table structure
Asked Answered
F

3

8

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.

Formulaic answered 30/6, 2018 at 22:17 Comment(0)
L
8

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"),
              )
            ],
          ),
        )
      ],
    ),
  )
Lightness answered 30/6, 2018 at 22:25 Comment(5)
Thanks a lot, that's exactly what I needed. I really appreciate your help :)Formulaic
i am really happy to hear thatLightness
why flex: 2 on both Expanded ? They negate each othersTektite
sorry @RémiRousselet will update my answer i didn't notice thatLightness
How you can manage text lines it should be equal in the left and right columns.Chemo
K
26

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"),
  ]),
]);
Kaete answered 23/12, 2018 at 18:43 Comment(0)
L
8

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"),
              )
            ],
          ),
        )
      ],
    ),
  )
Lightness answered 30/6, 2018 at 22:25 Comment(5)
Thanks a lot, that's exactly what I needed. I really appreciate your help :)Formulaic
i am really happy to hear thatLightness
why flex: 2 on both Expanded ? They negate each othersTektite
sorry @RémiRousselet will update my answer i didn't notice thatLightness
How you can manage text lines it should be equal in the left and right columns.Chemo
D
0

To create a table with Header & scroll.

return Container(
      child: SingleChildScrollView(
        scrollDirection: Axis.horizontal,
        child: Column(
          children: <Widget>[
            headerRow(), /* Header Row */
            dataRow(), /* Row 1 Data */
            dataRow(), /* Row 2 Data */
            Column(
              children: /* Add row cell data dynamically */,
            ),
          ],
        ),
      ),
    );

headerRow(List<dynamic> titleRowData) {
    return Container(
      height: ScreenUtil().setHeight(100),
      child: Row(
        children: /* Add header cell data dynamically */,
      ),
      decoration: BoxDecoration(
        borderRadius: BorderRadius.only(
          topLeft: Radius.circular(10),
          topRight: Radius.circular(10),
        ),
        gradient: LinearGradient(
          begin: Alignment.topCenter,
          end: Alignment.bottomCenter,
          stops: [0, 1],
          colors: [skyBlueColor, indigoColor],
        ),
      ),
    );
  }
Decoupage answered 29/10, 2020 at 5:9 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.