【Flutter】【widget】Table 表格widget


在这里插入图片描述

前言

Table 表格widget,其实很少使用到的,等有需要的时候在查看该widget


一、Table 是什么?

表格widget,但是不能像excel 那么强大。

项目Value
电脑$1600
手机$12
导管$1

二、使用步骤

项目Value
Table一个表格
TableRow一行
TableCell一个单元格

1.Table 基础使用

代码如下(示例):

    return Scaffold(
      appBar: AppBar(
        // Here we take the value from the MyHomePage object that was created by
        // the App.build method, and use it to set our appbar title.
        title: Text(widget.title),
      ),
      body: Table(
        children: [
          TableRow(children: [
            TableCell(
              child: Text(' i am one '),
            ),
            TableCell(
              child: Text(' i am one '),
            ),
            TableCell(
              child: Text(' i am one '),
            ),
            TableCell(
              child: Text(' i am one '),
            )
          ]),
          TableRow(children: [
            TableCell(
              child: Text(' i am one '),
            ),
            TableCell(
              child: Text(' i am one '),
            ),
            TableCell(
              child: Text(' i am one '),
            ),
            TableCell(
              child: Text(' i am one '),
            )
          ])
        ],
      ),

在这里插入图片描述

2.宽度

宽度的设置的几种形式

  • FlexColumnWidth
 //columnWidths 单元格的宽,map 哪列 :宽度
  columnWidths: {
    0: FlexColumnWidth(20),
    1: FlexColumnWidth(10),
    2: FlexColumnWidth(10),
    3: FlexColumnWidth(10)
  },

如图:
在这里插入图片描述

  • IntrinsicColumnWidth:以那个最宽的单元格为该列的宽度
   columnWidths: {
     0: IntrinsicColumnWidth(),
     1: FlexColumnWidth(10),
     2: FlexColumnWidth(20),
     3: FlexColumnWidth(10)
   },

如图:
在这里插入图片描述

  • MaxColumnWidth:以那个最宽的单元格为该列的宽度,设置最大值,如果超过这个最大也是取最大值
        columnWidths: const {
          0: MaxColumnWidth(FlexColumnWidth(20), FlexColumnWidth(200)),
          1: FlexColumnWidth(10),
          2: FlexColumnWidth(10),
          3: FlexColumnWidth(10)
        },

3.设置边框

代码如下(示例):

    return Scaffold(
      appBar: AppBar(
        // Here we take the value from the MyHomePage object that was created by
        // the App.build method, and use it to set our appbar title.
        title: Text(widget.title),
      ),
      body: Table(
        border: TableBorder(
          //添加上行左右的网格线
            top: BorderSide(color: Colors.red),
            left: BorderSide(color: Colors.red),
            bottom: BorderSide(color: Colors.red),
            right: BorderSide(color: Colors.red),
            //水平线
            horizontalInside: BorderSide(color: Colors.red),
            //垂直方向
            verticalInside: BorderSide(color: Colors.red)),
        //columnWidths 单元格的宽,map 哪列 :宽度
        columnWidths: {
          0: FlexColumnWidth(20),
          1: FlexColumnWidth(10),
          2: FlexColumnWidth(10),
          3: FlexColumnWidth(10)
        },
        children: [
          TableRow(children: [
            TableCell(
              child: Text(' i am one '),
            ),
            TableCell(
              child: Text(' i am one '),
            ),
            TableCell(
              child: Text(' i am one '),
            ),
            TableCell(
              child: Text(' i am one '),
            )
          ]),
          TableRow(children: [
            TableCell(
              child: Text(' i am one '),
            ),
            TableCell(
              child: Text(' i am one '),
            ),
            TableCell(
              child: Text(' i am one '),
            ),
            TableCell(
              child: Text(' i am one '),
            )
          ])
        ],
      ),
// This trailing comma makes auto-formatting nicer for build methods.
    );

该处使用的url网络请求的数据。

4.TableCell设置单元格式widget等其他设置

  • 可以单元的内容设置为widget
  TableCell(
       child: Card(child: Text(' i am onedfasdfadsfasd ')),
     ),

在这里插入图片描述

  • 设置垂直方向的对齐方式:
    TableCell(
         verticalAlignment: TableCellVerticalAlignment.middle,
        child: Text(' i am onedfasdfadsfasd '),
      ),

在这里插入图片描述

  • 设置背景颜色
 TableRow(
              decoration: BoxDecoration(
                  color: Colors.green,
                  borderRadius: BorderRadius.all(Radius.circular(15))),
              children: [
                TableCell(
                  verticalAlignment: TableCellVerticalAlignment.bottom,
                  child: Text(' i am onedfasdfadsfasd '),
                ),

在这里插入图片描述


总结

欢迎关注,留言,咨询,交流!
在这里插入图片描述

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Flutter中的Table Widget不支持单元格合并。但是可以通过使用Row和Column Widget来实现类似的效果。 以下是一种可能的实现方式: 1. 创建一个表格的二维数组,数组中每个元素代表一个单元格。 2. 遍历二维数组,使用Row和Column Widget来构建表格,对于需要合并的单元格,可以在Row或Column的children中使用一个空的占位Widget来代替。 以下是一个简单的示例代码: ```dart Table( children: [ TableRow( children: [ _buildCell('A1', 1, 1), _buildCell('B1', 1, 2), _buildCell('', 1, 2), // 空的占位Widget _buildCell('C1', 1, 1), ], ), TableRow( children: [ _buildCell('A2', 2, 1), _buildCell('', 2, 1), // 空的占位Widget _buildCell('', 2, 1), // 空的占位Widget _buildCell('C2', 2, 1), ], ), ], ); Widget _buildCell(String text, int rowSpan, int colSpan) { if (rowSpan == 1 && colSpan == 1) { return TableCell(child: Text(text)); } else { return TableCell( child: SizedBox(), rowSpan: rowSpan, columnSpan: colSpan, ); } } ``` 这个示例代码中,_buildCell方法根据rowSpan和colSpan参数来判断是否需要合并单元格,如果需要合并,则返回一个空的占位Widget,并设置rowSpan和colSpan属性。否则,返回一个包含文本的TableCell Widget。 需要注意的是,这个实现方式并不完美,如果表格中有跨行合并的单元格,则可能会导致表格的高度不一致。如果需要实现更复杂的表格,建议使用第三方插件或自行开发。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值