代码之家  ›  专栏  ›  技术社区  ›  Christian Amani

指示在Datatable Flatter上选择的项目数的文本

  •  1
  • Christian Amani  · 技术社区  · 7 年前

    您好,我想知道如何更改指示Datatable上所选元素数量的文本。 我希望选择“1次配送”,而不是“选择1次配送”。

        class DeliveryTableSource extends DataTableSource {
      List<RequestDeliverySource> _list;
      int _rowSelectedCount = 0;
    
      DeliveryTableSource(this._list);
    
      @override
      DataRow getRow(int index) {
        if (index < 0 || index > _list.length)
          return null;
        else {
          RequestDeliverySource source = _list[index];
          return DataRow.byIndex(
              cells: buildCells(source),
              index: index,
              selected: source.isSelected,
              onSelectChanged: (bool value) {
                if (source.isSelected != value) {
                  _rowSelectedCount += value ? 1 : -1;
                  source.isSelected = value;
                  notifyListeners();
                }
              });
        }
      }
    
      List<DataCell> buildCells(RequestDeliverySource source) {
        return [
          DataCell(Text("${source.name}")),
          DataCell(Text("${source.deliveryAddress}")),
          DataCell(Text("${source.pickupAddress}")),
          DataCell(Text("${source.plannedDeliveryDate}")),
          DataCell(Text("${source.effectiveDeliveryDate}")),
          DataCell(Text("${source.showStatus()}"))
        ];
      }
    
      void selectedAll(bool checked) {
        _list.forEach((source) {
          source.isSelected = checked;
        });
        _rowSelectedCount = checked ? _list.length : 0;
        notifyListeners();
      }
    
      @override
      bool get isRowCountApproximate => false;
    
      @override
      int get rowCount => _list.length;
    
      @override
      int get selectedRowCount => _rowSelectedCount;
    }
    

    我的组件小部件表:

    class DeliveryTable extends StatefulWidget {
      DeliveryTable({Key key}) : super(key: key);
    
      @override
      State<StatefulWidget> createState() {
        return new DeliveryTableState();
      }
    }
    
    class DeliveryTableState extends State<DeliveryTable> {
      int _rowsPerPage = 10;
    
      @override
      Widget build(BuildContext context) {
        RequestDelivery delivery = new RequestDelivery();
        delivery.name = "livraison test";
        RequestDeliverySource source = new RequestDeliverySource(delivery);
        DeliveryTableSource tableSource = new DeliveryTableSource([source]);
        return Container(
            width: double.infinity,
            child: SingleChildScrollView(
                child: PaginatedDataTable(
                    header: Text("Livraison effectuée",
                        textAlign: TextAlign.center,
                        style: TextStyle(fontSize: 16.0)),
                    rowsPerPage: _rowsPerPage,
                    onSelectAll: tableSource.selectedAll,
                    onRowsPerPageChanged: (int value) {
                      setState(() {
                        _rowsPerPage = value;
                      });
                    },
                    columns: _buildColumns(),
                    source: tableSource)));
      }
    
      List<DataColumn> _buildColumns() {
        return [
          DataColumn(label: Text("Nom"), tooltip: "Nom de la livraison"),
          DataColumn(
              label: Text("Lieux de livraison"), tooltip: "Adresse de livraison"),
          DataColumn(
              label: Text("Lieux de ramassage"), tooltip: "Adresse de ramassage"),
          DataColumn(
              label: Text("Date de livraison"), tooltip: "Date de livraison"),
          DataColumn(
              label: Text("Livrée le"), tooltip: "Date de livraison effective"),
          DataColumn(label: Text("Statut"), tooltip: "Statu de la livraison")
        ];
      }
    }
    

    enter image description here

    1 回复  |  直到 7 年前
        1
  •  0
  •   Miguel Ruivo    7 年前

    这是因为 PaginatedDataTable 如果提供的源的计数大于0,则将替换标头。要绕过这个问题,您可以在自己的应用程序中创建自己的头和getter DataTableSource .

    enter image description here

    class DataTable extends StatefulWidget {
      _DataTableState createState() => _DataTableState();
    }
    
    class _DataTableState extends State<DataTable> {
      final source = MySource();
    
      @override
      Widget build(BuildContext context) {
        return PaginatedDataTable(
          columns: <DataColumn>[
            DataColumn(
              label: Text('Label'),
            ),
          ],
          header: Container(
            child: Text('I have ${source.selectedItems} deliveries selected.'),
          ),
          source: source,
        );
      }
    }
    
    class MySource extends DataTableSource {
      @override
      DataRow getRow(int index) {
        return DataRow(
          cells: <DataCell>[
            DataCell(
              Text('New cell'),
            ),
          ],
        );
      }
    
      int get selectedItems => 20; // Create your own counter
    
      @override
      bool get isRowCountApproximate => false;
    
      @override
      int get rowCount => 10;
    
      @override
      int get selectedRowCount => 0; // You want to keep this at zero
    }