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,81 +43,132 @@ 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 ;
104- }
134+ DataGridRowAdapter ? buildRow (DataGridRow row) {
135+ return DataGridRowAdapter (
136+ cells: row.getCells ().map <Widget >((e) {
137+ return Container (
138+ alignment: ['id' , 'freight' ].contains (e.columnName)
139+ ? Alignment .centerRight
140+ : Alignment .centerLeft,
141+ padding: const EdgeInsets .symmetric (horizontal: 16.0 ),
142+ child: Text (e.value.toString ()),
143+ );
144+ }).toList ());
105145 }
106146
107147 @override
108- int compare (Employee a, Employee b, SortColumnDetails sortColumn) {
148+ int compare (DataGridRow ? a, DataGridRow ? b, SortColumnDetails sortColumn) {
109149 if (sortColumn.name == 'name' ) {
150+ final String ? value1 = a
151+ ? .getCells ()
152+ .firstWhereOrNull ((element) => element.columnName == sortColumn.name)
153+ ? .value
154+ .toString ();
155+ final String ? value2 = b
156+ ? .getCells ()
157+ .firstWhereOrNull ((element) => element.columnName == sortColumn.name)
158+ ? .value
159+ .toString ();
160+
161+ if (value1 == null || value2 == null ) {
162+ return 0 ;
163+ }
164+
110165 if (sortColumn.sortDirection == DataGridSortDirection .ascending) {
111- if (a.name == null || b.name == null )
112- return a.name == null ? - 1 : 1 ;
113- else
114- return a.name.toLowerCase ().compareTo (b.name.toLowerCase ());
166+ return value1.toLowerCase ().compareTo (value2.toLowerCase ());
115167 } else {
116- if (a.name == null || b.name == null )
117- return a.name == null ? 1 : - 1 ;
118- else
119- return b.name.toLowerCase ().compareTo (a.name.toLowerCase ());
168+ return value2.toLowerCase ().compareTo (value1.toLowerCase ());
120169 }
121170 }
171+
122172 return super .compare (a, b, sortColumn);
123173 }
124174}
0 commit comments