11import 'package:flutter/material.dart' ;
2+ import 'package:collection/collection.dart' ;
23import 'package:syncfusion_flutter_datagrid/datagrid.dart' ;
34
45void main () {
@@ -17,53 +18,82 @@ 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
3837 Widget build (BuildContext context) {
3938 return Scaffold (
40- appBar: AppBar (
41- title: const Text ('Syncfusion Flutter DataGrid' ),
42- ),
43- body: SfDataGrid (
44- source: _employeeDataSource,
45- allowSorting: true ,
46- 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' ),
51- ],
52- ),
53- );
39+ appBar: AppBar (
40+ title: const Text ('Syncfusion Flutter DataGrid' ),
41+ ),
42+ body: SfDataGrid (
43+ allowSorting: true ,
44+ source: _employeeDataSource,
45+ columns: < GridColumn > [
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+ label: Container (
63+ padding: EdgeInsets .all (8.0 ),
64+ alignment: Alignment .center,
65+ child: Text (
66+ 'Designation' ,
67+ overflow: TextOverflow .ellipsis,
68+ ))),
69+ GridTextColumn (
70+ columnName: 'salary' ,
71+ label: Container (
72+ padding: EdgeInsets .all (8.0 ),
73+ alignment: Alignment .center,
74+ child: Text ('Salary' ))),
75+ ],
76+ ));
5477 }
5578
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 ));
79+ List <Employee > populateData () {
80+ return < Employee > [
81+ Employee (10001 , 'James' , 'Project Lead' , 20000 ),
82+ Employee (10002 , 'Kathryn' , 'Manager' , 30000 ),
83+ Employee (10003 , 'Lara' , 'Developer' , 15000 ),
84+ Employee (10004 , 'Michael' , 'Designer' , 15000 ),
85+ Employee (10005 , 'martin' , 'Developer' , 15000 ),
86+ Employee (10006 , 'newberry' , 'Developer' , 15000 ),
87+ Employee (10007 , 'Balnc' , 'Developer' , 15000 ),
88+ Employee (10008 , 'Perry' , 'Developer' , 15000 ),
89+ Employee (10009 , 'Gable' , 'Developer' , 15000 ),
90+ Employee (10010 , 'grimes' , 'Developer' , 15000 ),
91+ Employee (10011 , 'Lara' , 'Developer' , 15000 ),
92+ Employee (10012 , 'Michael' , 'Designer' , 15000 ),
93+ Employee (10013 , 'martin' , 'Developer' , 15000 ),
94+ Employee (10014 , 'newberry' , 'Developer' , 15000 ),
95+ Employee (10015 , 'Balnc' , 'Developer' , 15000 ),
96+ ];
6797 }
6898}
6999
@@ -79,46 +109,61 @@ class Employee {
79109 final int salary;
80110}
81111
82- class EmployeeDataSource extends DataGridSource <Employee > {
112+ class EmployeeDataSource extends DataGridSource {
113+ EmployeeDataSource ({required List <Employee > employees}) {
114+ _employeeData = employees
115+ .map <DataGridRow >((e) => DataGridRow (cells: [
116+ DataGridCell <int >(columnName: 'id' , value: e.id),
117+ DataGridCell <String >(columnName: 'name' , value: e.name),
118+ DataGridCell <String >(
119+ columnName: 'designation' , value: e.designation),
120+ DataGridCell <int >(columnName: 'salary' , value: e.salary),
121+ ]))
122+ .toList ();
123+ }
124+
125+ List <DataGridRow > _employeeData = [];
126+
83127 @override
84- List <Employee > get dataSource => _employees ;
128+ List <DataGridRow > get rows => _employeeData ;
85129
86130 @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- }
131+ DataGridRowAdapter buildRow (DataGridRow row) {
132+ return DataGridRowAdapter (
133+ cells: row.getCells ().map <Widget >((e) {
134+ return Container (
135+ alignment: Alignment .center,
136+ padding: EdgeInsets .all (8.0 ),
137+ child: Text (e.value.toString ()),
138+ );
139+ }).toList ());
105140 }
106141
107142 @override
108- int compare (Employee a, Employee b, SortColumnDetails sortColumn) {
143+ int compare (DataGridRow ? a, DataGridRow ? b, SortColumnDetails sortColumn) {
109144 if (sortColumn.name == 'name' ) {
145+ final String ? value1 = a
146+ ? .getCells ()
147+ .firstWhereOrNull ((element) => element.columnName == sortColumn.name)
148+ ? .value
149+ .toString ();
150+ final String ? value2 = b
151+ ? .getCells ()
152+ .firstWhereOrNull ((element) => element.columnName == sortColumn.name)
153+ ? .value
154+ .toString ();
155+
156+ if (value1 == null || value2 == null ) {
157+ return 0 ;
158+ }
159+
110160 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 ());
161+ return value1.toLowerCase ().compareTo (value2.toLowerCase ());
115162 } 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 ());
163+ return value2.toLowerCase ().compareTo (value1.toLowerCase ());
120164 }
121165 }
166+
122167 return super .compare (a, b, sortColumn);
123168 }
124169}
0 commit comments