Articles in this section
Category / Section

How to select the rows through checkbox column in Flutter DataTable?

3 mins read

The Syncfusion Flutter DataTable (Data Grid) widget provides the built-in checkbox column support to select individual rows using checkboxes in each row. To enable the checkbox column, set the SfDataGrid.showCheckboxColumn property to true. The selection is applied to a row only if the SfDataGrid.selectionMode property is not none. 

 

You can get the checked items by using the DataGridController.selectedRows property.

 

The following steps explain how to select the rows using the checkbox column in Syncfusion Flutter DataTable:

 

STEP 1: Initialize the SfDataGrid widget with all the required properties and set SfDataGrid.showCheckboxColumn property to true.

 

  late EmployeeDataSource _employeeDataSource;
  List<Employee> _employees = <Employee>[];
 
  @override
  void initState() {
    super.initState();
    _employees = getEmployeeData();
    _employeeDataSource = EmployeeDataSource(employees: _employees);
  }
 
@override
Widget build(BuildContext context) {
  return Scaffold(
      appBar: AppBar(title: const Text('Syncfusion Flutter DataGrid')),
      body: SfDataGrid(
          source: _employeeDataSource,
          showCheckboxColumn: true,
          selectionMode: SelectionMode.multiple,
          columns: <GridColumn>[
            GridColumn(
                columnName: 'id',
                label: Container(
                    padding: const EdgeInsets.symmetric(horizontal: 16.0),
                    alignment: Alignment.centerRight,
                    child: const Text('ID'))),
            GridColumn(
                columnName: 'name',
                label: Container(
                    padding: const EdgeInsets.symmetric(horizontal: 16.0),
                    alignment: Alignment.centerLeft,
                    child: const Text('Name'))),
            GridColumn(
                columnName: 'designation',
                label: Container(
                    padding: const EdgeInsets.symmetric(horizontal: 16.0),
                    alignment: Alignment.centerLeft,
                    child: const Text('Designation',
                        overflow: TextOverflow.ellipsis))),
            GridColumn(
                columnName: 'salary',
                label: Container(
                    padding: const EdgeInsets.symmetric(horizontal: 16.0),
                    alignment: Alignment.centerRight,
                    child: const Text('Salary')))
          ]));
}

 

STEP 2: Create a data source class by extending DataGridSource for mapping data to the SfDataGrid.

 

class EmployeeDataSource extends DataGridSource {
  EmployeeDataSource({required List<Employee> employees}) {
    dataGridRows = employees
        .map<DataGridRow>((dataGridRow) => DataGridRow(cells: [
              DataGridCell<int>(columnName: 'id', value: dataGridRow.id),
              DataGridCell<String>(columnName: 'name', value: dataGridRow.name),
              DataGridCell<String>(
                  columnName: 'designation', value: dataGridRow.designation),
              DataGridCell<int>(
                  columnName: 'salary', value: dataGridRow.salary),
            ]))
        .toList();
  }
 
  List<DataGridRow> dataGridRows = [];
 
  @override
  List<DataGridRow> get rows => dataGridRows;
 
  @override
  DataGridRowAdapter? buildRow(DataGridRow row) {
    return DataGridRowAdapter(
        cells: row.getCells().map<Widget>((dataGridCell) {
      return Container(
          alignment: (dataGridCell.columnName == 'id' ||
                  dataGridCell.columnName == 'salary')
              ? Alignment.centerRight
              : Alignment.centerLeft,
          padding: EdgeInsets.symmetric(horizontal: 16.0),
          child: Text(
            dataGridCell.value.toString(),
            overflow: TextOverflow.ellipsis,
          ));
    }).toList());
  }
}

 

select_rows_through_checkbox_column

You can go through this UG link to know more about the checkbox column.

You can download this example on GitHub.

 


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