|
1 |
| -# How-to-add-button-column-in-Flutter-DataTable-SfDataGrid- |
| 1 | +# How-to-add-button-column-in-Flutter-DataTable--SfDataGrid- |
| 2 | + |
| 3 | +The Syncfusion [Flutter DataGrid](https://help.syncfusion.com/flutter/datagrid/overview) provides the support to load any widget in the cells. In this article, you can learn about how to load the button widget for specific column and perform any action on that button click. |
| 4 | + |
| 5 | +## STEP 1: |
| 6 | +Initialize the [SfDataGrid](https://pub.dev/documentation/syncfusion_flutter_datagrid/latest/datagrid/SfDataGrid-class.html) widget with all the required properties. |
| 7 | + |
| 8 | + |
| 9 | +```dart |
| 10 | + List<Employee> _employees = <Employee>[]; |
| 11 | + late EmployeeDataSource _employeeDataSource; |
| 12 | +
|
| 13 | + @override |
| 14 | + void initState() { |
| 15 | + super.initState(); |
| 16 | + _employees = getEmployeeData(); |
| 17 | + _employeeDataSource = EmployeeDataSource(_employees); |
| 18 | + } |
| 19 | +
|
| 20 | + @override |
| 21 | + Widget build(BuildContext context) { |
| 22 | + return Scaffold( |
| 23 | + appBar: AppBar( |
| 24 | + title: const Text('Flutter SfDataGrid'), |
| 25 | + ), |
| 26 | + body: SfDataGrid(source: _employeeDataSource, columns: [ |
| 27 | + GridColumn( |
| 28 | + columnName: 'id', |
| 29 | + label: Container( |
| 30 | + padding: const EdgeInsets.all(8.0), |
| 31 | + alignment: Alignment.center, |
| 32 | + child: const Text('ID'), |
| 33 | + ), |
| 34 | + ), |
| 35 | + GridColumn( |
| 36 | + columnName: 'name', |
| 37 | + label: Container( |
| 38 | + padding: const EdgeInsets.all(8.0), |
| 39 | + alignment: Alignment.center, |
| 40 | + child: const Text('Name'), |
| 41 | + ), |
| 42 | + ), |
| 43 | + GridColumn( |
| 44 | + columnName: 'designation', |
| 45 | + label: Container( |
| 46 | + padding: const EdgeInsets.all(8.0), |
| 47 | + alignment: Alignment.center, |
| 48 | + child: const Text('Designation'), |
| 49 | + ), |
| 50 | + ), |
| 51 | + GridColumn( |
| 52 | + columnName: 'salary', |
| 53 | + label: Container( |
| 54 | + padding: const EdgeInsets.all(8.0), |
| 55 | + alignment: Alignment.center, |
| 56 | + child: const Text('Salary '), |
| 57 | + ), |
| 58 | + ), |
| 59 | + GridColumn( |
| 60 | + columnName: 'button', |
| 61 | + label: Container( |
| 62 | + padding: const EdgeInsets.all(8.0), |
| 63 | + alignment: Alignment.center, |
| 64 | + child: const Text('Details '), |
| 65 | + ), |
| 66 | + ), |
| 67 | + ]), |
| 68 | + ); |
| 69 | + } |
| 70 | +
|
| 71 | +``` |
| 72 | +## STEP 2: |
| 73 | +Create a data source class by extending [DataGridSource](https://pub.dev/documentation/syncfusion_flutter_datagrid/latest/datagrid/DataGridSource-class.html) for mapping data to the SfDataGrid. In the [buildRow](https://pub.dev/documentation/syncfusion_flutter_datagrid/latest/datagrid/DataGridSource/buildRow.html) method, you can load the button widget based on the condition. Here, the respective row detail will be shown in the AlertDialog with a button click. |
| 74 | + |
| 75 | +```dart |
| 76 | +class EmployeeDataSource extends DataGridSource { |
| 77 | + EmployeeDataSource(List<Employee> employees) { |
| 78 | + buildDataGridRow(employees); |
| 79 | + } |
| 80 | +
|
| 81 | + void buildDataGridRow(List<Employee> employeeData) { |
| 82 | + dataGridRow = employeeData.map<DataGridRow>((employee) { |
| 83 | + return DataGridRow(cells: [ |
| 84 | + DataGridCell<int>(columnName: 'id', value: employee.id), |
| 85 | + DataGridCell<String>(columnName: 'name', value: employee.name), |
| 86 | + DataGridCell<String>( |
| 87 | + columnName: 'designation', value: employee.designation), |
| 88 | + const DataGridCell<Widget>(columnName: 'button', value: null), |
| 89 | + ]); |
| 90 | + }).toList(); |
| 91 | + } |
| 92 | +
|
| 93 | + List<DataGridRow> dataGridRow = <DataGridRow>[]; |
| 94 | +
|
| 95 | + @override |
| 96 | + List<DataGridRow> get rows => dataGridRow.isEmpty ? [] : dataGridRow; |
| 97 | +
|
| 98 | + @override |
| 99 | + DataGridRowAdapter? buildRow(DataGridRow row) { |
| 100 | + return DataGridRowAdapter( |
| 101 | + cells: row.getCells().map<Widget>((dataGridCell) { |
| 102 | + return Container( |
| 103 | + alignment: Alignment.center, |
| 104 | + child: dataGridCell.columnName == 'button' |
| 105 | + ? LayoutBuilder( |
| 106 | + builder: (BuildContext context, BoxConstraints constraints) { |
| 107 | + return ElevatedButton( |
| 108 | + onPressed: () { |
| 109 | + showDialog( |
| 110 | + context: context, |
| 111 | + builder: (context) => AlertDialog( |
| 112 | + content: SizedBox( |
| 113 | + height: 100, |
| 114 | + child: Column( |
| 115 | + mainAxisAlignment: |
| 116 | + MainAxisAlignment.spaceBetween, |
| 117 | + children: [ |
| 118 | + Text( |
| 119 | + 'Employee ID: ${row.getCells()[0].value.toString()}'), |
| 120 | + Text( |
| 121 | + 'Employee Name: ${row.getCells()[1].value.toString()}'), |
| 122 | + Text( |
| 123 | + 'Employee Designation: ${row.getCells()[2].value.toString()}'), |
| 124 | + ], |
| 125 | + )))); |
| 126 | + }, |
| 127 | + child: const Text('Details')); |
| 128 | + }) |
| 129 | + : Text(dataGridCell.value.toString())); |
| 130 | + }).toList()); |
| 131 | + } |
| 132 | +} |
| 133 | +
|
| 134 | +``` |
0 commit comments