Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 382d707

Browse files
committedApr 13, 2022
Sample has been added
1 parent caa6acb commit 382d707

File tree

123 files changed

+4348
-1
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

123 files changed

+4348
-1
lines changed
 

‎.gitignore

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
# Miscellaneous
2+
*.class
3+
*.log
4+
*.pyc
5+
*.swp
6+
.DS_Store
7+
.atom/
8+
.buildlog/
9+
.history
10+
.svn/
11+
12+
# IntelliJ related
13+
*.iml
14+
*.ipr
15+
*.iws
16+
.idea/
17+
18+
# The .vscode folder contains launch configuration and tasks you configure in
19+
# VS Code which you may wish to be included in version control, so this line
20+
# is commented out by default.
21+
#.vscode/
22+
23+
# Flutter/Dart/Pub related
24+
**/doc/api/
25+
**/ios/Flutter/.last_build_id
26+
.dart_tool/
27+
.flutter-plugins
28+
.flutter-plugins-dependencies
29+
.packages
30+
.pub-cache/
31+
.pub/
32+
/build/
33+
34+
# Web related
35+
lib/generated_plugin_registrant.dart
36+
37+
# Symbolication related
38+
app.*.symbols
39+
40+
# Obfuscation related
41+
app.*.map.json
42+
43+
# Android Studio will place build artifacts here
44+
/android/app/debug
45+
/android/app/profile
46+
/android/app/release

‎README.md

Lines changed: 134 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,134 @@
1-
# How-to-add-button-column-in-Flutter-DataTable-SfDataGrid-
1+
# How-to-add-button-column-in-Flutter-DataTable--SfDataGrid-
2+
3+
The Syncfusion [Flutter DataGrid](https://help.syncfusion.com/flutter/datagrid/overview) provides the support to load any widget in the cells. In this article, you can learn about how to load the button widget for specific column and perform any action on that button click.
4+
5+
## STEP 1:
6+
Initialize the [SfDataGrid](https://pub.dev/documentation/syncfusion_flutter_datagrid/latest/datagrid/SfDataGrid-class.html) widget with all the required properties.
7+
8+
9+
```dart
10+
List<Employee> _employees = <Employee>[];
11+
late EmployeeDataSource _employeeDataSource;
12+
13+
@override
14+
void initState() {
15+
super.initState();
16+
_employees = getEmployeeData();
17+
_employeeDataSource = EmployeeDataSource(_employees);
18+
}
19+
20+
@override
21+
Widget build(BuildContext context) {
22+
return Scaffold(
23+
appBar: AppBar(
24+
title: const Text('Flutter SfDataGrid'),
25+
),
26+
body: SfDataGrid(source: _employeeDataSource, columns: [
27+
GridColumn(
28+
columnName: 'id',
29+
label: Container(
30+
padding: const EdgeInsets.all(8.0),
31+
alignment: Alignment.center,
32+
child: const Text('ID'),
33+
),
34+
),
35+
GridColumn(
36+
columnName: 'name',
37+
label: Container(
38+
padding: const EdgeInsets.all(8.0),
39+
alignment: Alignment.center,
40+
child: const Text('Name'),
41+
),
42+
),
43+
GridColumn(
44+
columnName: 'designation',
45+
label: Container(
46+
padding: const EdgeInsets.all(8.0),
47+
alignment: Alignment.center,
48+
child: const Text('Designation'),
49+
),
50+
),
51+
GridColumn(
52+
columnName: 'salary',
53+
label: Container(
54+
padding: const EdgeInsets.all(8.0),
55+
alignment: Alignment.center,
56+
child: const Text('Salary '),
57+
),
58+
),
59+
GridColumn(
60+
columnName: 'button',
61+
label: Container(
62+
padding: const EdgeInsets.all(8.0),
63+
alignment: Alignment.center,
64+
child: const Text('Details '),
65+
),
66+
),
67+
]),
68+
);
69+
}
70+
71+
```
72+
## STEP 2:
73+
Create a data source class by extending [DataGridSource](https://pub.dev/documentation/syncfusion_flutter_datagrid/latest/datagrid/DataGridSource-class.html) for mapping data to the SfDataGrid. In the [buildRow](https://pub.dev/documentation/syncfusion_flutter_datagrid/latest/datagrid/DataGridSource/buildRow.html) method, you can load the button widget based on the condition. Here, the respective row detail will be shown in the AlertDialog with a button click.
74+
75+
```dart
76+
class EmployeeDataSource extends DataGridSource {
77+
EmployeeDataSource(List<Employee> employees) {
78+
buildDataGridRow(employees);
79+
}
80+
81+
void buildDataGridRow(List<Employee> employeeData) {
82+
dataGridRow = employeeData.map<DataGridRow>((employee) {
83+
return DataGridRow(cells: [
84+
DataGridCell<int>(columnName: 'id', value: employee.id),
85+
DataGridCell<String>(columnName: 'name', value: employee.name),
86+
DataGridCell<String>(
87+
columnName: 'designation', value: employee.designation),
88+
const DataGridCell<Widget>(columnName: 'button', value: null),
89+
]);
90+
}).toList();
91+
}
92+
93+
List<DataGridRow> dataGridRow = <DataGridRow>[];
94+
95+
@override
96+
List<DataGridRow> get rows => dataGridRow.isEmpty ? [] : dataGridRow;
97+
98+
@override
99+
DataGridRowAdapter? buildRow(DataGridRow row) {
100+
return DataGridRowAdapter(
101+
cells: row.getCells().map<Widget>((dataGridCell) {
102+
return Container(
103+
alignment: Alignment.center,
104+
child: dataGridCell.columnName == 'button'
105+
? LayoutBuilder(
106+
builder: (BuildContext context, BoxConstraints constraints) {
107+
return ElevatedButton(
108+
onPressed: () {
109+
showDialog(
110+
context: context,
111+
builder: (context) => AlertDialog(
112+
content: SizedBox(
113+
height: 100,
114+
child: Column(
115+
mainAxisAlignment:
116+
MainAxisAlignment.spaceBetween,
117+
children: [
118+
Text(
119+
'Employee ID: ${row.getCells()[0].value.toString()}'),
120+
Text(
121+
'Employee Name: ${row.getCells()[1].value.toString()}'),
122+
Text(
123+
'Employee Designation: ${row.getCells()[2].value.toString()}'),
124+
],
125+
))));
126+
},
127+
child: const Text('Details'));
128+
})
129+
: Text(dataGridCell.value.toString()));
130+
}).toList());
131+
}
132+
}
133+
134+
```

0 commit comments

Comments
 (0)
Please sign in to comment.