[英]How to insert a Data Row in the first line in a DataTable?
i have a DataTable, where i want to add Rows.我有一个 DataTable,我想在其中添加行。 The Code works, but the Data Row gets add below the last Data Row.
代码有效,但数据行被添加到最后一个数据行下方。 I want to add a Row at the top.
我想在顶部添加一行。 I used list.insert(0, element) but it didnt work.
我使用了 list.insert(0, element) 但它没有用。
This is my Code so far:到目前为止,这是我的代码:
class _TabelleState extends State<Tabelle> {
List<DataRow> _rowList = [
DataRow(
cells: <DataCell>[
DataCell(Datum()),
DataCell(UebungWidget()),
DataCell(UebungWidget()),
DataCell(UebungWidget()),
]),
];
void _addRow() {
setState(() {
_rowList.insert(0, (DataRow(
cells: <DataCell>[
DataCell(Datum()),
DataCell(UebungWidget()),
DataCell(UebungWidget()),
DataCell(UebungWidget()),
])));
});
}
In general, the approach you are using should work as expected.一般来说,您使用的方法应该按预期工作。 Nevertheless, if you are populating your
DataCell
s with TextField
s, in that case you will have to add a GlobalKey
to each TextField
, so that Flutter knows where each DataCell
belongs.不过,如果您使用
TextField
填充DataCell
,在这种情况下,您必须向每个TextField
添加一个GlobalKey
,以便Flutter知道每个DataCell
所属的位置。
For more info on Keys
watch this Flutter mini-tutorial .有关
Keys
的更多信息,请观看此 Flutter 迷你教程。
Please try the following complete running sample请尝试以下完整的运行示例
import 'package:flutter/material.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: MyHomePage(),
);
}
}
class MyHomePage extends StatefulWidget {
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
@override
Widget build(BuildContext context) {
return Tabelle();
}
}
class Tabelle extends StatefulWidget {
@override
_TabelleState createState() => _TabelleState();
}
class _TabelleState extends State<Tabelle> {
List<DataRow> _rowList = [
DataRow(
cells: <DataCell>[
DataCell(TextField(key: GlobalKey(),)),
DataCell(TextField(key: GlobalKey(),)),
DataCell(TextField(key: GlobalKey(),)),
DataCell(TextField(key: GlobalKey(),)),
]),
];
void _addRow() {
setState(() {
_rowList.insert(0, (DataRow(
cells: <DataCell>[
DataCell(TextField(key: GlobalKey(),)),
DataCell(TextField(key: GlobalKey(),)),
DataCell(TextField(key: GlobalKey(),)),
DataCell(TextField(key: GlobalKey(),)),
])));
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: Text('Plan'), actions: [
TextButton(
onPressed: _addRow,
child: Text(
'Neues Training',
style: TextStyle(color: Colors.white),
),
),
]),
body: SingleChildScrollView(
scrollDirection: Axis.vertical,
child: SingleChildScrollView(
scrollDirection: Axis.horizontal,
child: DataTable(
dataRowHeight: 200,
headingRowColor:
MaterialStateColor.resolveWith((states) => Colors.black26),
dataRowColor:
MaterialStateColor.resolveWith((states) => Colors.black26),
columnSpacing: 20,
columns: [
DataColumn(label: Text('seconds', style: TextStyle(fontSize: 16),)),
DataColumn(label: Text('Datum', style: TextStyle(fontSize: 16),)),
DataColumn(label: Text('Datum', style: TextStyle(fontSize: 16),)),
DataColumn(label: Text('Datum', style: TextStyle(fontSize: 16),)),
], rows: _rowList),
)),
);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.