David PHAM-VAN

Add WidgetWraper ImageProvider

1 # Changelog 1 # Changelog
2 2
3 -## 4.0.1 3 +## 4.1.0
4 4
5 - Remove useless files 5 - Remove useless files
  6 +- Add WidgetWraper ImageProvider and deprecate wrapWidget()
6 7
7 ## 4.0.0 8 ## 4.0.0
8 9
@@ -15,13 +15,16 @@ @@ -15,13 +15,16 @@
15 */ 15 */
16 16
17 import 'dart:async'; 17 import 'dart:async';
  18 +import 'dart:typed_data';
18 import 'dart:ui' as ui; 19 import 'dart:ui' as ui;
19 20
20 import 'package:flutter/material.dart'; 21 import 'package:flutter/material.dart';
21 import 'package:flutter/rendering.dart'; 22 import 'package:flutter/rendering.dart';
22 import 'package:pdf/pdf.dart'; 23 import 'package:pdf/pdf.dart';
  24 +import 'package:pdf/widgets.dart' as pw;
23 25
24 /// Wrap a Flutter Widget identified by a GlobalKey to a PdfImage. 26 /// Wrap a Flutter Widget identified by a GlobalKey to a PdfImage.
  27 +@Deprecated('Use WidgetWraper.fromKey() instead')
25 Future<PdfImage> wrapWidget( 28 Future<PdfImage> wrapWidget(
26 PdfDocument document, { 29 PdfDocument document, {
27 @required GlobalKey key, 30 @required GlobalKey key,
@@ -40,3 +43,85 @@ Future<PdfImage> wrapWidget( @@ -40,3 +43,85 @@ Future<PdfImage> wrapWidget(
40 return PdfImage(document, 43 return PdfImage(document,
41 image: imageData, width: image.width, height: image.height); 44 image: imageData, width: image.width, height: image.height);
42 } 45 }
  46 +
  47 +/// ImageProvider that draws a Flutter Widget on a PDF document
  48 +class WidgetWraper extends pw.ImageProvider {
  49 + WidgetWraper._(
  50 + this.bytes,
  51 + int width,
  52 + int height,
  53 + PdfImageOrientation orientation,
  54 + double dpi,
  55 + ) : super(width, height, orientation, dpi);
  56 +
  57 + /// Wrap a Flutter Widget identified by a GlobalKey to an ImageProvider.
  58 + ///
  59 + /// Use it with a RepaintBoundary:
  60 + /// ```
  61 + /// final rb = GlobalKey();
  62 + ///
  63 + /// @override
  64 + /// Widget build(BuildContext context) {
  65 + /// return RepaintBoundary(
  66 + /// key: rb,
  67 + /// child: FlutterLogo()
  68 + /// );
  69 + /// }
  70 + ///
  71 + /// Future<Uint8List> _generatePdf(PdfPageFormat format) async {
  72 + /// final pdf = pw.Document();
  73 + ///
  74 + /// final image = await WidgetWraper.fromKey(key: rb);
  75 + ///
  76 + /// pdf.addPage(
  77 + /// pw.Page(
  78 + /// build: (context) {
  79 + /// return pw.Center(
  80 + /// child: pw.Image(image),
  81 + /// );
  82 + /// },
  83 + /// ),
  84 + /// );
  85 + ///
  86 + /// return pdf.save();
  87 + /// }
  88 + /// ```
  89 + static Future<WidgetWraper> fromKey({
  90 + @required GlobalKey key,
  91 + int width,
  92 + int height,
  93 + double pixelRatio = 1.0,
  94 + PdfImageOrientation orientation,
  95 + double dpi,
  96 + }) async {
  97 + assert(key != null);
  98 + assert(pixelRatio != null && pixelRatio > 0);
  99 +
  100 + final RenderRepaintBoundary wrappedWidget =
  101 + key.currentContext.findRenderObject();
  102 + final image = await wrappedWidget.toImage(pixelRatio: pixelRatio);
  103 + final byteData = await image.toByteData(format: ui.ImageByteFormat.rawRgba);
  104 + final imageData = byteData.buffer.asUint8List();
  105 + return WidgetWraper._(
  106 + imageData,
  107 + image.width,
  108 + image.height,
  109 + orientation ?? PdfImageOrientation.topLeft,
  110 + dpi,
  111 + );
  112 + }
  113 +
  114 + /// The image data
  115 + final Uint8List bytes;
  116 +
  117 + @override
  118 + PdfImage buildImage(pw.Context context, {int width, int height}) {
  119 + return PdfImage(
  120 + context.document,
  121 + image: bytes,
  122 + width: width ?? this.width,
  123 + height: height ?? this.height,
  124 + orientation: orientation,
  125 + );
  126 + }
  127 +}
@@ -4,7 +4,7 @@ description: Plugin that allows Flutter apps to generate and print documents to @@ -4,7 +4,7 @@ description: Plugin that allows Flutter apps to generate and print documents to
4 homepage: https://github.com/DavBfr/dart_pdf/tree/master/printing 4 homepage: https://github.com/DavBfr/dart_pdf/tree/master/printing
5 repository: https://github.com/DavBfr/dart_pdf 5 repository: https://github.com/DavBfr/dart_pdf
6 issue_tracker: https://github.com/DavBfr/dart_pdf/issues 6 issue_tracker: https://github.com/DavBfr/dart_pdf/issues
7 -version: 4.0.1 7 +version: 4.1.0
8 8
9 environment: 9 environment:
10 sdk: ">=2.3.0 <3.0.0" 10 sdk: ">=2.3.0 <3.0.0"