Showing
2 changed files
with
59 additions
and
10 deletions
@@ -22,6 +22,7 @@ class PdfPreview extends StatefulWidget { | @@ -22,6 +22,7 @@ class PdfPreview extends StatefulWidget { | ||
22 | this.allowSharing = true, | 22 | this.allowSharing = true, |
23 | this.maxPageWidth, | 23 | this.maxPageWidth, |
24 | this.canChangePageFormat = true, | 24 | this.canChangePageFormat = true, |
25 | + this.canChangeOrientation = true, | ||
25 | this.actions, | 26 | this.actions, |
26 | this.pageFormats, | 27 | this.pageFormats, |
27 | this.onError, | 28 | this.onError, |
@@ -55,6 +56,9 @@ class PdfPreview extends StatefulWidget { | @@ -55,6 +56,9 @@ class PdfPreview extends StatefulWidget { | ||
55 | /// Add a drop-down menu to choose the page format | 56 | /// Add a drop-down menu to choose the page format |
56 | final bool canChangePageFormat; | 57 | final bool canChangePageFormat; |
57 | 58 | ||
59 | + /// Add a switch to change the page orientation | ||
60 | + final bool canChangeOrientation; | ||
61 | + | ||
58 | /// Additionnal actions to add to the widget | 62 | /// Additionnal actions to add to the widget |
59 | final List<PdfPreviewAction>? actions; | 63 | final List<PdfPreviewAction>? actions; |
60 | 64 | ||
@@ -92,7 +96,9 @@ class _PdfPreviewState extends State<PdfPreview> { | @@ -92,7 +96,9 @@ class _PdfPreviewState extends State<PdfPreview> { | ||
92 | 96 | ||
93 | final List<_PdfPreviewPage> pages = <_PdfPreviewPage>[]; | 97 | final List<_PdfPreviewPage> pages = <_PdfPreviewPage>[]; |
94 | 98 | ||
95 | - PdfPageFormat? pageFormat; | 99 | + late PdfPageFormat pageFormat; |
100 | + | ||
101 | + bool? horizontal; | ||
96 | 102 | ||
97 | PrintingInfo info = PrintingInfo.unavailable; | 103 | PrintingInfo info = PrintingInfo.unavailable; |
98 | bool infoLoaded = false; | 104 | bool infoLoaded = false; |
@@ -116,6 +122,10 @@ class _PdfPreviewState extends State<PdfPreview> { | @@ -116,6 +122,10 @@ class _PdfPreviewState extends State<PdfPreview> { | ||
116 | 'Letter': PdfPageFormat.letter, | 122 | 'Letter': PdfPageFormat.letter, |
117 | }; | 123 | }; |
118 | 124 | ||
125 | + PdfPageFormat get computedPageFormat => horizontal != null | ||
126 | + ? (horizontal! ? pageFormat.landscape : pageFormat.portrait) | ||
127 | + : pageFormat; | ||
128 | + | ||
119 | Future<void> _raster() async { | 129 | Future<void> _raster() async { |
120 | Uint8List _doc; | 130 | Uint8List _doc; |
121 | 131 | ||
@@ -124,7 +134,7 @@ class _PdfPreviewState extends State<PdfPreview> { | @@ -124,7 +134,7 @@ class _PdfPreviewState extends State<PdfPreview> { | ||
124 | } | 134 | } |
125 | 135 | ||
126 | try { | 136 | try { |
127 | - _doc = await widget.build(pageFormat!); | 137 | + _doc = await widget.build(computedPageFormat); |
128 | } catch (e) { | 138 | } catch (e) { |
129 | error = e; | 139 | error = e; |
130 | return; | 140 | return; |
@@ -178,7 +188,7 @@ class _PdfPreviewState extends State<PdfPreview> { | @@ -178,7 +188,7 @@ class _PdfPreviewState extends State<PdfPreview> { | ||
178 | pageFormat = PdfPageFormat.a4; | 188 | pageFormat = PdfPageFormat.a4; |
179 | } | 189 | } |
180 | } else { | 190 | } else { |
181 | - pageFormat = widget.initialPageFormat; | 191 | + pageFormat = widget.initialPageFormat!; |
182 | } | 192 | } |
183 | 193 | ||
184 | final _pageFormats = widget.pageFormats ?? defaultPageFormats; | 194 | final _pageFormats = widget.pageFormats ?? defaultPageFormats; |
@@ -223,7 +233,7 @@ class _PdfPreviewState extends State<PdfPreview> { | @@ -223,7 +233,7 @@ class _PdfPreviewState extends State<PdfPreview> { | ||
223 | final mq = MediaQuery.of(context); | 233 | final mq = MediaQuery.of(context); |
224 | dpi = (min(mq.size.width - 16, widget.maxPageWidth ?? double.infinity)) * | 234 | dpi = (min(mq.size.width - 16, widget.maxPageWidth ?? double.infinity)) * |
225 | mq.devicePixelRatio / | 235 | mq.devicePixelRatio / |
226 | - pageFormat!.width * | 236 | + computedPageFormat.width * |
227 | 72; | 237 | 72; |
228 | 238 | ||
229 | _raster(); | 239 | _raster(); |
@@ -384,13 +394,42 @@ class _PdfPreviewState extends State<PdfPreview> { | @@ -384,13 +394,42 @@ class _PdfPreviewState extends State<PdfPreview> { | ||
384 | ), | 394 | ), |
385 | onChanged: (PdfPageFormat? _pageFormat) { | 395 | onChanged: (PdfPageFormat? _pageFormat) { |
386 | setState(() { | 396 | setState(() { |
397 | + if (_pageFormat != null) { | ||
387 | pageFormat = _pageFormat; | 398 | pageFormat = _pageFormat; |
388 | _raster(); | 399 | _raster(); |
400 | + } | ||
401 | + }); | ||
402 | + }, | ||
403 | + ), | ||
404 | + ); | ||
405 | + | ||
406 | + if (widget.canChangeOrientation) { | ||
407 | + horizontal ??= pageFormat.width > pageFormat.height; | ||
408 | + final color = theme.accentIconTheme.color!; | ||
409 | + final disabledColor = color.withAlpha(120); | ||
410 | + actions.add( | ||
411 | + ToggleButtons( | ||
412 | + renderBorder: false, | ||
413 | + borderColor: disabledColor, | ||
414 | + color: disabledColor, | ||
415 | + selectedBorderColor: color, | ||
416 | + selectedColor: color, | ||
417 | + children: <Widget>[ | ||
418 | + Transform.rotate( | ||
419 | + angle: -pi / 2, child: const Icon(Icons.note_outlined)), | ||
420 | + const Icon(Icons.note_outlined), | ||
421 | + ], | ||
422 | + onPressed: (int index) { | ||
423 | + setState(() { | ||
424 | + horizontal = index == 1; | ||
425 | + _raster(); | ||
389 | }); | 426 | }); |
390 | }, | 427 | }, |
428 | + isSelected: <bool>[horizontal == false, horizontal == true], | ||
391 | ), | 429 | ), |
392 | ); | 430 | ); |
393 | } | 431 | } |
432 | + } | ||
394 | 433 | ||
395 | if (widget.actions != null) { | 434 | if (widget.actions != null) { |
396 | for (final action in widget.actions!) { | 435 | for (final action in widget.actions!) { |
@@ -403,7 +442,7 @@ class _PdfPreviewState extends State<PdfPreview> { | @@ -403,7 +442,7 @@ class _PdfPreviewState extends State<PdfPreview> { | ||
403 | : () => action.onPressed!( | 442 | : () => action.onPressed!( |
404 | context, | 443 | context, |
405 | widget.build, | 444 | widget.build, |
406 | - pageFormat!, | 445 | + computedPageFormat, |
407 | ), | 446 | ), |
408 | ), | 447 | ), |
409 | ); | 448 | ); |
@@ -439,19 +478,22 @@ class _PdfPreviewState extends State<PdfPreview> { | @@ -439,19 +478,22 @@ class _PdfPreviewState extends State<PdfPreview> { | ||
439 | Material( | 478 | Material( |
440 | elevation: 4, | 479 | elevation: 4, |
441 | color: theme.primaryColor, | 480 | color: theme.primaryColor, |
481 | + child: SizedBox( | ||
482 | + width: double.infinity, | ||
442 | child: SafeArea( | 483 | child: SafeArea( |
443 | - child: Row( | ||
444 | - mainAxisAlignment: MainAxisAlignment.spaceAround, | 484 | + child: Wrap( |
485 | + alignment: WrapAlignment.spaceAround, | ||
445 | children: actions, | 486 | children: actions, |
446 | ), | 487 | ), |
447 | ), | 488 | ), |
489 | + ), | ||
448 | ) | 490 | ) |
449 | ], | 491 | ], |
450 | ); | 492 | ); |
451 | } | 493 | } |
452 | 494 | ||
453 | Future<void> _print() async { | 495 | Future<void> _print() async { |
454 | - var format = pageFormat; | 496 | + var format = computedPageFormat; |
455 | 497 | ||
456 | if (!widget.canChangePageFormat && pages.isNotEmpty) { | 498 | if (!widget.canChangePageFormat && pages.isNotEmpty) { |
457 | format = PdfPageFormat( | 499 | format = PdfPageFormat( |
@@ -461,15 +503,21 @@ class _PdfPreviewState extends State<PdfPreview> { | @@ -461,15 +503,21 @@ class _PdfPreviewState extends State<PdfPreview> { | ||
461 | ); | 503 | ); |
462 | } | 504 | } |
463 | 505 | ||
506 | + try { | ||
464 | final result = await Printing.layoutPdf( | 507 | final result = await Printing.layoutPdf( |
465 | onLayout: widget.build, | 508 | onLayout: widget.build, |
466 | name: widget.pdfFileName ?? 'Document', | 509 | name: widget.pdfFileName ?? 'Document', |
467 | - format: format!, | 510 | + format: format, |
468 | ); | 511 | ); |
469 | 512 | ||
470 | if (result && widget.onPrinted != null) { | 513 | if (result && widget.onPrinted != null) { |
471 | widget.onPrinted!(context); | 514 | widget.onPrinted!(context); |
472 | } | 515 | } |
516 | + } catch (e) { | ||
517 | + if (widget.onError != null) { | ||
518 | + widget.onError!(context); | ||
519 | + } | ||
520 | + } | ||
473 | } | 521 | } |
474 | 522 | ||
475 | Future<void> _share() async { | 523 | Future<void> _share() async { |
@@ -483,7 +531,7 @@ class _PdfPreviewState extends State<PdfPreview> { | @@ -483,7 +531,7 @@ class _PdfPreviewState extends State<PdfPreview> { | ||
483 | referenceBox.localToGlobal(referenceBox.paintBounds.bottomRight); | 531 | referenceBox.localToGlobal(referenceBox.paintBounds.bottomRight); |
484 | final bounds = Rect.fromPoints(topLeft, bottomRight); | 532 | final bounds = Rect.fromPoints(topLeft, bottomRight); |
485 | 533 | ||
486 | - final bytes = await widget.build(pageFormat!); | 534 | + final bytes = await widget.build(computedPageFormat); |
487 | final result = await Printing.sharePdf( | 535 | final result = await Printing.sharePdf( |
488 | bytes: bytes, | 536 | bytes: bytes, |
489 | bounds: bounds, | 537 | bounds: bounds, |
-
Please register or login to post a comment