11import 'package:flutter/material.dart' ;
2+ import 'package:collection/collection.dart' ;
23import 'package:syncfusion_flutter_datagrid/datagrid.dart' ;
34
45void main () {
@@ -17,21 +18,19 @@ class MyApp extends StatelessWidget {
1718}
1819
1920class MyHomePage extends StatefulWidget {
20- MyHomePage ({Key key}) : super (key: key);
21+ MyHomePage ({Key ? key}) : super (key: key);
2122
2223 @override
2324 _MyHomePageState createState () => _MyHomePageState ();
2425}
2526
26- final List <Employee > _employees = < Employee > [];
27-
28- final EmployeeDataSource _employeeDataSource = EmployeeDataSource ();
29-
3027class _MyHomePageState extends State <MyHomePage > {
28+ late EmployeeDataSource _employeeDataSource;
29+
3130 @override
3231 void initState () {
3332 super .initState ();
34- populateData ();
33+ _employeeDataSource = EmployeeDataSource (employees : populateData () );
3534 }
3635
3736 @override
@@ -44,26 +43,54 @@ class _MyHomePageState extends State<MyHomePage> {
4443 source: _employeeDataSource,
4544 allowSorting: true ,
4645 columns: < GridColumn > [
47- GridNumericColumn (mappingName: 'id' , headerText: 'ID' ),
48- GridTextColumn (mappingName: 'name' , headerText: 'Name' ),
49- GridTextColumn (mappingName: 'designation' , headerText: 'Designation' ),
50- GridNumericColumn (mappingName: 'salary' , headerText: 'Salary' ),
46+ GridTextColumn (
47+ columnName: 'id' ,
48+ label: Container (
49+ padding: EdgeInsets .all (16.0 ),
50+ alignment: Alignment .center,
51+ child: Text (
52+ 'ID' ,
53+ ))),
54+ GridTextColumn (
55+ columnName: 'name' ,
56+ label: Container (
57+ padding: EdgeInsets .all (8.0 ),
58+ alignment: Alignment .center,
59+ child: Text ('Name' ))),
60+ GridTextColumn (
61+ columnName: 'designation' ,
62+ width: 110 ,
63+ label: Container (
64+ padding: EdgeInsets .all (8.0 ),
65+ alignment: Alignment .center,
66+ child: Text (
67+ 'Designation' ,
68+ overflow: TextOverflow .ellipsis,
69+ ))),
70+ GridTextColumn (
71+ columnName: 'salary' ,
72+ label: Container (
73+ padding: EdgeInsets .all (8.0 ),
74+ alignment: Alignment .center,
75+ child: Text ('Salary' ))),
5176 ],
5277 ),
5378 );
5479 }
5580
56- void populateData () {
57- _employees.add (Employee (10001 , 'James' , 'Project Lead' , 20000 ));
58- _employees.add (Employee (10002 , 'Kathryn' , 'Manager' , 30000 ));
59- _employees.add (Employee (10003 , 'Lara' , 'Developer' , 15000 ));
60- _employees.add (Employee (10004 , 'Michael' , 'Designer' , 15000 ));
61- _employees.add (Employee (10005 , 'Martin' , 'Developer' , 15000 ));
62- _employees.add (Employee (10006 , 'Newberry' , 'Developer' , 15000 ));
63- _employees.add (Employee (10007 , 'Balnc' , 'Developer' , 15000 ));
64- _employees.add (Employee (10008 , 'Perry' , 'Developer' , 15000 ));
65- _employees.add (Employee (10009 , 'Gable' , 'Developer' , 15000 ));
66- _employees.add (Employee (10010 , 'Grimes' , 'Developer' , 15000 ));
81+ List <Employee > populateData () {
82+ return < Employee > [
83+ Employee (10001 , 'James' , 'Project Lead' , 20000 ),
84+ Employee (10002 , 'Kathryn' , 'Manager' , 30000 ),
85+ Employee (10003 , 'Lara' , 'Developer' , 15000 ),
86+ Employee (10004 , 'Michael' , 'Designer' , 15000 ),
87+ Employee (10005 , 'Martin' , 'Developer' , 15000 ),
88+ Employee (10006 , 'Newberry' , 'Developer' , 15000 ),
89+ Employee (10007 , 'Balnc' , 'Developer' , 15000 ),
90+ Employee (10008 , 'Perry' , 'Developer' , 15000 ),
91+ Employee (10009 , 'Gable' , 'Developer' , 15000 ),
92+ Employee (10010 , 'Grimes' , 'Developer' , 15000 ),
93+ ];
6794 }
6895}
6996
@@ -79,59 +106,66 @@ class Employee {
79106 final int salary;
80107}
81108
82- class EmployeeDataSource extends DataGridSource <Employee > {
109+ class EmployeeDataSource extends DataGridSource {
110+ EmployeeDataSource ({required List <Employee > employees}) {
111+ _employeeData = employees
112+ .map <DataGridRow >((e) => DataGridRow (cells: [
113+ DataGridCell <int >(columnName: 'id' , value: e.id),
114+ DataGridCell <String >(columnName: 'name' , value: e.name),
115+ DataGridCell <String >(
116+ columnName: 'designation' , value: e.designation),
117+ DataGridCell <int >(columnName: 'salary' , value: e.salary),
118+ ]))
119+ .toList ();
120+ }
121+
122+ List <DataGridRow > _employeeData = [];
123+
83124 @override
84- List <Employee > get dataSource => _employees ;
125+ List <DataGridRow > get rows => _employeeData ;
85126
86127 @override
87- Object getValue ( Employee employee, String columnName ) {
88- switch (columnName) {
89- case 'id' :
90- return employee.id;
91- break ;
92- case 'name' :
93- return employee.name;
94- break ;
95- case 'salary' :
96- return employee.salary;
97- break ;
98- case 'designation' :
99- return employee.designation ;
100- break ;
101- default :
102- return ' ' ;
103- break ;
128+ int compare ( DataGridRow ? a, DataGridRow ? b, SortColumnDetails sortColumn ) {
129+ final String ? value1 = a
130+ ? . getCells ()
131+ . firstWhereOrNull ((element) => element.columnName == sortColumn.name)
132+ ? .value
133+ . toString ();
134+ final String ? value2 = b
135+ ? . getCells ()
136+ . firstWhereOrNull ((element) => element.columnName == sortColumn.name)
137+ ? .value
138+ . toString () ;
139+
140+ int ? aLength = value1 ? .length ;
141+ int ? bLength = value2 ? .length ;
142+
143+ if (aLength == null || bLength == null ) {
144+ return 0 ;
104145 }
105- }
106146
107- @override
108- Future <bool > handleSort () async {
109- if (sortedColumns.isNotEmpty) {
110- final sortColumn = sortedColumns.first;
111- _employees.sort ((Employee a, Employee b) {
112- return compare (a, b, sortColumn);
113- });
147+ if (aLength.compareTo (bLength) > 0 ) {
148+ return sortColumn.sortDirection == DataGridSortDirection .ascending
149+ ? 1
150+ : - 1 ;
151+ } else if (aLength.compareTo (bLength) == - 1 ) {
152+ return sortColumn.sortDirection == DataGridSortDirection .ascending
153+ ? - 1
154+ : 1 ;
155+ } else {
156+ return 0 ;
114157 }
115- return true ;
116158 }
117159
118160 @override
119- int compare (Employee a, Employee b, SortColumnDetails sortColumn) {
120- var x = getValue (a, sortColumn.name);
121- var y = getValue (b, sortColumn.name);
122- if (x == null || y == null ) {
123- if (sortColumn.sortDirection == DataGridSortDirection .ascending)
124- return x == null ? - 1 : 1 ;
125- else
126- return x == null ? 1 : - 1 ;
127- }
128- int xLength = x.toString ().length;
129- int yLength = y.toString ().length;
130- if (xLength.compareTo (yLength) > 0 )
131- return sortColumn.sortDirection == DataGridSortDirection .ascending ? 1 : - 1 ;
132- else if (xLength.compareTo (yLength) == - 1 )
133- return sortColumn.sortDirection == DataGridSortDirection .ascending ? - 1 : 1 ;
134- else
135- return 0 ;
161+ DataGridRowAdapter buildRow (DataGridRow row) {
162+ return DataGridRowAdapter (
163+ cells: row.getCells ().map <Widget >((e) {
164+ return Container (
165+ alignment: Alignment .center,
166+ padding: EdgeInsets .all (8.0 ),
167+ child: Text (e.value.toString ()),
168+ );
169+ }).toList ());
136170 }
137171}
0 commit comments