【问题标题】:Flutter DataTable - How to add DataRow (without repeating HeaderRow) on button click(tap)?Flutter DataTable - 如何在按钮单击(点击)时添加 DataRow(不重复 HeaderRow)?
【发布时间】:2022-01-03 01:00:44
【问题描述】:

我有以下代码可以在按下按钮时创建一个 DataTable 运行良好,但逻辑会创建另一个表,我只想在其中添加一个 DataRow。

List<Widget> _datatableList = [];
...
  void _addDataTableWidget() {
setState(() {
  _datatableList.add(_buildEmptyDataTable());
});
}

点击:

_addDataTableWidget();

数据表:

 _buildEmptyDataTable(){
 return
  DataTable(

    columns: const [

      DataColumn(
        label: Text(
          "Weight",
          style: TextStyle(
            fontStyle: FontStyle.italic,
            color: kBlack,
          ),
        ),
        numeric: true,
        
      ),
      DataColumn(
        label: Text(
          "Reps",
          style: TextStyle(
            fontStyle: FontStyle.italic,
            color: kBlack,
          ),
        ),
        numeric: true,
      ),

    ],
    rows: sets
        .map(
          (sets) => DataRow(cells: [              
            DataCell(Text(weightController.text)),
            DataCell(Text(repsController.text)),
          ]),
        )
        .toList(),
  ),
}

如何在单击按钮时仅添加下一个数据行?

【问题讨论】:

    标签: flutter dart dynamic datatable


    【解决方案1】:

    您可以通过检查rows 是否包含数据来处理,而不是在单击时创建DataTable。并在点击事件上添加行。

    
      List<DataRow> dataRows = [];
    
      @override
      Widget build(BuildContext context) {
        return Scaffold(
          body: Column(
            children: [
              TextField(
                controller: weightController,
              ),
              TextField(
                controller: repsController,
              ),
              if (dataRows.isNotEmpty)
                DataTable(
                  columns: const [
                    DataColumn(
                      label: Text(
                        "Weight",
                        style: TextStyle(
                          fontStyle: FontStyle.italic,
                          // color: kBlack,
                        ),
                      ),
                      numeric: true,
                    ),
                    DataColumn(
                      label: Text(
                        "Reps",
                        style: TextStyle(
                          fontStyle: FontStyle.italic,
                          color: Colors.black,
                        ),
                      ),
                      numeric: true,
                    ),
                  ],
                  rows: dataRows,
                ),
            ],
          ),
          floatingActionButton: FloatingActionButton(
            onPressed: () {
              setState(() {
                dataRows.add(DataRow(cells: [
                  DataCell(Text(weightController.text)),
                  DataCell(Text(repsController.text)),
                ]));
              });
            },
          ),
        );
      }
    }
    

    【讨论】:

      猜你喜欢
      • 2019-03-31
      • 2020-06-08
      • 2017-04-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多