简体   繁体   English

向 TableRow 添加填充

[英]Adding padding to TableRow

I'm trying to add padding to my table rows.我正在尝试向我的表格行添加填充。

This is what I have:这就是我所拥有的:

var list = (report['items'] as List)
          .map((item) => 
          TableRow(children: [
                Text(item['place']),
                Text(item['type']),
                Text(item['producer']),
                Text(item['serial_number']),
                Text(formatter.format(DateTime.parse(item['next_check_date']))
                    .toString()),
                Text(item['test_result']),
                Text(item['comments']),
              ]))
          .toList();

This is what I tried to do:这就是我试图做的:

var list = (report['items'] as List)
          .map((item) =>
      Container(
          padding: EdgeInsets.only(10.0),
          child: TableRow(children: [
                Text(item['place']),
                Text(item['type']),
                Text(item['producer']),
                Text(item['serial_number']),
                Text(formatter.format(DateTime.parse(item['next_check_date']))
                    .toString()),
                Text(item['test_result']),
                Text(item['comments']),
              ])))
          .toList();

But I get this error ( after adding the Container with the padding):但是我得到这个错误(在添加带有填充的容器之后):

The argument type 'TableRow' can't be assigned to the parameter type 'Widget'.

How I can add padding to my Table / TableRows?如何向我的表/表行添加填充?

What I found working in this case (when you want to add padding between rows in a table) is to wrap Text Widget with Container and add padding property to one of your elements (that is potentially the 'tallest' one) and use alignment property for other containers where you need to align text within the row.我发现在这种情况下(当您想在表中的行之间添加填充时)的工作是使用Container包装Text Widget 并将padding属性添加到您的元素之一(可能是“最高”的元素)并使用alignment属性对于需要在行内对齐文本的其他容器。 My example will look like the following (sorry for the image being blurry):我的示例将如下所示(抱歉图像模糊):

Padding(
  padding: const EdgeInsets.fromLTRB(32.0, 0.0, 32.0, 0.0),
  child: Table(
    columnWidths: {
      0: FractionColumnWidth(.75),
    },
    children: achievementList
        .map(
          (achivement) => TableRow(
            children: [
              Container(
                padding: EdgeInsets.only(bottom: 10.0),
                child: Text(
                  achivement.title,
                  style: TextStyle(
                    fontSize: 16.0,
                  ),
                ),
              ),
              Container(
                alignment: Alignment.centerRight,
                child: Text(
                  achivement.dateString,
                  style: TextStyle(
                    fontSize: 16.0,
                  ),
                ),
              ),
            ],
          ),
        )
        .toList(),
  ),
),

where achievementList is my object with title and dateString .其中achievementList是我与对象titledateString By first wrapping the Text Widget with Container and giving that first Container property padding: EgeInsets.only(bottom: 10.0), it gives padding to the whole row.通过首先用Container包装Text Widget 并赋予第一个Container属性padding: EgeInsets.only(bottom: 10.0),它为整行提供填充。

列表与项目之间的填充

  • Adding Space Between TableRowTableRow之间添加空间

在此处输入图片说明

    final attachmentWidget = Container(
    height: 200,
    width: width,
    decoration: BoxDecoration(
        color: Palette.white,
        borderRadius: BorderRadius.all(Radius.circular(30))),
    child: Center(
      child: Table(
        children: [
          TableRow(
            children: [
              iconColumn(
                  color: Colors.deepPurple,
                  icon: Icons.camera_alt,
                  name: 'Camera',
                  tap: null),
              iconColumn(
                  color: Palette.defaultColor,
                  icon: Icons.photo,
                  name: 'Camera Gallery',
                  tap: null),
              iconColumn(
                  color: Palette.defaultColor,
                  icon: Icons.videocam,
                  name: 'Video',
                  tap: null),
              iconColumn(
                  color: Palette.defaultColor,
                  icon: Icons.photo,
                  name: 'Video Gallery',
                  tap: null),
            ]
          ),
          TableRow(
            children: [
              VerticalSpacing(height: 15),//SizeBox Widget
              VerticalSpacing(height: 15),
              VerticalSpacing(height: 15),
              VerticalSpacing(height: 0),
            ]
          ),
          TableRow(
            children: [
              iconColumn(
                  color: Palette.mediumBlue,
                  icon: Icons.insert_drive_file,
                  name: 'Document',
                  tap: null),
              iconColumn(
                  color: Palette.chatGreen,
                  icon: Icons.audiotrack,
                  name: 'Audio',
                  tap: null),
              iconColumn(
                  color: Palette.chatGreen,
                  icon: Icons.location_on,
                  name: 'Location',
                  tap: null),
              VerticalSpacing(height: 0),
            ]
          )
        ],
      ),
    )
    );

