|
1 | | -# How-to-add-background-color-to-tapped-row-in-Flutter-DataTable |
2 | | -How to add background color to tapped row in Flutter DataTable. |
| 1 | +# How to add background color to tapped row in Flutter DataTable (SfDataGrid)?. |
| 2 | + |
| 3 | +In this article, we will show you how to add background color to tapped row 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 necessary properties. You can change the row background color using the [DataGridRowAdapter.color](https://pub.dev/documentation/syncfusion_flutter_datagrid/latest/datagrid/DataGridRowAdapter/color.html) property. We used the [onCellTap](https://pub.dev/documentation/syncfusion_flutter_datagrid/latest/datagrid/SfDataGrid/onCellTap.html) callback to detect the rowIndex of the tapped row. Then, by using [DataGridSource.effectiveRows](https://pub.dev/documentation/syncfusion_flutter_datagrid/latest/datagrid/DataGridSource/effectiveRows.html), you can retrieve the tapped row based on the rowIndex. To update the UI after a row is tapped, we called setState inside the onCellTap, which triggers the grid to rebuild and apply the background color to the tapped row. |
| 6 | + |
| 7 | +```dart |
| 8 | +@override |
| 9 | + Widget build(BuildContext context) { |
| 10 | + return Scaffold( |
| 11 | + appBar: AppBar( |
| 12 | + title: const Text('Syncfusion Flutter DataGrid'), |
| 13 | + ), |
| 14 | + body: SfDataGrid( |
| 15 | + source: employeeDataSource, |
| 16 | + onCellTap: (details) { |
| 17 | + // Ensure rowIndex is greater than 0 to avoid selecting the header. |
| 18 | + if (details.rowColumnIndex.rowIndex > 0) { |
| 19 | + // Adjust the row index to remove the header row from counting. |
| 20 | + setState(() { |
| 21 | + // When performing sorting or filtering,use DataGridSource.effectiveRows to retrieve the row. |
| 22 | + employeeDataSource.highlightRow = employeeDataSource |
| 23 | + .effectiveRows[details.rowColumnIndex.rowIndex - 1]; |
| 24 | + }); |
| 25 | + } |
| 26 | + }, |
| 27 | + columns: <GridColumn>[ |
| 28 | + GridColumn( |
| 29 | + columnName: 'id', |
| 30 | + label: Container( |
| 31 | + padding: const EdgeInsets.all(16.0), |
| 32 | + alignment: Alignment.center, |
| 33 | + child: const Text( |
| 34 | + 'ID', |
| 35 | + ))), |
| 36 | + GridColumn( |
| 37 | + columnName: 'name', |
| 38 | + label: Container( |
| 39 | + padding: const EdgeInsets.all(8.0), |
| 40 | + alignment: Alignment.center, |
| 41 | + child: const Text('Name'))), |
| 42 | + GridColumn( |
| 43 | + columnName: 'designation', |
| 44 | + label: Container( |
| 45 | + padding: const EdgeInsets.all(8.0), |
| 46 | + alignment: Alignment.center, |
| 47 | + child: const Text( |
| 48 | + 'Designation', |
| 49 | + overflow: TextOverflow.ellipsis, |
| 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 | + ); |
| 60 | + } |
| 61 | +
|
| 62 | +class EmployeeDataSource extends DataGridSource { |
| 63 | +
|
| 64 | + ….. |
| 65 | +
|
| 66 | + DataGridRow? highlightRow; |
| 67 | +
|
| 68 | + @override |
| 69 | + DataGridRowAdapter buildRow(DataGridRow row) { |
| 70 | + // Function to determine the background color of a row. |
| 71 | + Color getBackgroundColor() { |
| 72 | + if (row == highlightRow) { |
| 73 | + return Colors.deepPurple; |
| 74 | + } else { |
| 75 | + return Colors.transparent; |
| 76 | + } |
| 77 | + } |
| 78 | +
|
| 79 | + return DataGridRowAdapter( |
| 80 | + color: getBackgroundColor(), |
| 81 | + cells: row.getCells().map<Widget>((e) { |
| 82 | + return Container( |
| 83 | + alignment: Alignment.center, |
| 84 | + padding: const EdgeInsets.all(8.0), |
| 85 | + child: Text(e.value.toString()), |
| 86 | + ); |
| 87 | + }).toList()); |
| 88 | + } |
| 89 | +} |
| 90 | +``` |
| 91 | + |
| 92 | +You can download this example on [GitHub](https://github.com/SyncfusionExamples/How-to-add-background-color-to-tapped-row-in-Flutter-DataTable). |
0 commit comments