forked from bosskmk/pluto_grid
-
Notifications
You must be signed in to change notification settings - Fork 14
/
main.dart
153 lines (137 loc) · 3.95 KB
/
main.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
import 'dart:convert';
import 'package:file_saver/file_saver.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:pluto_grid_plus/pluto_grid_plus.dart';
import 'package:pluto_grid_plus_export/pluto_grid_plus_export.dart'
as pluto_grid_export;
void main() {
runApp(const MyApp());
}
/// For more details, please refer to the link below for how to use it.
/// https://github.com/bosskmk/pluto_grid/blob/master/demo/lib/screen/feature/export_screen.dart
class MyApp extends StatelessWidget {
const MyApp({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: const MyHomePage(),
);
}
}
class MyHomePage extends StatefulWidget {
const MyHomePage({super.key});
@override
State<MyHomePage> createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
late PlutoGridStateManager stateManager;
final List<PlutoColumn> columns = [
PlutoColumn(
title: 'Column1',
field: 'column_1',
type: PlutoColumnType.text(),
),
PlutoColumn(
title: 'Column2',
field: 'column_2',
type: PlutoColumnType.text(),
),
PlutoColumn(
title: 'Column3',
field: 'column_3',
type: PlutoColumnType.text(),
),
];
final List<PlutoRow> rows = [
PlutoRow(
cells: {
'column_1': PlutoCell(value: 'cell 1-1'),
'column_2': PlutoCell(value: 'cell 1-2'),
'column_3': PlutoCell(value: 'cell 1-3'),
},
),
PlutoRow(
cells: {
'column_1': PlutoCell(value: 'cell 2-1'),
'column_2': PlutoCell(value: 'cell 2-2'),
'column_3': PlutoCell(value: 'cell 2-3'),
},
),
PlutoRow(
cells: {
'column_1': PlutoCell(value: 'cell 3-1'),
'column_2': PlutoCell(value: 'cell 3-2'),
'column_3': PlutoCell(value: 'cell 3-3'),
},
),
];
void exportToPdf() async {
final themeData = pluto_grid_export.ThemeData.withFont(
base: pluto_grid_export.Font.ttf(
await rootBundle.load('fonts/open_sans/OpenSans-Regular.ttf'),
),
bold: pluto_grid_export.Font.ttf(
await rootBundle.load('fonts/open_sans/OpenSans-Bold.ttf'),
),
);
var plutoGridPdfExport = pluto_grid_export.PlutoGridDefaultPdfExport(
title: "Pluto Grid Sample pdf print",
creator: "Pluto Grid Rocks!",
format: pluto_grid_export.PdfPageFormat.a4.landscape,
themeData: themeData,
);
await pluto_grid_export.Printing.sharePdf(
bytes: await plutoGridPdfExport.export(stateManager),
filename: plutoGridPdfExport.getFilename(),
);
}
void exportToCsv() async {
String title = "pluto_grid_export";
var exported = const Utf8Encoder()
.convert(pluto_grid_export.PlutoGridExport.exportCSV(stateManager));
// use file_saver from pub.dev
await FileSaver.instance
.saveFile(name: "$title.csv", bytes: exported, ext: ".csv");
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: Padding(
padding: const EdgeInsets.all(20),
child: Column(
children: [
SizedBox(
height: 50,
child: Row(
children: [
TextButton(
onPressed: exportToPdf,
child: const Text('Export to PDF'),
),
TextButton(
onPressed: exportToCsv,
child: const Text('Export to CSV'),
),
],
),
),
Expanded(
child: PlutoGrid(
columns: columns,
rows: rows,
onLoaded: (e) {
stateManager = e.stateManager;
},
),
),
],
),
),
);
}
}