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);
}
相关推荐
烬羽15 分钟前
还在手动拼路径、写回调地狱?一文吃透 Node.js 的 path 和 fs
javascript·程序员·node.js
mONESY24 分钟前
LangChain JS 高性能并行执行解析、工具调用底层封装
javascript
YIAN2 小时前
LangChain JS 工具调用实战:基于 DeepSeek-V4-Flash 实现本地文件读取 AI 代码助手(完整可运行源码)
javascript·langchain·前端框架
尼斯湖皮皮怪2 小时前
iceCoder 记忆系统深度分析
前端·javascript
爸爸6192 小时前
07 ArkTS 基础类型与接口定义:从 TypeScript 到鸿蒙的类型升级
javascript·华为·typescript·harmonyos·鸿蒙系统
plainGeekDev3 小时前
依赖管理 → Version Catalog
android·java·kotlin
默_笙3 小时前
🌏 MCP 是 AI 界的 USB-C:一个协议让所有模型都能用上所有工具
前端·javascript
韩曙亮4 小时前
【Flutter】Flutter 进程保活 ③ ( 屏幕常亮设置 | Flutter 禁止息屏、关闭自动锁屏、屏蔽系统屏保 )
android·flutter·ios·屏幕常亮·禁止息屏
心中有国也有家4 小时前
AtomGit Flutter 鸿蒙客户端:情绪日记的时间线实现
学习·flutter·华为·harmonyos
爱笑鱼5 小时前
NDK、SDK、APP、Framework、Native 到底怎么区分?
android