Skip to content

SyncfusionExamples/How-to-dynamically-modify-the-hover-color-of-rows-in-Flutter-DataTable

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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

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. You can change the row highlighting color by using the SfDataGridThemeData.rowHoverColor property. By wrapping the cells with a MouseRegion, 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.

  @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());
  }
}

You can download this example on GitHub.

About

How to dynamically modify the hover color of rows in Flutter DataTable

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Contributors 3

  •  
  •  
  •