颤振表结构

时间:2018-06-30 22:17:43

标签: flutter flutter-layout tabular

我想要这个结构:

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

基本上,我需要有一个Table和2个columns,每个rows中有2个column,但这是我得到的效果:

这是我的代码:

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"),
                  )
                ],
              )

            ],
          ),
        )

我希望每个column占用width可用空间的一半。 在Android上,我将使用weight属性。

3 个答案:

答案 0 :(得分:10)

我建议使用Table Widget来保持一致性和易用性,因为嵌套的行和列可能会有点混乱且缩进。

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"),
  ]),
]);

答案 1 :(得分:7)

使用flex(默认为1),您可以将两列分开,然后使用crossAxisAlignment将它们的开头对齐: 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"),
              )
            ],
          ),
        )
      ],
    ),
  )

答案 2 :(得分:0)

使用标题和滚动创建表。

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],
        ),
      ),
    );
  }