flutter 在sfdatagrid中添加按钮获取整行详细信息点击

qnzebej0  于 2022-12-24  发布在  Flutter
关注(0)|答案(1)|浏览(401)

我添加SfDataGrid到我的网络应用程序在Flutter每件事都很好,只有一个问题,我面临如下。
下面是我为SfDataGrid '设计的DataSourceMethod

class EmployeeDataSource extends DataGridSource {
  EmployeeDataSource({required List<Employee> employees}) {
    _employees = employees
        .map<DataGridRow>((e) => DataGridRow(cells: [
      DataGridCell<int>(columnName: 'id', value: e.id),
      DataGridCell<String>(columnName: 'status', value: e.status),
      DataGridCell<String>(columnName: 'name', value: e.name),
      DataGridCell<String>(columnName: 'heartbeat', value: e.heartbeat),
      DataGridCell<String>(columnName: 'visibility', value: e.visibility),
      DataGridCell<String>(columnName: 'location', value: e.location),
      DataGridCell<String>(columnName: 'connectors', value: e.connectors),
      DataGridCell<String>(columnName: 'actions', value: e.actions),
    ])).toList();
  }

  List<DataGridRow>  _employees = [];

  @override
  List<DataGridRow> get rows =>  _employees;

  @override
  DataGridRowAdapter? buildRow(DataGridRow row) {
    return DataGridRowAdapter(
        cells: row.getCells().map<Widget>((dataGridCell) {
          return dataGridCell.columnName == "status" ?
          Container(
            alignment: Alignment.centerLeft,
            padding: EdgeInsets.all(16.0),
            child: Icon(
              Icons.circle,
              color:
              colorFromCssString(
                functions.getStatusColor(dataGridCell.value),
                defaultColor:
                Colors.black,
              ),
              size:
              24,
            ),
          ) : dataGridCell.columnName == "actions" ?
          Row(
            children: [
              FlutterFlowIconButton(
                borderColor: Colors.transparent,
                borderRadius: 30,
                borderWidth: 1,
                buttonSize: 60,
                icon: Icon(
                  Icons.edit,
                  color: Color(0xFF13345B),
                  size: 30,
                ),
                onPressed: () {
                  print('IconButton pressed ...');
                },
              ),
              FlutterFlowIconButton(
                borderColor: Colors.transparent,
                borderRadius: 30,
                borderWidth: 1,
                buttonSize: 60,
                icon: Icon(
                  Icons.remove_red_eye,
                  color: Color(0xFF13345B),
                  size: 30,
                ),
                onPressed: () async {

                 //So When I Tap On This Button I need Entire Row Details.
                },
              ),
            ],
          ) :
          Container(
            alignment: Alignment.centerLeft,
            padding: EdgeInsets.all(16.0),
            child: Text(
              dataGridCell.value.toString(),
              textAlign: TextAlign.start,
            ),
          );
        }).toList());
  }
}

我无法获得索引时,我点击按钮。任何人都知道如何获得一个特定的行数据时,点击一个按钮。我搜索了很多,但我只得到数据onCellTap方法不在按钮,我已经添加在DataGridRowAdapter
请帮助我,如果有人有任何想法。提前感谢

vlju58qv

vlju58qv1#

您可以从buildRow方法中的row属性获取相应行的全部详细信息。

class EmployeeDataSource extends DataGridSource {
  EmployeeDataSource({required List<Employee> employees}) {
    _employees = employees
        .map<DataGridRow>((e) => DataGridRow(cells: [
              DataGridCell<int>(columnName: 'id', value: e.id),
              DataGridCell<String>(columnName: 'status', value: e.status),
              DataGridCell<String>(columnName: 'name', value: e.name),
              DataGridCell<String>(columnName: 'heartbeat', value: e.heartbeat),
              DataGridCell<String>(
                  columnName: 'visibility', value: e.visibility),
              DataGridCell<String>(columnName: 'location', value: e.location),
              DataGridCell<String>(
                  columnName: 'connectors', value: e.connectors),
              DataGridCell<String>(columnName: 'actions', value: e.actions),
            ]))
        .toList();
  }

  List<DataGridRow> _employees = [];

  @override
  List<DataGridRow> get rows => _employees;

  @override
  DataGridRowAdapter? buildRow(DataGridRow row) {
    return DataGridRowAdapter(
        cells: row.getCells().map<Widget>((dataGridCell) {
      return dataGridCell.columnName == "status"
          ? Container(
              alignment: Alignment.centerLeft,
              padding: const EdgeInsets.all(16.0),
              child: Icon(
                Icons.circle,
                color: dataGridCell.value == 'available'
                    ? Colors.green
                    : Colors.red,
                size: 24,
              ),
            )
          : dataGridCell.columnName == "actions"
              ? Row(
                  children: [
                    IconButton(
                      icon: const Icon(
                        Icons.edit,
                        color: Color(0xFF13345B),
                        size: 30,
                      ),
                      onPressed: () {
                        print('IconButton pressed ...');
                      },
                    ),
                    IconButton(
                      icon: const Icon(
                        Icons.remove_red_eye,
                        color: Color(0xFF13345B),
                        size: 30,
                      ),
                      onPressed: () async {
                        // You can get the entire details of the corresponding row when tapping on the button
                        print('ID ${row.getCells()[0].value}');
                        for (var details in row.getCells()) {
                          print(details.value);
                        }
                      },
                    ),
                  ],
                )
              : Container(
                  alignment: Alignment.centerLeft,
                  padding: const EdgeInsets.all(16.0),
                  child: Text(
                    dataGridCell.value.toString(),
                    textAlign: TextAlign.start,
                  ),
                );
    }).toList());
  }
}

相关问题