How to add different widget to column headers in Flutter DataTable?
In this article, we will show you how to add different widget to column headers in Flutter DataTable.
Initialize the SfDataGrid widget with all the required properties. you can use the GridColumn property to add any widget to a column. This capability allows you to include widgets in column headers based on the respective GridColumn. Consequently, actions can be performed based on the widgets loaded in each GridColumn.
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('Flutter SfDataGrid'),
),
body: SfDataGrid(
source: _employeeDataSource,
columnWidthMode: ColumnWidthMode.auto,
columns: [
GridColumn(
columnName: 'id',
label: Container(
padding: const EdgeInsets.all(8.0),
alignment: Alignment.center,
child: const Text('ID'),
),
),
GridColumn(
columnName: 'name',
label: Container(
padding: const EdgeInsets.all(8.0),
alignment: Alignment.center,
child: const Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Icon(Icons.person),
SizedBox(width: 4),
Text('Name'),
],
),
),
),
GridColumn(
columnName: 'designation',
label: Container(
padding: const EdgeInsets.all(8.0),
alignment: Alignment.center,
child: ElevatedButton.icon(
onPressed: () {
// Handle action
},
icon: const Icon(Icons.work),
label: const Text('designation'),
),
),
),
GridColumn(
columnName: 'button',
label: Container(
padding: const EdgeInsets.all(8.0),
alignment: Alignment.center,
child: ElevatedButton.icon(
onPressed: () {
// Handle add action
},
icon: const Icon(Icons.add),
label: const Text('Add'),
),
),
),
]),
);
}
Conclusion
I hope you enjoyed learning about how to add different widget to column headers in Flutter DataTable.
You can refer to our Flutter DataTable 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 DataTable 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!