custom.dart
8.02 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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
/*
* 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:async';
import 'package:flutter/material.dart';
import 'package:pdf/pdf.dart';
import '../callback.dart';
import '../printing.dart';
import '../printing_info.dart';
import 'page.dart';
import 'raster.dart';
/// Custom widget builder that's used for custom
/// rasterized pdf pages rendering
typedef CustomPdfPagesBuilder = Widget Function(
BuildContext context, List<PdfPreviewPageData> pages);
/// Flutter widget that uses the rasterized pdf pages to display a document.
class PdfPreviewCustom extends StatefulWidget {
/// Show a pdf document built on demand
const PdfPreviewCustom({
Key? key,
this.pageFormat = PdfPageFormat.a4,
required this.build,
this.maxPageWidth,
this.onError,
this.scrollViewDecoration,
this.pdfPreviewPageDecoration,
this.pages,
this.previewPageMargin,
this.padding,
this.shouldRepaint = false,
this.loadingWidget,
this.dpi,
this.scrollPhysics,
this.shrinkWrap = false,
this.pagesBuilder,
}) : super(key: key);
/// Pdf paper page format
final PdfPageFormat pageFormat;
/// Called when a pdf document is needed
final LayoutCallback build;
/// Maximum width of the pdf document on screen
final double? maxPageWidth;
/// Widget to display if the PDF document cannot be displayed
final Widget Function(BuildContext context, Object error)? onError;
/// Decoration of scrollView
final Decoration? scrollViewDecoration;
/// Whether the scrollView should be shrinkwrapped
final bool shrinkWrap;
/// The physics for the scrollView - e.g. use this to disable scrolling inside a scrollable
final ScrollPhysics? scrollPhysics;
/// Decoration of PdfPreviewPage
final Decoration? pdfPreviewPageDecoration;
/// Pages to display. Default will display all the pages.
final List<int>? pages;
/// margin for the document preview page
///
/// defaults to [EdgeInsets.only(left: 20, top: 8, right: 20, bottom: 12)],
final EdgeInsets? previewPageMargin;
/// padding for the pdf_preview widget
final EdgeInsets? padding;
/// Force repainting the PDF document
final bool shouldRepaint;
/// Custom loading widget to use that is shown while PDF is being generated.
/// If null, a [CircularProgressIndicator] is used instead.
final Widget? loadingWidget;
/// The rendering dots per inch resolution
/// If not provided, this value is calculated.
final double? dpi;
/// clients can pass this builder to render
/// their own pages.
final CustomPdfPagesBuilder? pagesBuilder;
@override
PdfPreviewCustomState createState() => PdfPreviewCustomState();
}
class PdfPreviewCustomState extends State<PdfPreviewCustom>
with PdfPreviewRaster {
final listView = GlobalKey();
final _pageGlobalKeys = List.generate(100, (index) => GlobalKey());
bool infoLoaded = false;
int? preview;
double? updatePosition;
final scrollController = ScrollController(
keepScrollOffset: true,
);
final transformationController = TransformationController();
Timer? previewUpdate;
static const _errorMessage = 'Unable to display the document';
@override
double? get forcedDpi => widget.dpi;
@override
void dispose() {
previewUpdate?.cancel();
super.dispose();
}
@override
void reassemble() {
raster();
super.reassemble();
}
@override
void didUpdateWidget(covariant PdfPreviewCustom oldWidget) {
if (oldWidget.build != widget.build ||
widget.shouldRepaint ||
widget.pageFormat != oldWidget.pageFormat) {
preview = null;
updatePosition = null;
raster();
}
super.didUpdateWidget(oldWidget);
}
@override
void didChangeDependencies() {
if (!infoLoaded) {
infoLoaded = true;
Printing.info().then((PrintingInfo printingInfo) {
if (!mounted) {
return;
}
setState(() {
info = printingInfo;
raster();
});
});
}
raster();
super.didChangeDependencies();
}
/// Ensures that page with [index] is become visible.
Future<void> scrollToPage(
int index, {
Duration duration = const Duration(milliseconds: 300),
Curve curve = Curves.ease,
ScrollPositionAlignmentPolicy alignmentPolicy =
ScrollPositionAlignmentPolicy.explicit,
}) {
assert(index >= 0, 'Index of page cannot be negative');
final pageContext = _pageGlobalKeys[index].currentContext;
assert(pageContext != null, 'Context of GlobalKey cannot be null');
return Scrollable.ensureVisible(pageContext!,
duration: duration, curve: curve, alignmentPolicy: alignmentPolicy);
}
/// Returns the global key for page with [index].
Key getPageKey(int index) => _pageGlobalKeys[index];
Widget _showError(Object error) {
if (widget.onError != null) {
return widget.onError!(context, error);
}
return ErrorWidget(error);
}
Widget _createPreview() {
if (error != null) {
return _showError(error!);
}
final printingInfo = info;
if (printingInfo != null && !printingInfo.canRaster) {
return _showError(_errorMessage);
}
if (pages.isEmpty) {
return widget.loadingWidget ??
const Center(
child: CircularProgressIndicator(),
);
}
if (widget.pagesBuilder != null) {
return widget.pagesBuilder!(context, pages);
}
return ListView.builder(
controller: scrollController,
shrinkWrap: widget.shrinkWrap,
physics: widget.scrollPhysics,
padding: widget.padding,
itemCount: pages.length,
itemBuilder: (BuildContext context, int index) => GestureDetector(
onDoubleTap: () {
setState(() {
updatePosition = scrollController.position.pixels;
preview = index;
transformationController.value.setIdentity();
});
},
child: PdfPreviewPage(
key: getPageKey(index),
pageData: pages[index],
pdfPreviewPageDecoration: widget.pdfPreviewPageDecoration,
pageMargin: widget.previewPageMargin,
),
),
);
}
Widget _zoomPreview() {
return GestureDetector(
onDoubleTap: () {
setState(() {
preview = null;
});
},
child: InteractiveViewer(
transformationController: transformationController,
maxScale: 5,
child: Center(
child: PdfPreviewPage(
pageData: pages[preview!],
pdfPreviewPageDecoration: widget.pdfPreviewPageDecoration,
pageMargin: widget.previewPageMargin,
),
),
),
);
}
@override
Widget build(BuildContext context) {
Widget page;
if (preview != null) {
page = _zoomPreview();
} else {
page = Container(
constraints: widget.maxPageWidth != null
? BoxConstraints(maxWidth: widget.maxPageWidth!)
: null,
child: _createPreview(),
);
if (updatePosition != null) {
Timer.run(() {
scrollController.jumpTo(updatePosition!);
updatePosition = null;
});
}
}
return Container(
decoration: widget.scrollViewDecoration ??
BoxDecoration(
gradient: LinearGradient(
colors: <Color>[Colors.grey.shade400, Colors.grey.shade200],
begin: Alignment.topCenter,
end: Alignment.bottomCenter,
),
),
width: double.infinity,
alignment: Alignment.center,
child: page,
);
}
}