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

背景

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

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

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

  
  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);
}

在这里插入图片描述

相关推荐

  1. Flutter——详细(GestureDetector)使用教程

    2024-07-11 23:54:03       24 阅读
  2. 基于 element-ui 表格组件 el-table 导出表格数据

    2024-07-11 23:54:03       28 阅读
  3. Vue3:封装Table 表格组件问题修改

    2024-07-11 23:54:03       34 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-07-11 23:54:03       70 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-07-11 23:54:03       74 阅读
  3. 在Django里面运行非项目文件

    2024-07-11 23:54:03       62 阅读
  4. Python语言-面向对象

    2024-07-11 23:54:03       72 阅读

热门阅读

  1. Grind 75 | 3. merge two sorted lists

    2024-07-11 23:54:03       25 阅读
  2. 6、Redis系统-数据结构-07-QuickList

    2024-07-11 23:54:03       25 阅读
  3. flink使用

    2024-07-11 23:54:03       23 阅读
  4. Github 2024-07-05开源项目日报 Top10

    2024-07-11 23:54:03       21 阅读
  5. 2024.7.7刷题记录

    2024-07-11 23:54:03       21 阅读
  6. Vue3 + Vite项目使用SVG图片

    2024-07-11 23:54:03       19 阅读
  7. 代码随想录-DAY⑤-哈希表——leetcode 242 | 349 | 202

    2024-07-11 23:54:03       23 阅读
  8. Python爬虫-requests模块

    2024-07-11 23:54:03       26 阅读