flutter - 如何动态地将列添加到 DataTable?
问题描述
在我的项目中,我有一个DataTable
,用户可以在其中输入数据。到目前为止,用户可以添加和删除Rows
DataTable。但我不知道如何添加Columns
。问题是,每添加一列,每一行都需要一个额外的单元格。如何在我的代码中实现它?
到目前为止,这是我的代码:
import 'package:flutter/material.dart';
class ExerciseTable extends StatefulWidget {
ExerciseTable({Key key, this.title}) : super(key: key);
final String title;
@override
_ExerciseTableState createState() => _ExerciseTableState();
}
class _ExerciseTableState extends State<ExerciseTable> {
List<DataRow> _rowList = [
DataRow(cells: <DataCell>[
DataCell(Datum(key: GlobalKey())),
DataCell(ExerciseWidget(key: GlobalKey())),
DataCell(ExerciseWidget(key: GlobalKey())),
DataCell(ExerciseWidget(key: GlobalKey())),
DataCell(ExerciseWidget(key: GlobalKey())),
DataCell(ExerciseWidget(key: GlobalKey())),
DataCell(ExerciseWidget(key: GlobalKey())),
DataCell(Notes(key: GlobalKey())),
]),
];
void _addRow() {
setState(() {
_rowList.insert(
0,
(DataRow(cells: <DataCell>[
DataCell(Datum(key: GlobalKey())),
DataCell(ExerciseWidget(key: GlobalKey())),
DataCell(ExerciseWidget(key: GlobalKey())),
DataCell(ExerciseWidget(key: GlobalKey())),
DataCell(ExerciseWidget(key: GlobalKey())),
DataCell(ExerciseWidget(key: GlobalKey())),
DataCell(ExerciseWidget(key: GlobalKey())),
DataCell(Notes(key: GlobalKey())),
])));
});
}
void _deleteRow() {
setState(() {
_rowList.removeAt(0);
});
}
List<DataColumn> _columnList = [
DataColumn(label: Text('Datum', style: TextStyle(fontSize: 16))),
DataColumn(label: ExerciseName(key: GlobalKey())),
DataColumn(label: ExerciseName(key: GlobalKey())),
DataColumn(label: ExerciseName(key: GlobalKey())),
DataColumn(label: ExerciseName(key: GlobalKey())),
DataColumn(label: ExerciseName(key: GlobalKey())),
DataColumn(label: ExerciseName(key: GlobalKey())),
DataColumn(label: TextButton.icon(icon: Icon(Icons.add), label: Text('Add Column'), onPressed: null, key: GlobalKey())),
];
void _addColumn() {
setState(() {
_columnList.insert(_columnList.length-1,
DataColumn(label: ExerciseName(key: GlobalKey())),
);
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.only(bottomLeft: Radius.circular(20.0)),
),
actions: [
IconButton(
onPressed: _deleteRow,
icon: Icon(Icons.delete_outline),
),
IconButton(
onPressed: _addRow,
icon: Icon(Icons.add),
),
]),
body: SingleChildScrollView(
scrollDirection: Axis.vertical,
child: SingleChildScrollView(
scrollDirection: Axis.horizontal,
child: DataTable(
columns: _columnList,
rows: _rowList),
),
)
);
}
}
解决方案
您必须通过 _rowList 创建一个循环
- 使用 List.add(v) 而不是 List.insert(length-1, v)
- 更改数据后最好调用 setState
void _addColumn() {
for( DataRow r in _rowList)
r.cells.add(ExerciseWidget(key: GlobalKey()));
_columnList.add(DataColumn(label: ExerciseName(key: GlobalKey())));
setState(() {
});
}
推荐阅读
- apache - Moodle - 没有可用的监听套接字正在关闭
- javascript - 在导出的 es6 函数中使用 vendor 函数
- java - 如何通过 Elasticsearch 在 HibernateSearch 中进行突出显示
- windows-10 - 从 Windows 10 上的网络共享运行的应用程序无法连接到 SQL Server
- sql - 从 Linq Query 管理空白字段
- c# - 如何从 appsettings.js 加载自定义设置
- dart - Flutter 中的 Round CachedNetworkImage
- python - PyTorch 0.40 优化问题
- android - 是否有与 JavaFX 的 Timeline 类等效的 Android?
- javascript - 为什么他写movie.haveWatched、movie.title、movie.rating?