Showing
2 changed files
with
23 additions
and
14 deletions
@@ -4,7 +4,7 @@ | @@ -4,7 +4,7 @@ | ||
4 | import 'package:flutter/material.dart'; | 4 | import 'package:flutter/material.dart'; |
5 | 5 | ||
6 | import 'package:pdf/pdf.dart'; | 6 | import 'package:pdf/pdf.dart'; |
7 | -import 'package:pdf/widgets.dart' as pdf; | 7 | +import 'package:pdf/widgets.dart' as pw; |
8 | import 'package:printing/printing.dart'; | 8 | import 'package:printing/printing.dart'; |
9 | 9 | ||
10 | void main() => runApp(MyApp()); | 10 | void main() => runApp(MyApp()); |
@@ -15,43 +15,51 @@ class MyApp extends StatelessWidget { | @@ -15,43 +15,51 @@ class MyApp extends StatelessWidget { | ||
15 | return MaterialApp( | 15 | return MaterialApp( |
16 | home: Scaffold( | 16 | home: Scaffold( |
17 | appBar: AppBar( | 17 | appBar: AppBar( |
18 | - title: const Text('Printing Demo'), | 18 | + title: Text('Printing Demo'), |
19 | ), | 19 | ), |
20 | floatingActionButton: FloatingActionButton( | 20 | floatingActionButton: FloatingActionButton( |
21 | - child: const Icon(Icons.print), | 21 | + child: Icon(Icons.print), |
22 | tooltip: 'Print Document', | 22 | tooltip: 'Print Document', |
23 | onPressed: () { | 23 | onPressed: () { |
24 | + // This is where we print the document | ||
24 | Printing.layoutPdf( | 25 | Printing.layoutPdf( |
25 | - onLayout: buildPdf, | 26 | + // [onLayout] will be called multiple times |
27 | + // when the user changes the printer or printer settings | ||
28 | + onLayout: (PdfPageFormat format) { | ||
29 | + // Any valid Pdf document can be returned here as a list of int | ||
30 | + return buildPdf(format); | ||
31 | + }, | ||
26 | ); | 32 | ); |
27 | }, | 33 | }, |
28 | ), | 34 | ), |
29 | body: Center( | 35 | body: Center( |
30 | - child: const Text('Click on the print button below'), | 36 | + child: Text('Click on the print button below'), |
31 | ), | 37 | ), |
32 | ), | 38 | ), |
33 | ); | 39 | ); |
34 | } | 40 | } |
35 | 41 | ||
42 | + /// This method takes a page format and generates the Pdf file data | ||
36 | List<int> buildPdf(PdfPageFormat format) { | 43 | List<int> buildPdf(PdfPageFormat format) { |
37 | - final Document doc = Document(); | 44 | + // Create the Pdf document |
45 | + final pw.Document doc = pw.Document(); | ||
38 | 46 | ||
47 | + // Add one page with centered text "Hello World" | ||
39 | doc.addPage( | 48 | doc.addPage( |
40 | - pdf.Page( | 49 | + pw.Page( |
41 | pageFormat: format, | 50 | pageFormat: format, |
42 | - build: (pdf.Context context) { | ||
43 | - return pdf.ConstrainedBox( | ||
44 | - constraints: const pdf.BoxConstraints.expand(), | ||
45 | - child: pdf.FittedBox( | ||
46 | - child: pdf.Text( | ||
47 | - 'Hello World', | ||
48 | - ), | 51 | + build: (pw.Context context) { |
52 | + return pw.ConstrainedBox( | ||
53 | + constraints: pw.BoxConstraints.expand(), | ||
54 | + child: pw.FittedBox( | ||
55 | + child: pw.Text('Hello World'), | ||
49 | ), | 56 | ), |
50 | ); | 57 | ); |
51 | }, | 58 | }, |
52 | ), | 59 | ), |
53 | ); | 60 | ); |
54 | 61 | ||
62 | + // Build and return the final Pdf file data | ||
55 | return doc.save(); | 63 | return doc.save(); |
56 | } | 64 | } |
57 | } | 65 | } |
-
Please register or login to post a comment