David PHAM-VAN

Convert print screen example to Widgets

1 # 2.0.3 1 # 2.0.3
2 * Add file save and view to example application 2 * Add file save and view to example application
  3 +* Convert print screen example to Widgets
3 4
4 # 2.0.2 5 # 2.0.2
5 * Fix example application 6 * Fix example application
@@ -73,41 +73,24 @@ class MyAppState extends State<MyApp> { @@ -73,41 +73,24 @@ class MyAppState extends State<MyApp> {
73 print('Print Screen ${im.width}x${im.height} ...'); 73 print('Print Screen ${im.width}x${im.height} ...');
74 74
75 Printing.layoutPdf(onLayout: (PdfPageFormat format) { 75 Printing.layoutPdf(onLayout: (PdfPageFormat format) {
76 - final PdfDocument pdf = PdfDocument(deflate: zlib.encode);  
77 - final PdfPage page = PdfPage(pdf, pageFormat: format);  
78 - final PdfGraphics g = page.getGraphics();  
79 -  
80 - // Center the image  
81 - final double w = page.pageFormat.width -  
82 - page.pageFormat.marginLeft -  
83 - page.pageFormat.marginRight;  
84 - final double h = page.pageFormat.height -  
85 - page.pageFormat.marginTop -  
86 - page.pageFormat.marginBottom;  
87 - double iw, ih;  
88 - if (im.width.toDouble() / im.height.toDouble() < 1.0) {  
89 - ih = h;  
90 - iw = im.width.toDouble() * ih / im.height.toDouble();  
91 - } else {  
92 - iw = w;  
93 - ih = im.height.toDouble() * iw / im.width.toDouble();  
94 - } 76 + final pdf.Document document = PdfDoc();
95 77
96 - final PdfImage image = PdfImage(pdf, 78 + final image = PdfImage(document.document,
97 image: bytes.buffer.asUint8List(), 79 image: bytes.buffer.asUint8List(),
98 width: im.width, 80 width: im.width,
99 height: im.height); 81 height: im.height);
100 - g.drawImage(  
101 - image,  
102 - page.pageFormat.marginLeft + (w - iw) / 2.0,  
103 - page.pageFormat.height -  
104 - page.pageFormat.marginTop -  
105 - ih -  
106 - (h - ih) / 2.0,  
107 - iw,  
108 - ih);  
109 -  
110 - return pdf.save(); 82 +
  83 + document.addPage(pdf.Page(
  84 + pageFormat: format,
  85 + build: (pdf.Context context) {
  86 + return pdf.Center(
  87 + child: pdf.Expanded(
  88 + child: pdf.Image(image),
  89 + ),
  90 + ); // Center
  91 + })); // Page
  92 +
  93 + return document.save();
111 }); 94 });
112 } 95 }
113 96