【问题标题】:How to insert a Data Row in the first line in a DataTable?如何在数据表的第一行插入数据行?
【发布时间】:2021-05-27 17:05:17
【问题描述】:

我有一个数据表,我想在其中添加行。代码有效,但数据行被添加到最后一个数据行下方。我想在顶部添加一行。我使用了 list.insert(0, element) 但它没有用。

到目前为止,这是我的代码:

class _TabelleState extends State<Tabelle> {

  List<DataRow> _rowList = [
    DataRow(
        cells: <DataCell>[
      DataCell(Datum()),
      DataCell(UebungWidget()),
      DataCell(UebungWidget()),
      DataCell(UebungWidget()),
    ]),
  ];
  
  void _addRow() {
    setState(() {
      _rowList.insert(0, (DataRow(
          cells: <DataCell>[
        DataCell(Datum()),
        DataCell(UebungWidget()),
        DataCell(UebungWidget()),
        DataCell(UebungWidget()),
      ])));
    });
  }

【问题讨论】:

    标签: list flutter datatable datarow


    【解决方案1】:

    一般来说,您使用的方法应该可以按预期工作。不过,如果您使用 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),
              )),
        );
      }
    }
    

    【讨论】:

    • 感谢您的代码,我将其与我的代码进行了比较,并没有发现错误。你知道为什么我的插入功能不起作用吗?
    • @Dalon 正如您自己所指出的,您的代码看起来还不错。如果您仍然有问题,如果您可以发布更多代码以查看一些上下文,那就太好了。也许还有数据表声明?有排序吗?
    • @Dalon,我正在运行您发布的代码,它在数据表的开头添加行,而不是在末尾...
    • 当我在 DataCells 中使用 TextField() 时它不起作用,但是当我只使用 Text() 时它起作用。我该如何解决?
    • TextField() 小部件不会像单元格中的其他小部件那样移动...我不知道为什么。就像他们卡住了
    猜你喜欢
    • 1970-01-01
    • 2015-10-28
    • 1970-01-01
    • 2020-11-12
    • 1970-01-01
    • 2013-05-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多