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,94 +43,137 @@ 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: const EdgeInsets .symmetric (horizontal: 16.0 ),
50+ alignment: Alignment .centerRight,
51+ child: Text (
52+ 'ID' ,
53+ ))),
54+ GridTextColumn (
55+ columnName: 'name' ,
56+ label: Container (
57+ padding: const EdgeInsets .symmetric (horizontal: 16.0 ),
58+ alignment: Alignment .centerLeft,
59+ child: Text (
60+ 'Name' ,
61+ ))),
62+ GridTextColumn (
63+ columnName: 'city' ,
64+ width: 110 ,
65+ label: Container (
66+ padding: const EdgeInsets .symmetric (horizontal: 16.0 ),
67+ alignment: Alignment .centerLeft,
68+ child: Text (
69+ 'City' ,
70+ overflow: TextOverflow .ellipsis,
71+ ))),
72+ GridTextColumn (
73+ columnName: 'Freight' ,
74+ label: Container (
75+ padding: const EdgeInsets .symmetric (horizontal: 16.0 ),
76+ alignment: Alignment .centerRight,
77+ child: Text ('Freight' ))),
5178 ],
5279 ),
5380 );
5481 }
5582
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 ));
83+ List <Employee > populateData () {
84+ return < Employee > [
85+ Employee (10001 , 'James' , 'Bruxelles' , 20000 ),
86+ Employee (10002 , 'Kathryn' , 'Rosario' , 30000 ),
87+ Employee (10003 , 'Lara' , 'Recife' , 15000 ),
88+ Employee (10004 , 'Michael' , 'Graz' , 15000 ),
89+ Employee (10005 , 'Martin' , 'Montreal' , 15000 ),
90+ Employee (10006 , 'Newberry' , 'Tsawassen' , 15000 ),
91+ Employee (10007 , 'Balnc' , 'Campinas' , 15000 ),
92+ Employee (10008 , 'Perry' , 'Resende' , 15000 ),
93+ Employee (10009 , 'Gable' , 'Resende' , 15000 ),
94+ Employee (10010 , 'Grimes' , 'Recife' , 15000 ),
95+ Employee (10011 , 'Newberry' , 'Tsawassen' , 15000 ),
96+ Employee (10012 , 'Balnc' , 'Campinas' , 15000 ),
97+ Employee (10013 , 'Perry' , 'Resende' , 15000 ),
98+ Employee (10014 , 'Gable' , 'Resende' , 15000 ),
99+ Employee (10015 , 'Grimes' , 'Recife' , 15000 ),
100+ ];
67101 }
68102}
69103
70104class Employee {
71- Employee (this .id, this .name, this .designation , this .salary );
105+ Employee (this .id, this .name, this .city , this .freight );
72106
73107 final int id;
74108
75109 final String name;
76110
77- final String designation ;
111+ final String city ;
78112
79- final int salary ;
113+ final int freight ;
80114}
81115
82- class EmployeeDataSource extends DataGridSource <Employee > {
116+ class EmployeeDataSource extends DataGridSource {
117+ EmployeeDataSource ({required List <Employee > employees}) {
118+ _employeeData = employees
119+ .map <DataGridRow >((e) => DataGridRow (cells: [
120+ DataGridCell <int >(columnName: 'id' , value: e.id),
121+ DataGridCell <String >(columnName: 'name' , value: e.name),
122+ DataGridCell <String >(columnName: 'city' , value: e.city),
123+ DataGridCell <int >(columnName: 'freight' , value: e.freight),
124+ ]))
125+ .toList ();
126+ }
127+
128+ List <DataGridRow > _employeeData = [];
129+
83130 @override
84- List <Employee > get dataSource => _employees ;
131+ List <DataGridRow > get rows => _employeeData ;
85132
86133 @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 ;
134+ int compare ( DataGridRow ? a, DataGridRow ? b, SortColumnDetails sortColumn ) {
135+ final String ? value1 = a
136+ ? . getCells ()
137+ . firstWhereOrNull ((element) => element.columnName == sortColumn.name)
138+ ? .value
139+ . toString ();
140+ final String ? value2 = b
141+ ? . getCells ()
142+ . firstWhereOrNull ((element) => element.columnName == sortColumn.name)
143+ ? .value
144+ . toString () ;
145+
146+ int ? aLength = value1 ? .length ;
147+ int ? bLength = value2 ? .length ;
148+
149+ if (aLength == null || bLength == null ) {
150+ return 0 ;
104151 }
105- }
106152
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- });
153+ if (aLength.compareTo (bLength) > 0 ) {
154+ return sortColumn.sortDirection == DataGridSortDirection .ascending
155+ ? 1
156+ : - 1 ;
157+ } else if (aLength.compareTo (bLength) == - 1 ) {
158+ return sortColumn.sortDirection == DataGridSortDirection .ascending
159+ ? - 1
160+ : 1 ;
161+ } else {
162+ return 0 ;
114163 }
115- return true ;
116164 }
117165
118166 @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 ;
167+ DataGridRowAdapter ? buildRow (DataGridRow row) {
168+ return DataGridRowAdapter (
169+ cells: row.getCells ().map <Widget >((e) {
170+ return Container (
171+ alignment: ['id' , 'freight' ].contains (e.columnName)
172+ ? Alignment .centerRight
173+ : Alignment .centerLeft,
174+ padding: const EdgeInsets .symmetric (horizontal: 16.0 ),
175+ child: Text (e.value.toString ()),
176+ );
177+ }).toList ());
136178 }
137179}
0 commit comments