How to highlight the cell value when searching in Flutter DataTable (SfDataGrid)?
In this article, we will show you how to highlight the cell value when searching in Flutter DataTable.
Initialize the SfDataGrid widget with all the necessary properties. Within the DataGridSource.buildRow(), if searchController.text is equal to the corresponding cell value, you should add the appropriate style to highlight the text in the cell. Additionally, remember to call notifyListeners when the searchController.text changes, to refresh the DataGrid and apply highlighting to the searched cell value.
Widget searchField() {
return Container(
width: 200,
height: 50,
decoration: BoxDecoration(
border: Border.all(
color: Colors.grey,
width: 1.0,
),
borderRadius: BorderRadius.circular(8.0),
),
child: TextField(
controller: employeeDataSource.searchController,
onChanged: (v) {
employeeDataSource.refreshDataGridSource();
initiateScrolling();
},
decoration: const InputDecoration(
hintText: "Search",
),
),
);
}
class EmployeeDataSource extends DataGridSource {
…
final searchController = TextEditingController();
@override
DataGridRowAdapter buildRow(DataGridRow row) {
return DataGridRowAdapter(
cells: row.getCells().map<Widget>((e) {
TextStyle? getTextStyle() {
if (searchController.text.toLowerCase() ==
e.value.toString().toLowerCase()) {
return TextStyle(
fontWeight: FontWeight.bold,
color: Colors.pinkAccent,
background: Paint()..color = Colors.yellow);
} else {
return null;
}
}
return Container(
alignment: Alignment.center,
padding: const EdgeInsets.all(8.0),
child: Text(
e.value.toString(),
style: getTextStyle(),
),
);
}).toList(),
);
}
Conclusion
I hope you enjoyed learning about How to highlight the cell value when searching in Flutter DataTable (SfDataGrid).
You can refer to our Flutter DataGrid feature tour page to know about its other groundbreaking feature representations and documentation, and how to quickly get started for configuration specifications.
For current customers, you can check out our components from the License and Downloads page. If you are new to Syncfusion®, you can try our 30-day free trial to check out our other controls.
If you have any queries or require clarifications, please let us know in the comments section below. You can also contact us through our support forums, Direct-Trac, or feedback portal. We are always happy to assist you!