Flutter——最详细(Table)网格、表格组件使用教程

背景

用于展示表格组件,可指定线宽、列宽、文字方向等属性

属性 作用
columnWidths 列的宽度
defaultVerticalAlignment 网格内部组件摆放方向
border 网格样式修改
children 表格里面的组件
textDirection 文本排序方向
dart 复制代码
import 'package:flutter/material.dart';

class CustomTable extends StatelessWidget {
  const CustomTable({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    _ItemBean title = _ItemBean("姓名", "年龄", "性别", "单位名称", "单位地点");
    _ItemBean m = _ItemBean("周", "20", "男", "得意", "武汉");
    _ItemBean kg = _ItemBean("王", "18", "男", "中科", "武汉");
    _ItemBean s = _ItemBean("李", "18", "男", "互动", "武汉");
    _ItemBean a = _ItemBean("菜", "18", "男", "阿里", "武汉");
    _ItemBean k = _ItemBean("热", "18", "男", "字节", "武汉");
    _ItemBean mol = _ItemBean("赵", "18", "女", "腾讯", "武汉");
    _ItemBean cd = _ItemBean("曹", "18", "男", "盛天", "武汉");

    List<_ItemBean> data = [title, m, kg, s, a, k, mol, cd];

    return SingleChildScrollView(
      scrollDirection: Axis.horizontal,
      child: Table(
        columnWidths: const <int, TableColumnWidth>{
          0: IntrinsicColumnWidth(),//可以根据内部组件大小自适应宽度
          1: FixedColumnWidth(80.0),
          2: FixedColumnWidth(80.0),
          3: FixedColumnWidth(80.0),
          4: FixedColumnWidth(80.0),
        },
        defaultVerticalAlignment: TableCellVerticalAlignment.middle,
        border: TableBorder.all(
            color: Colors.red, width: 1.0, style: BorderStyle.solid),
        children: data
            .map((item) => TableRow(children: <Widget>[
                  Center(
                      child: Text(
                    item.name,
                    style: const TextStyle(color: Colors.black, fontSize: 18),
                  )),
                  Padding(
                    padding: const EdgeInsets.all(8.0),
                    child: Center(
                        child: Text(
                      item.symbol,
                      style: const TextStyle(color: Colors.red, fontSize: 18),
                    )),
                  ),
                  Padding(
                    padding: const EdgeInsets.all(8.0),
                    child: Center(
                        child: Text(
                      item.unitSymbol,
                      style: const TextStyle(color: Colors.green, fontSize: 18),
                    )),
                  ),
                  Padding(
                    padding: const EdgeInsets.all(8.0),
                    child: Center(
                        child: Text(
                      item.unitName,
                      style:
                          const TextStyle(color: Colors.yellow, fontSize: 18),
                    )),
                  ),
                  Padding(
                    padding: const EdgeInsets.all(8.0),
                    child: Center(
                        child: Text(
                      item.unit,
                      style:
                          const TextStyle(color: Colors.deepPurpleAccent, fontSize: 18),
                    )),
                  ),
                ]))
            .toList(),
      ),
    );
  }
}

class _ItemBean {
  String name;
  String symbol;
  String unit;
  String unitName;
  String unitSymbol;

  _ItemBean(this.name, this.symbol, this.unit, this.unitName, this.unitSymbol);
}
相关推荐
箓维22 分钟前
MySQL_表的约束
android·数据库·mysql
GuWenyue38 分钟前
昵称限 20 字,8 个 emoji 却超长:一个「字」在系统里有四种长度
javascript·数据库·后端
HEJOO93 小时前
MySQL 日期时间类型怎么选:DATE、DATETIME、TIMESTAMP、TIME、YEAR 全解析
android·数据库·mysql
画中有画4 小时前
自动化脚本中使用RSA
前端·javascript·自动化
风早爽太4 小时前
Android 开发笔记:封装 Fragment 的管理工具类
android·kotlin
峥嵘life4 小时前
Android 17 具体代码新特性总结
android·开发语言·网络
只睡四小时5 小时前
AI 写的倒计时:Electron 每轮吞 0.8 秒
javascript·人工智能·性能优化·electron·时间管理·番茄钟
全速向光5 小时前
用 Three.js 从零打造一棵会发光的 3D 圣诞树
开发语言·javascript·3d
布语world5 小时前
某东pc滑块分析
前端·javascript·网络·python·安全
map_vis_3d6 小时前
百度地图自定义覆盖物 CustomOverlay:用 HTML、React、Vue 组件做标注
前端·javascript·vue·react·百度地图