How to Sort a Column using a Dropdown in Flutter DataTable?
In this article, we will show you how to sort a column using a dropdown in Flutter DataTable.
Initialize the SfDataGrid with the necessary properties. To sort a column using a dropdown, manually define SortColumnDetails objects and add them to the sortedColumns collection of SfDataGrid.source. When a column is selected from the dropdown, it is added to this collection, and the grid sorts the data accordingly. Before applying a new sort, the existing sorted columns are cleared to ensure only the selected column is sorted. To perform sorting at runtime, call the sort() method after updating the sortedColumns collection. The dropdown allows users to choose from available columns (eg : id, name, designation, salary), triggering sorting dynamically.
void _sortDataGrid(String columnName) {
employeeDataSource.sortedColumns.clear();
employeeDataSource.sortedColumns.add(
SortColumnDetails(
name: columnName,
sortDirection: DataGridSortDirection.ascending,
),
);
employeeDataSource.sort();
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: const Text('Syncfusion DataGrid Sorting')),
body: Column(
children: [
Padding(
padding: const EdgeInsets.all(8.0),
child: Container(
height: 40,
width: 200,
decoration: BoxDecoration(
border: Border.all(color: Colors.grey),
borderRadius: BorderRadius.circular(5),
),
padding: EdgeInsets.symmetric(horizontal: 8),
child: DropdownButtonHideUnderline(
child: DropdownButton<String>(
value: selectedColumn,
hint: const Text("Select Column to Sort"),
isExpanded: true,
icon: Icon(Icons.arrow_drop_down),
style: TextStyle(fontSize: 14, color: Colors.black),
items:
["id", "name", "designation", "salary"].map((
String column,
) {
return DropdownMenuItem<String>(
value: column,
child: Text(column.toUpperCase()),
);
}).toList(),
onChanged: (String? newValue) {
if (newValue != null) {
setState(() {
selectedColumn = newValue;
});
_sortDataGrid(newValue);
}
},
),
),
),
),
Expanded(
child: SfDataGrid(
source: employeeDataSource,
columnWidthMode: ColumnWidthMode.fill,
columns: <GridColumn>[
GridColumn(
columnName: 'id',
label: Container(
padding: EdgeInsets.all(16.0),
alignment: Alignment.center,
child: Text('ID'),
),
),
GridColumn(
columnName: 'name',
label: Container(
padding: EdgeInsets.all(8.0),
alignment: Alignment.center,
child: Text('Name'),
),
),
GridColumn(
columnName: 'designation',
label: Container(
padding: EdgeInsets.all(8.0),
alignment: Alignment.center,
child: Text('Designation'),
),
),
GridColumn(
columnName: 'salary',
label: Container(
padding: EdgeInsets.all(8.0),
alignment: Alignment.center,
child: Text('Salary'),
),
),
],
),
),
],
),
);
}
You can download this example on GitHub.
Conclusion
I hope you enjoyed learning how to sort a column using a dropdown in Flutter DataTable.
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. You can also explore our Flatter DataGrid example to understand how to create and manipulate data.
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!