David PHAM-VAN

Use the Barcode library to generate QR-Codes

1 # Changelog 1 # Changelog
2 2
  3 +## 1.6.2
  4 +
  5 +- Use the Barcode library to generate QR-Codes
  6 +
3 ## 1.6.1 7 ## 1.6.1
4 8
5 - Fix Image width and height attributes 9 - Fix Image width and height attributes
@@ -23,9 +23,10 @@ import 'dart:typed_data'; @@ -23,9 +23,10 @@ import 'dart:typed_data';
23 import 'package:barcode/barcode.dart'; 23 import 'package:barcode/barcode.dart';
24 import 'package:meta/meta.dart'; 24 import 'package:meta/meta.dart';
25 import 'package:pdf/pdf.dart'; 25 import 'package:pdf/pdf.dart';
26 -import 'package:qr/qr.dart';  
27 import 'package:vector_math/vector_math_64.dart'; 26 import 'package:vector_math/vector_math_64.dart';
28 27
  28 +export 'package:barcode/barcode.dart';
  29 +
29 part 'widgets/annotations.dart'; 30 part 'widgets/annotations.dart';
30 part 'widgets/barcode.dart'; 31 part 'widgets/barcode.dart';
31 part 'widgets/basic.dart'; 32 part 'widgets/basic.dart';
@@ -20,98 +20,12 @@ part of widget; @@ -20,98 +20,12 @@ part of widget;
20 20
21 typedef QrError = void Function(dynamic error); 21 typedef QrError = void Function(dynamic error);
22 22
23 -class _QrCodeWidget extends Widget {  
24 - _QrCodeWidget({  
25 - @required String data,  
26 - this.version,  
27 - this.errorCorrectionLevel,  
28 - this.color,  
29 - this.onError,  
30 - this.gapless = false,  
31 - }) : assert(data != null),  
32 - _qr = version == null  
33 - ? QrCode.fromData(  
34 - data: data,  
35 - errorCorrectLevel: errorCorrectionLevel,  
36 - )  
37 - : QrCode(  
38 - version,  
39 - errorCorrectionLevel,  
40 - ) {  
41 - // configure and make the QR code data  
42 - try {  
43 - if (version != null) {  
44 - _qr.addData(data);  
45 - }  
46 - _qr.make();  
47 - } catch (ex) {  
48 - if (onError != null) {  
49 - _hasError = true;  
50 - onError(ex);  
51 - }  
52 - }  
53 - }  
54 -  
55 - @override  
56 - void layout(Context context, BoxConstraints constraints,  
57 - {bool parentUsesSize = false}) {  
58 - box = PdfRect.fromPoints(PdfPoint.zero, constraints.biggest);  
59 - }  
60 -  
61 - /// the qr code version  
62 - final int version;  
63 -  
64 - /// the qr code error correction level  
65 - final int errorCorrectionLevel;  
66 -  
67 - /// the color of the dark squares  
68 - final PdfColor color;  
69 -  
70 - final QrError onError;  
71 -  
72 - final bool gapless;  
73 -  
74 - // our qr code data  
75 - final QrCode _qr;  
76 -  
77 - bool _hasError = false;  
78 -  
79 - @override  
80 - void paint(Context context) {  
81 - super.paint(context);  
82 -  
83 - if (_hasError) {  
84 - return;  
85 - }  
86 -  
87 - final double shortestSide = box.width < box.height ? box.width : box.height;  
88 - assert(shortestSide > 0);  
89 -  
90 - context.canvas.setFillColor(color);  
91 - final double squareSize = shortestSide / _qr.moduleCount.toDouble();  
92 - final int pxAdjustValue = gapless ? 1 : 0;  
93 - for (int x = 0; x < _qr.moduleCount; x++) {  
94 - for (int y = 0; y < _qr.moduleCount; y++) {  
95 - if (_qr.isDark(y, x)) {  
96 - context.canvas.drawRect(  
97 - box.left + x * squareSize,  
98 - box.top - (y + 1) * squareSize,  
99 - squareSize + pxAdjustValue,  
100 - squareSize + pxAdjustValue,  
101 - );  
102 - }  
103 - }  
104 - }  
105 -  
106 - context.canvas.fillPath();  
107 - }  
108 -}  
109 - 23 +@Deprecated('Use BarcodeWidget instead')
110 class QrCodeWidget extends StatelessWidget { 24 class QrCodeWidget extends StatelessWidget {
111 QrCodeWidget({ 25 QrCodeWidget({
112 @required this.data, 26 @required this.data,
113 this.version, 27 this.version,
114 - this.errorCorrectionLevel = QrErrorCorrectLevel.L, 28 + this.errorCorrectionLevel = BarcodeQRCorrectionLevel.low,
115 this.color = PdfColors.black, 29 this.color = PdfColors.black,
116 this.backgroundColor, 30 this.backgroundColor,
117 this.decoration, 31 this.decoration,
@@ -129,7 +43,7 @@ class QrCodeWidget extends StatelessWidget { @@ -129,7 +43,7 @@ class QrCodeWidget extends StatelessWidget {
129 final int version; 43 final int version;
130 44
131 /// the qr code error correction level 45 /// the qr code error correction level
132 - final int errorCorrectionLevel; 46 + final BarcodeQRCorrectionLevel errorCorrectionLevel;
133 47
134 /// the color of the dark squares 48 /// the color of the dark squares
135 final PdfColor color; 49 final PdfColor color;
@@ -150,41 +64,19 @@ class QrCodeWidget extends StatelessWidget { @@ -150,41 +64,19 @@ class QrCodeWidget extends StatelessWidget {
150 64
151 @override 65 @override
152 Widget build(Context context) { 66 Widget build(Context context) {
153 - Widget qrcode = AspectRatio(  
154 - aspectRatio: 1.0,  
155 - child: _QrCodeWidget( 67 + return BarcodeWidget(
  68 + barcode: Barcode.qrCode(
  69 + typeNumber: version,
  70 + errorCorrectLevel: errorCorrectionLevel,
  71 + ),
156 data: data, 72 data: data,
157 - version: version,  
158 - errorCorrectionLevel: errorCorrectionLevel, 73 + backgroundColor: backgroundColor,
159 color: color, 74 color: color,
160 - onError: onError,  
161 - gapless: gapless,  
162 - ));  
163 -  
164 - if (padding != null) {  
165 - qrcode = Padding(padding: padding, child: qrcode);  
166 - }  
167 -  
168 - if (decoration != null) {  
169 - qrcode = DecoratedBox(  
170 decoration: decoration, 75 decoration: decoration,
171 - child: qrcode,  
172 - );  
173 - } else if (backgroundColor != null) {  
174 - qrcode = DecoratedBox(  
175 - decoration: BoxDecoration(color: backgroundColor),  
176 - child: qrcode, 76 + width: size,
  77 + height: size,
  78 + margin: margin,
  79 + padding: padding,
177 ); 80 );
178 } 81 }
179 -  
180 - if (size != null) {  
181 - qrcode = SizedBox(width: size, height: size, child: qrcode);  
182 - }  
183 -  
184 - if (margin != null) {  
185 - qrcode = Padding(padding: margin, child: qrcode);  
186 - }  
187 -  
188 - return qrcode;  
189 - }  
190 } 82 }
@@ -4,7 +4,7 @@ description: A pdf producer for Dart. It can create pdf files for both web or fl @@ -4,7 +4,7 @@ description: A pdf producer for Dart. It can create pdf files for both web or fl
4 homepage: https://github.com/DavBfr/dart_pdf/tree/master/pdf 4 homepage: https://github.com/DavBfr/dart_pdf/tree/master/pdf
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: 1.6.1 7 +version: 1.6.2
8 8
9 environment: 9 environment:
10 sdk: ">=2.3.0 <3.0.0" 10 sdk: ">=2.3.0 <3.0.0"
@@ -15,8 +15,7 @@ dependencies: @@ -15,8 +15,7 @@ dependencies:
15 utf: ^0.9.0 15 utf: ^0.9.0
16 crypto: ^2.0.6 16 crypto: ^2.0.6
17 archive: ^2.0.10 17 archive: ^2.0.10
18 - qr: ^1.2.0  
19 - barcode: ^1.5.0 18 + barcode: ^1.7.0
20 image: ^2.1.4 19 image: ^2.1.4
21 20
22 dev_dependencies: 21 dev_dependencies:
@@ -18,14 +18,17 @@ @@ -18,14 +18,17 @@
18 18
19 import 'dart:io'; 19 import 'dart:io';
20 20
21 -import 'package:barcode/barcode.dart';  
22 -import 'package:pdf/pdf.dart';  
23 import 'package:pdf/widgets.dart'; 21 import 'package:pdf/widgets.dart';
24 import 'package:test/test.dart'; 22 import 'package:test/test.dart';
25 23
26 Document pdf; 24 Document pdf;
27 25
28 -Widget barcode(Barcode barcode, String data, {double width = 200}) { 26 +Widget barcode(
  27 + Barcode barcode,
  28 + String data, {
  29 + double width = 200,
  30 + double height = 80,
  31 +}) {
29 return Row( 32 return Row(
30 mainAxisAlignment: MainAxisAlignment.spaceEvenly, 33 mainAxisAlignment: MainAxisAlignment.spaceEvenly,
31 children: <Widget>[ 34 children: <Widget>[
@@ -42,7 +45,7 @@ Widget barcode(Barcode barcode, String data, {double width = 200}) { @@ -42,7 +45,7 @@ Widget barcode(Barcode barcode, String data, {double width = 200}) {
42 barcode: barcode, 45 barcode: barcode,
43 data: data, 46 data: data,
44 width: width, 47 width: width,
45 - height: 80, 48 + height: height,
46 margin: const EdgeInsets.symmetric(vertical: 20), 49 margin: const EdgeInsets.symmetric(vertical: 20),
47 ), 50 ),
48 ), 51 ),
@@ -72,34 +75,13 @@ void main() { @@ -72,34 +75,13 @@ void main() {
72 barcode(Barcode.ean2(), '44', width: 40), 75 barcode(Barcode.ean2(), '44', width: 40),
73 barcode(Barcode.ean5(), '30897', width: 60), 76 barcode(Barcode.ean5(), '30897', width: 60),
74 barcode(Barcode.itf14(), '2578639587234'), 77 barcode(Barcode.itf14(), '2578639587234'),
  78 + barcode(Barcode.telepen(), 'Telepen'),
  79 + barcode(Barcode.qrCode(), 'QR-Code!', width: 120, height: 120),
75 ], 80 ],
76 ), 81 ),
77 ); 82 );
78 }); 83 });
79 84
80 - test('QrCode Widgets', () {  
81 - pdf.addPage(  
82 - Page(  
83 - build: (Context context) => QrCodeWidget(  
84 - data: 'HELLO 123',  
85 - size: 200,  
86 - padding: const EdgeInsets.all(20),  
87 - margin: const EdgeInsets.all(20),  
88 - decoration: const BoxDecoration(  
89 - borderRadius: 20,  
90 - color: PdfColors.white,  
91 - border: BoxBorder(  
92 - color: PdfColors.blue,  
93 - top: true,  
94 - bottom: true,  
95 - left: true,  
96 - right: true,  
97 - )),  
98 - ),  
99 - ),  
100 - );  
101 - });  
102 -  
103 tearDownAll(() { 85 tearDownAll(() {
104 final File file = File('widgets-barcode.pdf'); 86 final File file = File('widgets-barcode.pdf');
105 file.writeAsBytesSync(pdf.save()); 87 file.writeAsBytesSync(pdf.save());
@@ -100,7 +100,12 @@ Future<pw.Document> generateDocument(PdfPageFormat format) async { @@ -100,7 +100,12 @@ Future<pw.Document> generateDocument(PdfPageFormat format) async {
100 Percent(size: 60, value: .7, title: pw.Text('Word')), 100 Percent(size: 60, value: .7, title: pw.Text('Word')),
101 Percent(size: 60, value: .4, title: pw.Text('Excel')), 101 Percent(size: 60, value: .4, title: pw.Text('Excel')),
102 ]), 102 ]),
103 - pw.QrCodeWidget(data: 'Parnella Charlesbois', size: 60), 103 + pw.BarcodeWidget(
  104 + data: 'Parnella Charlesbois',
  105 + width: 60,
  106 + height: 60,
  107 + barcode: pw.Barcode.qrCode(),
  108 + ),
104 ], 109 ],
105 ) 110 )
106 ]), 111 ]),