7

我想得到这个结构:

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

基本上我需要一个Tablewith 2 columnswith 2 rowsin each 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属性,仅此而已。

4

3 回答 3

25

我建议使用 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"),
  ]),
]);
于 2018-12-23T18:43:26.317 回答
7

使用flex(默认为 1),您可以将两列分开,然后使用crossAxisAlignment将它们在开头对齐项目: 在此处输入图像描述

  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"),
              )
            ],
          ),
        )
      ],
    ),
  )
于 2018-06-30T22:25:26.053 回答
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],
        ),
      ),
    );
  }
于 2020-10-29T05:09:20.557 回答