英文:
How can I use TextField in PaginatedDataTable?
问题
我正在编写一个简单的应用程序,我想在表格中使用TextField。我希望表格的每个单元格都有一个TextField,如果我输入内容,它会告诉我该值是否正确或不正确。我创建了一个名为VDataTable的DataTable类。
class VDataTable extends DataTableSource {
final TextEditingController _controller = TextEditingController();
@override
DataRow? getRow(int index) {
return DataRow(cells: [
DataCell(
TextField(
controller: _controller,
onSubmitted: (value) {
if (value.isNotEmpty && value == data[index]["A"].toString()) {
print('Correct');
}
},
),
),
]);
}
@override
bool get isRowCountApproximate => false;
@override
int get rowCount => strVerbsData.length;
@override
int get selectedRowCount => 0;
}
然后在主文件中,我使用了PaginatedDataTable。
class _MyHomePageState extends State<MyHomePage> {
final DataTableSource _data = VDataTable();
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: SingleChildScrollView(
child: Column(
children: [
PaginatedDataTable(columns: const [
DataColumn(label: Text("A")),
DataColumn(label: Text("B")),
], source: _data),
],
),
),
);
}
}
现在的问题是,我无法编辑单个单元格(TextField)。它在所有单元格中都显示TextField,而且所有单元格都显示相同的文本。有什么解决办法吗?
英文:
I am writing a simple application where I wanted to use the Table. I want that Table has TextField in cells and now if I write something, it will tell if that value is correct or incorrect. I created a DataTable class.
class VDataTable extends DataTableSource {
final TextEditingController _controller = TextEditingController();
@override
DataRow? getRow(int index) {
return DataRow(cells: [
DataCell(
TextField(
controller: _controller,
onSubmitted: (value) {
if (value.isNotEmpty && value == data[index]["A"].toString()) {
print('Correct');
}
},
),
),
]);
}
@override
// TODO: implement isRowCountApproximate
bool get isRowCountApproximate => false;
@override
// TODO: implement rowCount
int get rowCount => strVerbsData.length;
@override
// TODO: implement selectedRowCount
int get selectedRowCount => 0;
}
Then in the main file, I am using PaginatedDataTable.
class _MyHomePageState extends State<MyHomePage> {
final DataTableSource _data = VDataTable();
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: SingleChildScrollView(
child: Column(
children: [
PaginatedDataTable(columns: const [
DataColumn(label: Text("A")),
DataColumn(label: Text("B")),
], source: _data),
],
),
),
);
}
}
Now, issue is that I cannot edit a single cell (TextField). It shows textfield in all the cells and all cells shows same text. Any solution?
答案1
得分: 0
因为你只有一个单独的TextEditingController。 TextEditingController保存TextField的文本,如果你现在改变一个单独的TextField,所有的都会改变。
你必须为每个TextField创建一个新的TextEditingController。所以只需使用一个List<TextEditingController> textEditingController
。
英文:
Because you only have 1 single TextEditingController. The TextEditingController holds the text for the TextField, if you change now 1 single TextField, all will change.
You have to create a new TextEditingController for each TextField. So just use a List<TextEditingController> textEditingController
.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论