David PHAM-VAN

Implement Barcode textPadding and bytes data

1 # Changelog 1 # Changelog
2 2
3 -## 1.9.1 3 +## 1.10.0
4 4
5 -- Fix dependency to Image 5 +- Fix dependencies
  6 +- Implement Barcode textPadding and bytes data
6 7
7 ## 1.9.0 8 ## 1.9.0
8 9
@@ -17,6 +17,7 @@ @@ -17,6 +17,7 @@
17 library widget; 17 library widget;
18 18
19 import 'dart:collection'; 19 import 'dart:collection';
  20 +import 'dart:convert';
20 import 'dart:math' as math; 21 import 'dart:math' as math;
21 import 'dart:typed_data'; 22 import 'dart:typed_data';
22 import 'package:image/image.dart' as im; 23 import 'package:image/image.dart' as im;
@@ -25,10 +25,11 @@ class _BarcodeWidget extends Widget { @@ -25,10 +25,11 @@ class _BarcodeWidget extends Widget {
25 this.color = PdfColors.black, 25 this.color = PdfColors.black,
26 this.drawText, 26 this.drawText,
27 this.textStyle, 27 this.textStyle,
  28 + this.textPadding,
28 }); 29 });
29 30
30 /// the barcode data 31 /// the barcode data
31 - final String data; 32 + final Uint8List data;
32 33
33 final Barcode barcode; 34 final Barcode barcode;
34 35
@@ -38,6 +39,8 @@ class _BarcodeWidget extends Widget { @@ -38,6 +39,8 @@ class _BarcodeWidget extends Widget {
38 39
39 final TextStyle textStyle; 40 final TextStyle textStyle;
40 41
  42 + final double textPadding;
  43 +
41 @override 44 @override
42 void layout(Context context, BoxConstraints constraints, 45 void layout(Context context, BoxConstraints constraints,
43 {bool parentUsesSize = false}) { 46 {bool parentUsesSize = false}) {
@@ -50,12 +53,13 @@ class _BarcodeWidget extends Widget { @@ -50,12 +53,13 @@ class _BarcodeWidget extends Widget {
50 53
51 final List<BarcodeText> textList = <BarcodeText>[]; 54 final List<BarcodeText> textList = <BarcodeText>[];
52 55
53 - for (BarcodeElement element in barcode.make( 56 + for (BarcodeElement element in barcode.makeBytes(
54 data, 57 data,
55 width: box.width, 58 width: box.width,
56 height: box.height, 59 height: box.height,
57 drawText: drawText, 60 drawText: drawText,
58 fontHeight: textStyle.fontSize, 61 fontHeight: textStyle.fontSize,
  62 + textPadding: textPadding,
59 )) { 63 )) {
60 if (element is BarcodeBar) { 64 if (element is BarcodeBar) {
61 if (element.black) { 65 if (element.black) {
@@ -121,12 +125,13 @@ class _BarcodeWidget extends Widget { @@ -121,12 +125,13 @@ class _BarcodeWidget extends Widget {
121 super.debugPaint(context); 125 super.debugPaint(context);
122 126
123 if (drawText) { 127 if (drawText) {
124 - for (BarcodeElement element in barcode.make( 128 + for (BarcodeElement element in barcode.makeBytes(
125 data, 129 data,
126 width: box.width, 130 width: box.width,
127 height: box.height, 131 height: box.height,
128 drawText: drawText, 132 drawText: drawText,
129 fontHeight: textStyle.fontSize, 133 fontHeight: textStyle.fontSize,
  134 + textPadding: textPadding,
130 )) { 135 )) {
131 if (element is BarcodeText) { 136 if (element is BarcodeText) {
132 context.canvas.drawRect( 137 context.canvas.drawRect(
@@ -147,7 +152,38 @@ class _BarcodeWidget extends Widget { @@ -147,7 +152,38 @@ class _BarcodeWidget extends Widget {
147 } 152 }
148 153
149 class BarcodeWidget extends StatelessWidget { 154 class BarcodeWidget extends StatelessWidget {
150 - BarcodeWidget({ 155 + /// Draw a barcode using String data
  156 + factory BarcodeWidget({
  157 + @required String data,
  158 + @required Barcode barcode,
  159 + PdfColor color = PdfColors.black,
  160 + PdfColor backgroundColor,
  161 + BoxDecoration decoration,
  162 + EdgeInsets margin,
  163 + EdgeInsets padding,
  164 + double width,
  165 + double height,
  166 + bool drawText = true,
  167 + TextStyle textStyle,
  168 + double textPadding = 0,
  169 + }) =>
  170 + BarcodeWidget.fromBytes(
  171 + data: utf8.encoder.convert(data),
  172 + barcode: barcode,
  173 + color: color,
  174 + backgroundColor: backgroundColor,
  175 + decoration: decoration,
  176 + margin: margin,
  177 + padding: padding,
  178 + width: width,
  179 + height: height,
  180 + drawText: drawText,
  181 + textStyle: textStyle,
  182 + textPadding: textPadding,
  183 + );
  184 +
  185 + /// Draw a barcode using Uint8List data
  186 + BarcodeWidget.fromBytes({
151 @required this.data, 187 @required this.data,
152 @required this.barcode, 188 @required this.barcode,
153 this.color = PdfColors.black, 189 this.color = PdfColors.black,
@@ -159,29 +195,51 @@ class BarcodeWidget extends StatelessWidget { @@ -159,29 +195,51 @@ class BarcodeWidget extends StatelessWidget {
159 this.height, 195 this.height,
160 this.drawText = true, 196 this.drawText = true,
161 this.textStyle, 197 this.textStyle,
162 - }) : assert(barcode != null); 198 + this.textPadding = 0,
  199 + }) : assert(data != null),
  200 + assert(barcode != null),
  201 + assert(textPadding != null);
163 202
164 /// the barcode data 203 /// the barcode data
165 - final String data; 204 + final Uint8List data;
166 205
  206 + /// The type of barcode to use.
  207 + /// use:
  208 + /// * Barcode.code128()
  209 + /// * Barcode.ean13()
  210 + /// * ...
167 final Barcode barcode; 211 final Barcode barcode;
168 212
  213 + /// The bars color
  214 + /// should be black or really dark color
169 final PdfColor color; 215 final PdfColor color;
170 216
  217 + /// The background color.
  218 + /// this should be white or really light color
171 final PdfColor backgroundColor; 219 final PdfColor backgroundColor;
172 220
  221 + /// Padding to apply
173 final EdgeInsets padding; 222 final EdgeInsets padding;
174 223
  224 + /// Margin to apply
175 final EdgeInsets margin; 225 final EdgeInsets margin;
176 226
  227 + /// Width of the barcode with padding
177 final double width; 228 final double width;
178 229
  230 + /// Height of the barcode with padding
179 final double height; 231 final double height;
180 232
  233 + /// Whether to draw the text with the barcode
181 final bool drawText; 234 final bool drawText;
182 235
  236 + /// Text style to use to draw the text
183 final TextStyle textStyle; 237 final TextStyle textStyle;
184 238
  239 + /// Padding to add between the text and the barcode
  240 + final double textPadding;
  241 +
  242 + /// Decoration to apply to the barcode
185 final BoxDecoration decoration; 243 final BoxDecoration decoration;
186 244
187 @override 245 @override
@@ -203,6 +261,7 @@ class BarcodeWidget extends StatelessWidget { @@ -203,6 +261,7 @@ class BarcodeWidget extends StatelessWidget {
203 barcode: barcode, 261 barcode: barcode,
204 drawText: drawText, 262 drawText: drawText,
205 textStyle: _textStyle, 263 textStyle: _textStyle,
  264 + textPadding: textPadding,
206 ); 265 );
207 266
208 if (padding != null) { 267 if (padding != null) {
@@ -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.9.1 7 +version: 1.10.0
8 8
9 environment: 9 environment:
10 sdk: ">=2.3.0 <3.0.0" 10 sdk: ">=2.3.0 <3.0.0"
@@ -15,10 +15,10 @@ dependencies: @@ -15,10 +15,10 @@ 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 - barcode: ^1.14.0 18 + barcode: ^1.16.0
19 image: ^2.1.10 19 image: ^2.1.10
20 path_parsing: ^0.1.4 20 path_parsing: ^0.1.4
21 21
22 dev_dependencies: 22 dev_dependencies:
23 test: 23 test:
24 - pedantic: 1.9.0 24 + pedantic: 1.9.2