widget_test.dart
7.48 KB
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
/*
* Copyright (C) 2017, David PHAM-VAN <dev.nfet.net@gmail.com>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import 'dart:convert';
import 'dart:io';
import 'package:pdf/pdf.dart';
import 'package:pdf/widgets.dart';
import 'package:test/test.dart';
void main() {
test('Pdf', () {
Document.debug = true;
final Document pdf = Document(title: 'Widgets Test');
final TextStyle symbol = TextStyle(font: Font.zapfDingbats());
final List<int> imData = zlib.decode(base64.decode(
'eJz7//8/w388uOTCT6a4Ez96Q47++I+OI479mEVALyNU7z9seuNP/mAm196Ekz8YR+0dWHtBmJC9S+7/Zog89iMIKLYaHQPVJGLTD7MXpDfq+I9goNhPdPPDjv3YlnH6Jye6+2H21l/6yeB/4HsSDr1bQXrRwq8HqHcGyF6QXp9933N0tn/7Y7vn+/9gLPaih0PDlV9MIAzVm6ez7dsfzW3f/oMwzAx0e7FhoJutdbcj9MKw9frnL2J2POfBpxeEg478YLba/X0Wsl6lBXf+s0bP/s8ePXeWePJCvPEJNYMRZIYWSO/cq/9Z/Nv+M4bO+M8YDjFDJGkhzvSE7A6jRTdnsQR2wfXCMLHuMC5byyidvGgWE5JeZDOIcYdR+TpmkBno+mFmAAC+DGhl'));
final PdfImage im =
PdfImage(pdf.document, image: imData, width: 16, height: 20);
pdf.addPage(Page(
pageFormat: const PdfPageFormat(400, 400),
margin: const EdgeInsets.all(10),
build: (Context context) => Column(children: <Widget>[
Container(
padding: const EdgeInsets.all(5),
margin: const EdgeInsets.only(bottom: 10),
decoration: const BoxDecoration(
color: PdfColor.amber,
border: BoxBorder(
top: true,
bottom: true,
left: true,
right: true,
width: 2)),
child: Text('Hello World',
textScaleFactor: 2, textAlign: TextAlign.center)),
Align(alignment: Alignment.topLeft, child: Text('Left align')),
Padding(padding: const EdgeInsets.all(5)),
Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: <Widget>[
Image(im),
PdfLogo(),
Column(children: <Widget>[
Text('(', style: symbol),
Text('4', style: symbol),
]),
]),
Padding(
padding: const EdgeInsets.only(left: 30, top: 20),
child: Lorem(textAlign: TextAlign.justify)),
Expanded(
child: Transform.scale(
child: Transform.rotate(
child: FittedBox(child: Text('Expanded')),
angle: 0.2),
scale: 0.9)),
Container(
padding: const EdgeInsets.only(top: 5),
decoration: const BoxDecoration(
border: BoxBorder(top: true, width: 1)),
child: Text("That's all Folks!",
textAlign: TextAlign.center,
style: Theme.of(context)
.defaultTextStyle
.copyWith(font: Font.timesBoldItalic()),
textScaleFactor: 3)),
])));
pdf.addPage(Page(
pageFormat: const PdfPageFormat(400, 400),
margin: const EdgeInsets.all(10),
build: (Context context) => Center(
child: GridView(
crossAxisCount: 3,
direction: Axis.vertical,
crossAxisSpacing: 10,
mainAxisSpacing: 10,
padding: const EdgeInsets.all(10),
children: List<Widget>.generate(
9, (int n) => FittedBox(child: Text('${n + 1}')))))));
pdf.addPage(MultiPage(
pageFormat: const PdfPageFormat(400, 200),
margin: const EdgeInsets.all(10),
build: (Context context) => <Widget>[
Table.fromTextArray(context: context, data: <List<String>>[
<String>['Company', 'Contact', 'Country'],
<String>['Alfreds Futterkiste', 'Maria Anders', 'Germany'],
<String>[
'Centro comercial Moctezuma',
'Francisco Chang',
'Mexico'
],
<String>['Ernst Handel', 'Roland Mendel', 'Austria'],
<String>['Island Trading', 'Helen Bennett', 'UK'],
<String>[
'Laughing Bacchus Winecellars',
'Yoshi Tannamuri',
'Canada'
],
<String>[
'Magazzini Alimentari Riuniti',
'Giovanni Rovelli',
'Italy'
],
<String>[
'Spaceage Stereo',
'Igor Cavalcanti Pereira',
'Brasil'
],
<String>['Team Uno', 'Frantisek Stefánek', 'Czech Republic'],
<String>["Isaly's", 'Michelle J. Kristensen', 'Danmark'],
<String>['Albers', 'Marjolaine Laramée', 'France'],
<String>['Dynatronics Accessories', "Cong Ch'en", 'China'],
<String>['York Steak House', 'Outi Vuorinen', 'Finland'],
<String>['Weathervane', 'Else Jeremiassen', 'Iceland'],
]),
]));
pdf.addPage(Page(
pageFormat: const PdfPageFormat(400, 200),
margin: const EdgeInsets.all(10),
build: (Context context) => Stack(overflow: Overflow.visible,
// fit: StackFit.expand,
// alignment: Alignment.bottomRight,
children: <Widget>[
Positioned(
right: 10,
top: 10,
child: CustomPaint(
size: const PdfPoint(50, 50),
painter: (PdfGraphics canvas, PdfPoint size) {
canvas
..setColor(PdfColor.indigo)
..drawRRect(0, 0, size.x, size.y, 10, 10)
..fillPath();
})),
Positioned(
left: 10,
bottom: 10,
child: RichText(
text: TextSpan(
text: 'Hello ',
style: Theme.of(context).defaultTextStyle,
children: <TextSpan>[
TextSpan(
text: 'bold',
style: Theme.of(context)
.defaultTextStyleBold
.copyWith(
fontSize: 20, color: PdfColor.blue)),
const TextSpan(
text: ' world!',
),
],
),
))
])));
final File file = File('widgets.pdf');
file.writeAsBytesSync(pdf.save());
});
}