How to Control Button Visibility on Row Selection in Flutter DataGrid?
In this article, we will show you how to control button visibility on row selection in Flutter DataTable.
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.
Conclusion
I hope you enjoyed learning how to control button visibility on row selection in Flutter DataGrid.
You can refer to our Flutter DataGrid 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 Flatter DataGrid example 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 forums, Direct-Trac, or feedback portal. We are always happy to assist you!