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

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

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 &amp;&amp; value == data[index][&quot;A&quot;].toString()) {
              print(&#39;Correct&#39;);
            }
          },
        ),
      ),
    ]);
  }

  @override
  // TODO: implement isRowCountApproximate
  bool get isRowCountApproximate =&gt; false;

  @override
  // TODO: implement rowCount
  int get rowCount =&gt; strVerbsData.length;

  @override
  // TODO: implement selectedRowCount
  int get selectedRowCount =&gt; 0;
}

Then in the main file, I am using PaginatedDataTable.

class _MyHomePageState extends State&lt;MyHomePage&gt; {

  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(&quot;A&quot;)),
              DataColumn(label: Text(&quot;B&quot;)),
            ], 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?

你可以在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:

确定