一般来说,您使用的方法应该可以按预期工作。不过,如果您使用 TextFields 填充您的 DataCells,在这种情况下,您必须将 GlobalKey 添加到 每个 TextField,所以Flutter 知道每个 DataCell 所属的位置。
有关Keys 的更多信息,请观看this Flutter mini-tutorial。
请尝试以下完整的运行示例
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),
)),
);
}
}