Showing
6 changed files
with
134 additions
and
47 deletions
@@ -20,10 +20,10 @@ import 'dart:collection'; | @@ -20,10 +20,10 @@ import 'dart:collection'; | ||
20 | import 'dart:math' as math; | 20 | import 'dart:math' as math; |
21 | import 'dart:typed_data'; | 21 | import 'dart:typed_data'; |
22 | 22 | ||
23 | +import 'package:barcode/barcode.dart'; | ||
23 | import 'package:meta/meta.dart'; | 24 | import 'package:meta/meta.dart'; |
24 | import 'package:pdf/pdf.dart'; | 25 | import 'package:pdf/pdf.dart'; |
25 | import 'package:qr/qr.dart'; | 26 | import 'package:qr/qr.dart'; |
26 | -import 'package:barcode/barcode.dart'; | ||
27 | import 'package:vector_math/vector_math_64.dart'; | 27 | import 'package:vector_math/vector_math_64.dart'; |
28 | 28 | ||
29 | part 'widgets/annotations.dart'; | 29 | part 'widgets/annotations.dart'; |
@@ -23,6 +23,8 @@ class _BarcodeWidget extends Widget { | @@ -23,6 +23,8 @@ class _BarcodeWidget extends Widget { | ||
23 | @required this.data, | 23 | @required this.data, |
24 | this.barcode, | 24 | this.barcode, |
25 | this.color = PdfColors.black, | 25 | this.color = PdfColors.black, |
26 | + this.drawText, | ||
27 | + this.textStyle, | ||
26 | }); | 28 | }); |
27 | 29 | ||
28 | /// the barcode data | 30 | /// the barcode data |
@@ -32,6 +34,10 @@ class _BarcodeWidget extends Widget { | @@ -32,6 +34,10 @@ class _BarcodeWidget extends Widget { | ||
32 | 34 | ||
33 | final PdfColor color; | 35 | final PdfColor color; |
34 | 36 | ||
37 | + final bool drawText; | ||
38 | + | ||
39 | + final TextStyle textStyle; | ||
40 | + | ||
35 | @override | 41 | @override |
36 | void layout(Context context, BoxConstraints constraints, | 42 | void layout(Context context, BoxConstraints constraints, |
37 | {bool parentUsesSize = false}) { | 43 | {bool parentUsesSize = false}) { |
@@ -42,30 +48,87 @@ class _BarcodeWidget extends Widget { | @@ -42,30 +48,87 @@ class _BarcodeWidget extends Widget { | ||
42 | void paint(Context context) { | 48 | void paint(Context context) { |
43 | super.paint(context); | 49 | super.paint(context); |
44 | 50 | ||
45 | - final BarcodeDraw draw = barcode.draw; | ||
46 | - if (draw is _BarcodeDraw) { | ||
47 | - draw | ||
48 | - ..canvas = context.canvas | ||
49 | - ..left = box.left | ||
50 | - ..top = box.top; | 51 | + final List<BarcodeText> textList = <BarcodeText>[]; |
52 | + | ||
53 | + for (BarcodeElement element in barcode.make( | ||
54 | + data, | ||
55 | + width: box.width, | ||
56 | + height: box.height, | ||
57 | + drawText: drawText, | ||
58 | + fontHeight: textStyle.fontSize, | ||
59 | + )) { | ||
60 | + if (element is BarcodeBar) { | ||
61 | + if (element.black) { | ||
62 | + context.canvas.drawRect( | ||
63 | + box.left + element.left, | ||
64 | + box.top + element.top - element.height, | ||
65 | + element.width, | ||
66 | + element.height, | ||
67 | + ); | ||
68 | + } | ||
69 | + } else if (element is BarcodeText) { | ||
70 | + textList.add(element); | ||
71 | + } | ||
51 | } | 72 | } |
52 | 73 | ||
53 | - context.canvas.setFillColor(color); | ||
54 | - barcode.make(data, box.width, box.height); | ||
55 | - context.canvas.fillPath(); | ||
56 | - } | ||
57 | -} | 74 | + context.canvas |
75 | + ..setFillColor(color) | ||
76 | + ..fillPath(); | ||
58 | 77 | ||
59 | -class _BarcodeDraw extends BarcodeDraw { | ||
60 | - PdfGraphics canvas; | ||
61 | - double left; | ||
62 | - double top; | 78 | + if (drawText) { |
79 | + final PdfFont font = textStyle.font.getFont(context); | ||
80 | + | ||
81 | + for (BarcodeText text in textList) { | ||
82 | + final PdfFontMetrics metrics = font.stringMetrics(text.text); | ||
83 | + | ||
84 | + final double left = text.left + | ||
85 | + box.left + | ||
86 | + (text.width - metrics.width * text.height) / 2; | ||
87 | + | ||
88 | + final double top = box.top - | ||
89 | + text.top - | ||
90 | + metrics.descent * textStyle.fontSize - | ||
91 | + text.height; | ||
92 | + | ||
93 | + context.canvas | ||
94 | + ..setFillColor(textStyle.color) | ||
95 | + ..drawString( | ||
96 | + font, | ||
97 | + text.height, | ||
98 | + text.text, | ||
99 | + left, | ||
100 | + top, | ||
101 | + ); | ||
102 | + } | ||
103 | + } | ||
104 | + } | ||
63 | 105 | ||
64 | @override | 106 | @override |
65 | - void fillRect( | ||
66 | - double left, double top, double width, double height, bool black) { | ||
67 | - if (black) { | ||
68 | - canvas.drawRect(this.left + left, this.top + top - height, width, height); | 107 | + void debugPaint(Context context) { |
108 | + super.debugPaint(context); | ||
109 | + | ||
110 | + if (drawText) { | ||
111 | + for (BarcodeElement element in barcode.make( | ||
112 | + data, | ||
113 | + width: box.width, | ||
114 | + height: box.height, | ||
115 | + drawText: drawText, | ||
116 | + fontHeight: textStyle.fontSize, | ||
117 | + )) { | ||
118 | + if (element is BarcodeText) { | ||
119 | + context.canvas.drawRect( | ||
120 | + box.x + element.left, | ||
121 | + box.y + box.height - element.top - element.height, | ||
122 | + element.width, | ||
123 | + element.height, | ||
124 | + ); | ||
125 | + } | ||
126 | + } | ||
127 | + | ||
128 | + context.canvas | ||
129 | + ..setStrokeColor(PdfColors.blue) | ||
130 | + ..setLineWidth(1) | ||
131 | + ..strokePath(); | ||
69 | } | 132 | } |
70 | } | 133 | } |
71 | } | 134 | } |
@@ -110,31 +173,24 @@ class BarcodeWidget extends StatelessWidget { | @@ -110,31 +173,24 @@ class BarcodeWidget extends StatelessWidget { | ||
110 | 173 | ||
111 | @override | 174 | @override |
112 | Widget build(Context context) { | 175 | Widget build(Context context) { |
113 | - final TextStyle _textStyle = textStyle ?? TextStyle(font: Font.courier()); | 176 | + final TextStyle defaultstyle = Theme.of(context).defaultTextStyle.copyWith( |
177 | + font: Font.courier(), | ||
178 | + fontNormal: Font.courier(), | ||
179 | + fontBold: Font.courierBold(), | ||
180 | + fontItalic: Font.courierOblique(), | ||
181 | + fontBoldItalic: Font.courierBoldOblique(), | ||
182 | + lineSpacing: 1, | ||
183 | + ); | ||
184 | + final TextStyle _textStyle = defaultstyle.merge(textStyle); | ||
114 | 185 | ||
115 | Widget barcode = _BarcodeWidget( | 186 | Widget barcode = _BarcodeWidget( |
116 | data: data, | 187 | data: data, |
117 | color: color, | 188 | color: color, |
118 | - barcode: Barcode.fromType( | ||
119 | - type: type, | ||
120 | - draw: _BarcodeDraw(), | ||
121 | - ), | 189 | + barcode: Barcode.fromType(type), |
190 | + drawText: drawText, | ||
191 | + textStyle: _textStyle, | ||
122 | ); | 192 | ); |
123 | 193 | ||
124 | - if (drawText) { | ||
125 | - barcode = Column( | ||
126 | - children: <Widget>[ | ||
127 | - Flexible(child: barcode), | ||
128 | - Text( | ||
129 | - data, | ||
130 | - style: _textStyle, | ||
131 | - textAlign: TextAlign.center, | ||
132 | - softWrap: false, | ||
133 | - ), | ||
134 | - ], | ||
135 | - ); | ||
136 | - } | ||
137 | - | ||
138 | if (padding != null) { | 194 | if (padding != null) { |
139 | barcode = Padding(padding: padding, child: barcode); | 195 | barcode = Padding(padding: padding, child: barcode); |
140 | } | 196 | } |
@@ -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.3.28 | 7 | +version: 1.3.29 |
8 | 8 | ||
9 | environment: | 9 | environment: |
10 | sdk: ">=2.3.0 <3.0.0" | 10 | sdk: ">=2.3.0 <3.0.0" |
@@ -16,7 +16,7 @@ dependencies: | @@ -16,7 +16,7 @@ dependencies: | ||
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 | 18 | qr: ^1.2.0 |
19 | - barcode: ^0.1.0 | 19 | + barcode: ^1.0.0 |
20 | 20 | ||
21 | dev_dependencies: | 21 | dev_dependencies: |
22 | test: | 22 | test: |
@@ -18,6 +18,7 @@ | @@ -18,6 +18,7 @@ | ||
18 | 18 | ||
19 | import 'dart:io'; | 19 | import 'dart:io'; |
20 | 20 | ||
21 | +import 'package:barcode/barcode.dart'; | ||
21 | import 'package:pdf/pdf.dart'; | 22 | import 'package:pdf/pdf.dart'; |
22 | import 'package:pdf/widgets.dart'; | 23 | import 'package:pdf/widgets.dart'; |
23 | import 'package:test/test.dart'; | 24 | import 'package:test/test.dart'; |
@@ -30,13 +31,36 @@ void main() { | @@ -30,13 +31,36 @@ void main() { | ||
30 | pdf = Document(); | 31 | pdf = Document(); |
31 | }); | 32 | }); |
32 | 33 | ||
33 | - test('Barcode Widgets Code39', () { | 34 | + test('Barcode Widgets', () { |
34 | pdf.addPage( | 35 | pdf.addPage( |
35 | - Page( | ||
36 | - build: (Context context) => BarcodeWidget( | ||
37 | - data: 'HELLO 123', | ||
38 | - width: 200, | ||
39 | - height: 50, | 36 | + MultiPage( |
37 | + build: (Context context) => List<Widget>.generate( | ||
38 | + BarcodeType.values.length, | ||
39 | + (int index) { | ||
40 | + return Row( | ||
41 | + mainAxisAlignment: MainAxisAlignment.spaceEvenly, | ||
42 | + children: <Widget>[ | ||
43 | + Text(BarcodeType.values[index].toString()), | ||
44 | + BarcodeWidget( | ||
45 | + type: BarcodeType.values[index], | ||
46 | + data: 'HELLO 123', | ||
47 | + width: 200, | ||
48 | + height: 50, | ||
49 | + margin: const EdgeInsets.symmetric(vertical: 20), | ||
50 | + padding: | ||
51 | + const EdgeInsets.symmetric(horizontal: 10, vertical: 3), | ||
52 | + decoration: BoxDecoration( | ||
53 | + border: BoxBorder( | ||
54 | + color: PdfColors.blue, | ||
55 | + top: true, | ||
56 | + bottom: true, | ||
57 | + left: true, | ||
58 | + right: true, | ||
59 | + )), | ||
60 | + ), | ||
61 | + ], | ||
62 | + ); | ||
63 | + }, | ||
40 | ), | 64 | ), |
41 | ), | 65 | ), |
42 | ); | 66 | ); |
test/golden/widgets-barcode.pdf
0 → 100644
No preview for this file type
-
Please register or login to post a comment