|
1 |
| -# How-to-highlight-the-last-selected-row-in-Flutter-DataTable |
2 |
| -How to highlight the last selected row in Flutter DataTable |
| 1 | +# How to highlight the last selected row in Flutter DataTable (SfDataGrid)?. |
| 2 | + |
| 3 | +In this article, we will show you how to highlight the last selected 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. Instead of setting the color in the [DataGridRowAdapter](https://pub.dev/documentation/syncfusion_flutter_datagrid/latest/datagrid/DataGridRowAdapter-class.html), apply it directly to the container. In the [onSelectionChanged](https://pub.dev/documentation/syncfusion_flutter_datagrid/latest/datagrid/SfDataGrid/onSelectionChanged.html) callback, ensure you call [notifyListeners](https://pub.dev/documentation/syncfusion_flutter_datagrid/latest/datagrid/DataGridSourceChangeNotifier/notifyListeners.html) from the DataGridSource when a new row is selected. By calling notifyListeners, all rows will be refreshed, which will trigger the buildRow method for each row. This approach allows a distinct selection color to be applied exclusively to the most recently selected 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 | + columnWidthMode: ColumnWidthMode.fill, |
| 17 | + controller: employeeDataSource.controller, |
| 18 | + selectionMode: SelectionMode.multiple, |
| 19 | + onSelectionChanged: |
| 20 | + (List<DataGridRow> newRows, List<DataGridRow> oldRows) { |
| 21 | + if (newRows.isNotEmpty) { |
| 22 | + employeeDataSource.updateDataGridSource(); |
| 23 | + } |
| 24 | + }, |
| 25 | + columns: getColumns(), |
| 26 | + ), |
| 27 | + ); |
| 28 | + } |
| 29 | +
|
| 30 | +class EmployeeDataSource extends DataGridSource { |
| 31 | +
|
| 32 | +…… |
| 33 | +
|
| 34 | + DataGridController controller = DataGridController(); |
| 35 | +
|
| 36 | + @override |
| 37 | + DataGridRowAdapter? buildRow(DataGridRow row) { |
| 38 | + Color getRowBackgroundColor(DataGridRow row, {Color? color}) { |
| 39 | + if (controller.selectedRows.last == row) { |
| 40 | + return color ?? Colors.indigo; |
| 41 | + } |
| 42 | + return Colors.transparent; |
| 43 | + } |
| 44 | +
|
| 45 | + return controller.selectedRows.isNotEmpty |
| 46 | + ? DataGridRowAdapter( |
| 47 | + cells: row.getCells().map<Widget>((dataGridCell) { |
| 48 | + return Container( |
| 49 | + color: getRowBackgroundColor(row), |
| 50 | + alignment: Alignment.center, |
| 51 | + child: Text( |
| 52 | + dataGridCell.value.toString(), |
| 53 | + )); |
| 54 | + }).toList()) |
| 55 | + : DataGridRowAdapter( |
| 56 | + cells: row.getCells().map<Widget>((dataGridCell) { |
| 57 | + return Container( |
| 58 | + color: Colors.transparent, |
| 59 | + alignment: Alignment.center, |
| 60 | + child: Text( |
| 61 | + dataGridCell.value.toString(), |
| 62 | + )); |
| 63 | + }).toList()); |
| 64 | + } |
| 65 | +
|
| 66 | + void updateDataGridSource() { |
| 67 | + notifyListeners(); |
| 68 | + } |
| 69 | +} |
| 70 | +``` |
| 71 | + |
| 72 | +You can download this example on [GitHub](https://github.com/SyncfusionExamples/How-to-highlight-the-last-selected-row-in-Flutter-DataTable). |
0 commit comments