Articles in this section
Category / Section

How to dynamically modify the hover color of rows in Flutter DataTable (SfDataGrid)?

3 mins read

In this article, we will show you how to dynamically modify the hover color of rows in Flutter DataTable.

Initialize the SfDataGrid widget with all the required properties. We can change the row highlighting color by using the SfDataGridThemeData.rowHoverColor property. By wrapping the cells with a MouseRegion, we 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.

  @override
  void initState() {
    super.initState();
    employees = getEmployeeData();
    employeeDataSource = EmployeeDataSource(setState, employeeData: employees);
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('Syncfusion Flutter DataGrid'),
      ),
      body: SfDataGridTheme(
        data: SfDataGridThemeData(
            rowHoverColor: employeeDataSource._rowHoverColor),
        child: SfDataGrid(
          source: employeeDataSource,
          columnWidthMode: ColumnWidthMode.fill,
          columns: getColumn(),
        ),
      ),
    );
  }

class EmployeeDataSource extends DataGridSource {
  EmployeeDataSource(this.setState, {required List<Employee> employeeData}) {
   …..
  }

  List<DataGridRow> _employeeData = [];

  StateSetter setState;

  Color? _rowHoverColor;

 …..

  @override
  DataGridRowAdapter buildRow(DataGridRow row) {
    MaterialColor getColorBasedOnRowIndex(DataGridRow dataGridRow) {
      int index = _employeeData.indexOf(dataGridRow);

      if (index % 2 == 0 || index == 0) {
        return Colors.amber;
      } else if (index == 1) {
        return Colors.purple;
      } else if (index == 3) {
        return Colors.pink;
      } else if (index == 5) {
        return Colors.teal;
      } else if (index == 7) {
        return Colors.cyan;
      } else if (index == 9) {
        return Colors.red;
      } else {
        return Colors.indigo;
      }
    }

    return DataGridRowAdapter(
        cells: row.getCells().map<Widget>((e) {
      return MouseRegion(
        onEnter: (event) {
          setState(() {
            _rowHoverColor = getColorBasedOnRowIndex(row);
          });
        },
        onExit: (event) {
          setState(() {
            _rowHoverColor = null;
          });
        },
        child: Container(
          alignment: Alignment.center,
          padding: const EdgeInsets.all(8.0),
          child: Text(e.value.toString()),
        ),
      );
    }).toList());
  }
}

View the GitHub sample here.

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (0)
Please  to leave a comment
Access denied
Access denied