Skip to content

Commit 5e06265

Browse files
Modified the sample as per the new API structure
1 parent 6db5c8c commit 5e06265

File tree

2 files changed

+102
-68
lines changed

2 files changed

+102
-68
lines changed

lib/main.dart

Lines changed: 100 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import 'package:flutter/material.dart';
2+
import 'package:collection/collection.dart';
23
import 'package:syncfusion_flutter_datagrid/datagrid.dart';
34

45
void main() {
@@ -17,21 +18,19 @@ class MyApp extends StatelessWidget {
1718
}
1819

1920
class 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-
3027
class _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
}

pubspec.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,12 @@ publish_to: 'none' # Remove this line if you wish to publish to pub.dev
1818
version: 1.0.0+1
1919

2020
environment:
21-
sdk: ">=2.7.0 <3.0.0"
21+
sdk: ">=2.12.0 <3.0.0"
2222

2323
dependencies:
2424
flutter:
2525
sdk: flutter
26-
syncfusion_flutter_datagrid: ^18.3.40-beta
26+
syncfusion_flutter_datagrid: 19.1.54-beta.1
2727

2828

2929
# The following adds the Cupertino Icons font to your application.

0 commit comments

Comments
 (0)