You could wrap your table with Padding widget like this: 您可以使用Padding小部件包装表格,如下所示:

Padding(
                  padding: const EdgeInsets.symmetric(horizontal: 8.0),
                  child: Table(
                    children: [
                      TableRow(
                        children: [
                          Text('1'),
                          Text('2'),
                          Text('3'),
                        ],
                      ),
                    ],
                  ),
                ),

because TableRow isn't type of Widget.因为 TableRow 不是 Widget 类型。 you can use this你可以用这个

Container(
  padding: EdgeInsets.symmetric(horizontal: 10.0),
  child: Table(
    children: (report['items'] as List)
      .map((item) =>
      TableRow(children: [
        Text(item['place']),
        Text(item['type']),
        Text(item['producer']),
        Text(item['serial_number']),
        Text(formatter.format(DateTime.parse(item['next_check_date']))
            .toString()),
        Text(item['test_result']),
        Text(item['comments']),
      ]))
      .toList()
  ),
);

The best way I found so far (only way really) was to add a placeholder container of some height.到目前为止我发现的最好的方法(真的是唯一的方法)是添加一个具有一定高度的占位符容器。 That forces the row to be at least that tall.这迫使行至少有那么高。 Then you may want to set the relative size of that column to something small.然后您可能希望将该列的相对大小设置为较小的值。

  TableRow _buildRow(int index) {
    return TableRow(children: [
      Center(child: Icon(Icons.check_box_outline_blank), ),
      AutoSizeText("Info A", textAlign: TextAlign.center,maxLines: 1,maxFontSize: 20,),
      AutoSizeText("Info B", textAlign: TextAlign.center,maxLines: 1,maxFontSize: 20,),
      CustomTextField(
        controller: _nameController[index],
        keyboardType: TextInputType.text,
        autoFocus: false,
        icon: Icon(Icons.lightbulb_outline),
        hint: "Name",
      ),
      Container(
        height: 40,
        child: RaisedButton(
          onPressed: _pressMe,
          color: Colors.blue,
          shape: RoundedRectangleBorder(
              borderRadius: BorderRadius.all(Radius.circular(10.0))),
          child: AutoSizeText("Press Me",
              maxLines: 1,
              style: TextStyle(fontSize: 20.0, color: Colors.white)),
        ),
      ),
      Container(height: 70,),  // This is the line that helps me!!
    ]);
  }

Here are the parameters to add to your final Table widget to make this "fake" column very small (see column index 5)以下是添加到最终表格小部件的参数,以使这个“假”列非常小(参见列索引 5)

 defaultVerticalAlignment: TableCellVerticalAlignment.middle,
          columnWidths: {0: FractionColumnWidth(.05),
            1: FractionColumnWidth(.125),
            2: FractionColumnWidth(.125),
            3: FractionColumnWidth(.4),
            4: FractionColumnWidth(.2),
            5: FractionColumnWidth(.02)},

The type for Table children is List<TableRow> meaning the elements in the list must have the TableRow as the immediate widget, you can then add any widget inside the TableRow widget. Table children 的类型是List<TableRow>意味着列表中的元素必须将TableRow作为直接小部件,然后您可以在TableRow小部件中添加任何小部件。

In your case:在你的情况下:

var list = (report['items'] as List)
          .map((item) =>
     TableRow(children: [
                 Container(
          padding: EdgeInsets.only(10.0),
          child: Text(item['place'])),
                 Container(
          padding: EdgeInsets.only(10.0),
          child: Text(item['type'])),
                 Container(
          padding: EdgeInsets.only(10.0),
          child: Text(item['producer'])),
                 Container(
          padding: EdgeInsets.only(10.0),
          child: Text(item['serial_number'])),
                 Container(
          padding: EdgeInsets.only(10.0),
          child: Text(formatter.format(DateTime.parse(item['next_check_date']))
                    .toString())),
                 Container(
          padding: EdgeInsets.only(10.0),
          child: Text(item['test_result']),
                Text(item['comments'])),
              ]))
          .toList();

use sized box使用大小的盒子

TableRow(children: [
                  SizedBox(
                    height: 50.0,
                  ),
                  Icon(FontAwesomeIcons.check,
                      size: 16, color: Colors.green),
                  Text('Testing'),
                ]),

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM