|
1 | | -# How-to-dynamically-modify-the-hover-color-of-rows-in-Flutter-DataTable |
2 | | -How to dynamically modify the hover color of rows in Flutter DataTable |
| 1 | +# How to dynamically modify the hover color of rows in Flutter DataTable (SfDataGrid)?. |
| 2 | + |
| 3 | +In this article, we will show you how to dynamically modify the hover color of rows 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 change the row highlighting color by using the [SfDataGridThemeData.rowHoverColor](https://pub.dev/documentation/syncfusion_flutter_core/latest/theme/SfDataGridThemeData/rowHoverColor.html) property. By wrapping the cells with a [MouseRegion](https://api.flutter.dev/flutter/widgets/MouseRegion-class.html), you can detect mouse enter and exit events to change the hover color. Use `setState` to update the rowHoverColor and trigger a rebuild of the SfDataGrid to reflect the changes. |
| 6 | + |
| 7 | +```dart |
| 8 | + @override |
| 9 | + void initState() { |
| 10 | + super.initState(); |
| 11 | + employees = getEmployeeData(); |
| 12 | + employeeDataSource = EmployeeDataSource(setState, employeeData: employees); |
| 13 | + } |
| 14 | +
|
| 15 | + @override |
| 16 | + Widget build(BuildContext context) { |
| 17 | + return Scaffold( |
| 18 | + appBar: AppBar( |
| 19 | + title: const Text('Syncfusion Flutter DataGrid'), |
| 20 | + ), |
| 21 | + body: SfDataGridTheme( |
| 22 | + data: SfDataGridThemeData( |
| 23 | + rowHoverColor: employeeDataSource._rowHoverColor), |
| 24 | + child: SfDataGrid( |
| 25 | + source: employeeDataSource, |
| 26 | + columnWidthMode: ColumnWidthMode.fill, |
| 27 | + columns: getColumn(), |
| 28 | + ), |
| 29 | + ), |
| 30 | + ); |
| 31 | + } |
| 32 | +
|
| 33 | +class EmployeeDataSource extends DataGridSource { |
| 34 | + EmployeeDataSource(this.setState, {required List<Employee> employeeData}) { |
| 35 | + ….. |
| 36 | + } |
| 37 | +
|
| 38 | + List<DataGridRow> _employeeData = []; |
| 39 | +
|
| 40 | + StateSetter setState; |
| 41 | +
|
| 42 | + Color? _rowHoverColor; |
| 43 | +
|
| 44 | + ….. |
| 45 | +
|
| 46 | + @override |
| 47 | + DataGridRowAdapter buildRow(DataGridRow row) { |
| 48 | + MaterialColor getColorBasedOnRowIndex(DataGridRow dataGridRow) { |
| 49 | + int index = _employeeData.indexOf(dataGridRow); |
| 50 | +
|
| 51 | + if (index % 2 == 0 || index == 0) { |
| 52 | + return Colors.amber; |
| 53 | + } else if (index == 1) { |
| 54 | + return Colors.purple; |
| 55 | + } else if (index == 3) { |
| 56 | + return Colors.pink; |
| 57 | + } else if (index == 5) { |
| 58 | + return Colors.teal; |
| 59 | + } else if (index == 7) { |
| 60 | + return Colors.cyan; |
| 61 | + } else if (index == 9) { |
| 62 | + return Colors.red; |
| 63 | + } else { |
| 64 | + return Colors.indigo; |
| 65 | + } |
| 66 | + } |
| 67 | +
|
| 68 | + return DataGridRowAdapter( |
| 69 | + cells: row.getCells().map<Widget>((e) { |
| 70 | + return MouseRegion( |
| 71 | + onEnter: (event) { |
| 72 | + setState(() { |
| 73 | + _rowHoverColor = getColorBasedOnRowIndex(row); |
| 74 | + }); |
| 75 | + }, |
| 76 | + onExit: (event) { |
| 77 | + setState(() { |
| 78 | + _rowHoverColor = null; |
| 79 | + }); |
| 80 | + }, |
| 81 | + child: Container( |
| 82 | + alignment: Alignment.center, |
| 83 | + padding: const EdgeInsets.all(8.0), |
| 84 | + child: Text(e.value.toString()), |
| 85 | + ), |
| 86 | + ); |
| 87 | + }).toList()); |
| 88 | + } |
| 89 | +} |
| 90 | +``` |
| 91 | +You can download this example on [GitHub](https://github.com/SyncfusionExamples/How-to-dynamically-modify-the-hover-color-of-rows-in-Flutter-DataTable). |
0 commit comments