Articles in this section
Category / Section

How to Get the Entire Row Details in Flutter DataGrid?

2 mins read

In this article, you'll learn how to retrieve complete row information when tapping cells in a Flutter DataGrid 

 

STEP 1Create a Data Source Class


First, create a data source class by extending
DataGridSource to map your data to the SfDataGrid widget. 

class EmployeeDataSource extends DataGridSource {
  EmployeeDataSource(List<Employee> employees) {
    buildDataGridRow(employees);
  }
 
  void buildDataGridRow(List<Employee> employeeData) {
    dataGridRow = employeeData.map<DataGridRow>((employee) {
      return DataGridRow(cells: [
        DataGridCell<int>(columnName: 'id', value: employee.id),
        DataGridCell<String>(columnName: 'name', value: employee.name),
        DataGridCell<String>(
            columnName: 'designation', value: employee.designation),
        DataGridCell<int>(columnName: 'salary', value: employee.salary),
      ]);
    }).toList();
  }
 
  List<DataGridRow> dataGridRow = <DataGridRow>[];
 
  @override
  List<DataGridRow> get rows => dataGridRow.isEmpty ? [] : dataGridRow;
 
  @override
  DataGridRowAdapter? buildRow(DataGridRow row) {
    return DataGridRowAdapter(
        cells: row.getCells().map<Widget>((dataGridCell) {
      return Container(
        alignment: Alignment.center,
        padding: const EdgeInsets.symmetric(horizontal: 8.0),
        child: SelectableText(dataGridCell.value.toString()),
      );
    }).toList());
  }
}

 

 

STEP 2Initialize the SfDataGrid Widget

Next, initialize the 
SfDataGrid widget with all required properties. When a row is tapped, you can access the row index through the onCellTap callback and fetch the corresponding row data from DataGridSource.effectiveRows. Here, the respective row details will be shown in the AlertDialog with a onCellTap.

 

List<Employee> _employees = <Employee>[];
late EmployeeDataSource _employeeDataSource;
 
@override
  void initState() {
    super.initState();
    _employees = getEmployeeData();
    _employeeDataSource = EmployeeDataSource(_employees);
  }
 
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('Flutter SfDataGrid'),
      ),
      body: SfDataGrid(
        source: _employeeDataSource,
        columns: getColumns,
        columnWidthMode: ColumnWidthMode.fill,
        allowSorting: true,
        onCellTap: ((details) {
          if (details.rowColumnIndex.rowIndex != 0) {
            int selectedRowIndex = details.rowColumnIndex.rowIndex - 1;
            var row =
                _employeeDataSource.effectiveRows.elementAt(selectedRowIndex);
 
            showDialog(
                context: context,
                builder: (context) => AlertDialog(
                    shape: const RoundedRectangleBorder(
                        borderRadius: BorderRadius.all(Radius.circular(32.0))),
                    content: SizedBox(
                      height: 300,
                      width: 300,
                      child: Column(
                          mainAxisAlignment: MainAxisAlignment.spaceBetween,
                          children: [
                            Row(children: [
                              const Text('Employee ID'),
                              const Padding(
                                  padding:
                                      EdgeInsets.symmetric(horizontal: 25)),
                              const Text(':'),
                              const Padding(
                                  padding:
                                      EdgeInsets.symmetric(horizontal: 10)),
                              Text(row.getCells()[0].value.toString()),
                            ]),
                            Row(children: [
                              const Text('Employee Name'),
                              const Padding(
                                  padding:
                                      EdgeInsets.symmetric(horizontal: 10)),
                              const Text(':'),
                              const Padding(
                                  padding:
                                      EdgeInsets.symmetric(horizontal: 10)),
                              Text(row.getCells()[1].value.toString()),
                            ]),
                            Row(children: [
                              const Text('Designation'),
                              const Padding(
                                  padding:
                                      EdgeInsets.symmetric(horizontal: 25)),
                              const Text(':'),
                              const Padding(
                                  padding:
                                      EdgeInsets.symmetric(horizontal: 10)),
                              Text(row.getCells()[2].value.toString()),
                            ]),
                            Row(children: [
                              const Text('Salary'),
                              const Padding(
                                  padding:
                                      EdgeInsets.symmetric(horizontal: 45)),
                              const Text(':'),
                              const Padding(
                                  padding:
                                      EdgeInsets.symmetric(horizontal: 10)),
                              Text(row.getCells()[3].value.toString()),
                            ]),
                            SizedBox(
                              width: 300,
                              child: ElevatedButton(
                                  onPressed: () {
                                    Navigator.pop(context);
                                  },
                                  child: const Text("OK")),
                            )
                          ]),
                    )));
          }
        }),
      ),
    );
  }

 

Get row details onCellTap in Flutter DataGrid

 

View the GitHub sample here.


Conclusion

I hope you enjoyed learning about how to get the entire row details in Flutter DataGrid.

You can refer to our Flutter SfDataGrid feature tour page to know about its other groundbreaking feature representations and documentation, and how to quickly get started for configuration specifications. You can also explore our Flutter SfDataGrid demo to understand how to create and manipulate data.

For current customers, you can check out our components from the License and Downloads page. If you are new to Syncfusion®, you can try our 30-day free trial to check out our other controls.

If you have any queries or require clarifications, please let us know in the comments section below. You can also contact us through our support forumsDirect-Trac, or feedback portal. We are always happy to assist you!

 

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (0)
Please  to leave a comment
Access denied
Access denied