|
1 |
| -# How-to-add-different-widget-to-column-headers-in-Flutter-DataTable |
2 |
| -How to add different widget to column headers in Flutter DataTable |
| 1 | +# How to add different widget to column headers in Flutter DataTable (SfDataGrid)? |
| 2 | + |
| 3 | +In this article, we will show you how to add different widget to column headers in [Flutter DataTable](https://www.syncfusion.com/flutter-widgets/flutter-datagrid). |
| 4 | + |
| 5 | +Initialize the [SfDataGrid](https://pub.dev/documentation/syncfusion_flutter_datagrid/latest/datagrid/SfDataGrid-class.html) widget with all the required properties. you can use the [GridColumn](https://pub.dev/documentation/syncfusion_flutter_datagrid/latest/datagrid/GridColumn-class.html) 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. |
| 6 | + |
| 7 | +```dart |
| 8 | + @override |
| 9 | + Widget build(BuildContext context) { |
| 10 | + return Scaffold( |
| 11 | + appBar: AppBar( |
| 12 | + title: const Text('Flutter SfDataGrid'), |
| 13 | + ), |
| 14 | + body: SfDataGrid( |
| 15 | + source: _employeeDataSource, |
| 16 | + columnWidthMode: ColumnWidthMode.auto, |
| 17 | + columns: [ |
| 18 | + GridColumn( |
| 19 | + columnName: 'id', |
| 20 | + label: Container( |
| 21 | + padding: const EdgeInsets.all(8.0), |
| 22 | + alignment: Alignment.center, |
| 23 | + child: const Text('ID'), |
| 24 | + ), |
| 25 | + ), |
| 26 | + GridColumn( |
| 27 | + columnName: 'name', |
| 28 | + label: Container( |
| 29 | + padding: const EdgeInsets.all(8.0), |
| 30 | + alignment: Alignment.center, |
| 31 | + child: const Row( |
| 32 | + mainAxisAlignment: MainAxisAlignment.center, |
| 33 | + children: [ |
| 34 | + Icon(Icons.person), |
| 35 | + SizedBox(width: 4), |
| 36 | + Text('Name'), |
| 37 | + ], |
| 38 | + ), |
| 39 | + ), |
| 40 | + ), |
| 41 | + GridColumn( |
| 42 | + columnName: 'designation', |
| 43 | + label: Container( |
| 44 | + padding: const EdgeInsets.all(8.0), |
| 45 | + alignment: Alignment.center, |
| 46 | + child: ElevatedButton.icon( |
| 47 | + onPressed: () { |
| 48 | + // Handle action |
| 49 | + }, |
| 50 | + icon: const Icon(Icons.work), |
| 51 | + label: const Text('designation'), |
| 52 | + ), |
| 53 | + ), |
| 54 | + ), |
| 55 | + GridColumn( |
| 56 | + columnName: 'button', |
| 57 | + label: Container( |
| 58 | + padding: const EdgeInsets.all(8.0), |
| 59 | + alignment: Alignment.center, |
| 60 | + child: ElevatedButton.icon( |
| 61 | + onPressed: () { |
| 62 | + // Handle add action |
| 63 | + }, |
| 64 | + icon: const Icon(Icons.add), |
| 65 | + label: const Text('Add'), |
| 66 | + ), |
| 67 | + ), |
| 68 | + ), |
| 69 | + ]), |
| 70 | + ); |
| 71 | + } |
| 72 | +``` |
| 73 | + |
| 74 | +You can download the example from [GitHub](https://github.com/SyncfusionExamples/How-to-add-different-widget-to-column-headers-in-Flutter-DataTable). |
0 commit comments