Skip to content

This demo shows how to control button visibility on row selection in Flutter DataTable.

Notifications You must be signed in to change notification settings

SyncfusionExamples/How-to-control-button-visibility-on-row-selection-in-Flutter-DataTable

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

How to control button visibility on row selection in Flutter DataTable (SfDataGrid)?

In this article, we will show you how to control button visibility on row selection in Flutter DataTable.

To initialize the SfDataGrid widget with all the necessary properties, first create an instance of the DataGridController and assign it to the controller property. Use the selectedRows list from the DataGridController to check if a row is selected. In the buildRow method, if a row is selected, display an ElevatedButton; otherwise, show an empty widget (SizedBox.shrink()). When the button is clicked, it shows an AlertDialog with details of the selected row.

 @override
  DataGridRowAdapter? buildRow(DataGridRow row) {
    bool isSelected = controller.selectedRows.contains(row);
    return DataGridRowAdapter(
      cells:
          row.getCells().map<Widget>((dataGridCell) {
            return Container(
              alignment: Alignment.center,
              child:
                  dataGridCell.columnName == 'button'
                      ? LayoutBuilder(
                        builder: (
                          BuildContext context,
                          BoxConstraints constraints,
                        ) {
                          return isSelected
                              ? ElevatedButton(
                                onPressed: () {
                                  showDialog(
                                    context: context,
                                    builder:
                                        (context) => AlertDialog(
                                          content: SizedBox(
                                            height: 100,
                                            child: Column(
                                              mainAxisAlignment:
                                                  MainAxisAlignment
                                                      .spaceBetween,
                                              children: [
                                                Text(
                                                  'Employee ID: ${row.getCells()[0].value.toString()}',
                                                ),
                                                Text(
                                                  'Employee Name: ${row.getCells()[1].value.toString()}',
                                                ),
                                                Text(
                                                  'Employee Designation: ${row.getCells()[2].value.toString()}',
                                                ),
                                              ],
                                            ),
                                          ),
                                        ),
                                  );
                                },
                                child: const Text('Details'),
                              )
                              : const SizedBox.shrink();
                        },
                      )
                      : Text(dataGridCell.value.toString()),
            );
          }).toList(),
    );
}

You can download this example on GitHub.

About

This demo shows how to control button visibility on row selection in Flutter DataTable.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published