你可以在PaginatedDataTable中如何使用TextField?

huangapple go评论82阅读模式
英文:

How can I use TextField in PaginatedDataTable?

问题

我正在编写一个简单的应用程序,我想在表格中使用TextField。我希望表格的每个单元格都有一个TextField,如果我输入内容,它会告诉我该值是否正确或不正确。我创建了一个名为VDataTable的DataTable类。

  1. class VDataTable extends DataTableSource {
  2. final TextEditingController _controller = TextEditingController();
  3. @override
  4. DataRow? getRow(int index) {
  5. return DataRow(cells: [
  6. DataCell(
  7. TextField(
  8. controller: _controller,
  9. onSubmitted: (value) {
  10. if (value.isNotEmpty && value == data[index]["A"].toString()) {
  11. print('Correct');
  12. }
  13. },
  14. ),
  15. ),
  16. ]);
  17. }
  18. @override
  19. bool get isRowCountApproximate => false;
  20. @override
  21. int get rowCount => strVerbsData.length;
  22. @override
  23. int get selectedRowCount => 0;
  24. }

然后在主文件中,我使用了PaginatedDataTable。

  1. class _MyHomePageState extends State<MyHomePage> {
  2. final DataTableSource _data = VDataTable();
  3. @override
  4. Widget build(BuildContext context) {
  5. return Scaffold(
  6. appBar: AppBar(
  7. title: Text(widget.title),
  8. ),
  9. body: SingleChildScrollView(
  10. child: Column(
  11. children: [
  12. PaginatedDataTable(columns: const [
  13. DataColumn(label: Text("A")),
  14. DataColumn(label: Text("B")),
  15. ], source: _data),
  16. ],
  17. ),
  18. ),
  19. );
  20. }
  21. }

现在的问题是,我无法编辑单个单元格(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.

  1. class VDataTable extends DataTableSource {
  2. final TextEditingController _controller = TextEditingController();
  3. @override
  4. DataRow? getRow(int index) {
  5. return DataRow(cells: [
  6. DataCell(
  7. TextField(
  8. controller: _controller,
  9. onSubmitted: (value) {
  10. if (value.isNotEmpty &amp;&amp; value == data[index][&quot;A&quot;].toString()) {
  11. print(&#39;Correct&#39;);
  12. }
  13. },
  14. ),
  15. ),
  16. ]);
  17. }
  18. @override
  19. // TODO: implement isRowCountApproximate
  20. bool get isRowCountApproximate =&gt; false;
  21. @override
  22. // TODO: implement rowCount
  23. int get rowCount =&gt; strVerbsData.length;
  24. @override
  25. // TODO: implement selectedRowCount
  26. int get selectedRowCount =&gt; 0;
  27. }

Then in the main file, I am using PaginatedDataTable.

  1. class _MyHomePageState extends State&lt;MyHomePage&gt; {
  2. final DataTableSource _data = VDataTable();
  3. @override
  4. Widget build(BuildContext context) {
  5. return Scaffold(
  6. appBar: AppBar(
  7. title: Text(widget.title),
  8. ),
  9. body: SingleChildScrollView(
  10. child: Column(
  11. children: [
  12. PaginatedDataTable(columns: const [
  13. DataColumn(label: Text(&quot;A&quot;)),
  14. DataColumn(label: Text(&quot;B&quot;)),
  15. ], source: _data),
  16. ],
  17. ),
  18. ),
  19. );
  20. }
  21. }

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?

你可以在PaginatedDataTable中如何使用TextField?

答案1

得分: 0

因为你只有一个单独的TextEditingController。 TextEditingController保存TextField的文本,如果你现在改变一个单独的TextField,所有的都会改变。

你必须为每个TextField创建一个新的TextEditingController。所以只需使用一个List&lt;TextEditingController&gt; 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&lt;TextEditingController&gt; textEditingController.

huangapple
  • 本文由 发表于 2023年4月10日 18:27:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/75976280.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定