diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..9b34d7d --- /dev/null +++ b/.gitignore @@ -0,0 +1,25 @@ +# Files and directories created by pub +.packages +.pub/ +build/ +# Remove the following pattern if you wish to check in your lock file +**/pubspec.lock + +# Directory created by dartdoc +doc/api/ +.idea +/intl_en.arb +.DS_Store + +# coverage +coverage/ +lcov.info + +# tests +.flutter-plugins + +# Files generated by dart tools +.dart_tool + +# generated by flutter +flutter_export_environment.sh \ No newline at end of file diff --git a/example/lib/main.dart b/example/lib/main.dart index c2d8c90..55e1774 100644 --- a/example/lib/main.dart +++ b/example/lib/main.dart @@ -1,33 +1,104 @@ import 'package:flutter/material.dart'; import 'package:barcode_flutter/barcode_flutter.dart'; -void main() => runApp(new MyApp()); - +void main() => runApp(MyApp()); class MyApp extends StatelessWidget { @override Widget build(BuildContext context) { - return new MaterialApp( + return MaterialApp( title: 'Flutter Demo', - theme: new ThemeData( + theme: ThemeData( primarySwatch: Colors.blue, ), - home: new MyHomePage(codeList: [ - new BarCodeItem(type: BarCodeType.Code39, codeStr: "CODE39", description: "Code39 with text", hasText: true), - new BarCodeItem(type: BarCodeType.Code39, codeStr: "CODE39", description: "Code39", hasText: false), - new BarCodeItem(type: BarCodeType.Code93, codeStr: "BARCODE93", description: "Code93 with text", hasText: true), - new BarCodeItem(type: BarCodeType.Code93, codeStr: "BARCODE93", description: "Code93", hasText: false), - new BarCodeItem(type: BarCodeType.Code128, codeStr: "BARCODE128", description: "Code128 with text", hasText: true), - new BarCodeItem(type: BarCodeType.Code128, codeStr: "BARCODE128", description: "Code128", hasText: false), - new BarCodeItem(type: BarCodeType.CodeEAN8, codeStr: "65833254", description: "EAN8 with text", hasText: true), - new BarCodeItem(type: BarCodeType.CodeEAN8, codeStr: "65833254", description: "EAN8", hasText: false), - new BarCodeItem(type: BarCodeType.CodeEAN13, codeStr: "9501101530003", description: "EAN13 with text", hasText: true), - new BarCodeItem(type: BarCodeType.CodeEAN13, codeStr: "9501101530003", description: "EAN13", hasText: false), - new BarCodeItem(type: BarCodeType.CodeUPCA, codeStr: "123456789012", description: "UPCA with text", hasText: true), - new BarCodeItem(type: BarCodeType.CodeUPCA, codeStr: "123456789012", description: "UPCA", hasText: false), - new BarCodeItem(type: BarCodeType.CodeUPCE, codeStr: "00123457", description: "UPCE with text", hasText: true), - new BarCodeItem(type: BarCodeType.CodeUPCE, codeStr: "00123457", description: "UPCE", hasText: false), - ],) + home: MyHomePage( + codeList: [ + BarCodeItem( + type: BarCodeType.Code39, + codeStr: "CODE39", + description: "Code39 with text", + hasText: true, + ), + BarCodeItem( + type: BarCodeType.Code39, + codeStr: "CODE39", + description: "Code39", + hasText: false, + ), + BarCodeItem( + type: BarCodeType.Code93, + codeStr: "BARCODE93", + description: "Code93 with text", + hasText: true, + ), + BarCodeItem( + type: BarCodeType.Code93, + codeStr: "BARCODE93", + description: "Code93", + hasText: false, + ), + BarCodeItem( + type: BarCodeType.Code128, + codeStr: "BARCODE128", + description: "Code128 with text", + hasText: true, + ), + BarCodeItem( + type: BarCodeType.Code128, + codeStr: "BARCODE128", + description: "Code128", + hasText: false, + ), + BarCodeItem( + type: BarCodeType.CodeEAN8, + codeStr: "65833254", + description: "EAN8 with text", + hasText: true, + ), + BarCodeItem( + type: BarCodeType.CodeEAN8, + codeStr: "65833254", + description: "EAN8", + hasText: false, + ), + BarCodeItem( + type: BarCodeType.CodeEAN13, + codeStr: "9501101530003", + description: "EAN13 with text", + hasText: true, + ), + BarCodeItem( + type: BarCodeType.CodeEAN13, + codeStr: "9501101530003", + description: "EAN13", + hasText: false, + ), + BarCodeItem( + type: BarCodeType.CodeUPCA, + codeStr: "123456789012", + description: "UPCA with text", + hasText: true, + ), + BarCodeItem( + type: BarCodeType.CodeUPCA, + codeStr: "123456789012", + description: "UPCA", + hasText: false, + ), + BarCodeItem( + type: BarCodeType.CodeUPCE, + codeStr: "00123457", + description: "UPCE with text", + hasText: true, + ), + BarCodeItem( + type: BarCodeType.CodeUPCE, + codeStr: "00123457", + description: "UPCE", + hasText: false, + ), + ], + ), ); } } @@ -38,49 +109,63 @@ class MyHomePage extends StatefulWidget { final String title = "BarCode Flutter"; @override - _MyHomePageState createState() => new _MyHomePageState(); + _MyHomePageState createState() => _MyHomePageState(); } class _MyHomePageState extends State { @override Widget build(BuildContext context) { - return new Scaffold( - appBar: new AppBar( - title: new Text(widget.title), + return Scaffold( + appBar: AppBar( + title: Text(widget.title), ), - body: new ListView( + body: ListView( children: widget.codeList.map((element) { - return new Padding(padding: const EdgeInsets.all(10.0), - child: new Card( - child: new Column( - children: [ - new Align( - alignment: Alignment.centerLeft, - child: new Text(element.description, - textAlign: TextAlign.left, - style: new TextStyle(fontWeight: FontWeight.bold, fontSize: 20.0, color: Colors.black45), + return Padding( + padding: const EdgeInsets.all(10.0), + child: Card( + color: Colors.blueGrey[50], + child: Padding( + padding: EdgeInsets.all(8.0), + child: Column( + children: [ + Align( + alignment: Alignment.centerLeft, + child: Text( + element.description, + textAlign: TextAlign.left, + style: TextStyle( + fontWeight: FontWeight.bold, + fontSize: 20.0, + color: Colors.black45, + ), + ), ), - ), - new Center(child: - new Container(padding: const EdgeInsets.all(10.0), - child: new BarCodeImage( - data:element.codeStr, - codeType: element.type, - lineWidth: 2.0, - barHeight: 100.0, - hasText: element.hasText, - onError: (error) { - print("Generate barcode failed. error msg: $error"); - }, + Center( + child: Container( + padding: const EdgeInsets.all(10.0), + child: BarCodeImage( + // backgroundColor: Colors.red, + // foregroundColor: Colors.deepPurple, + data: element.codeStr, + codeType: element.type, + lineWidth: 2.0, + barHeight: 100.0, + hasText: element.hasText, + onError: (error) { + print("Generate barcode failed. error msg: $error"); + }, + ), ), - ) - ) - ] - ) - ),) ; - } - ).toList() - )); + ), + ], + ), + ), + ), + ); + }).toList(), + ), + ); } } diff --git a/example/pubspec.lock b/example/pubspec.lock deleted file mode 100644 index eb8ca7b..0000000 --- a/example/pubspec.lock +++ /dev/null @@ -1,377 +0,0 @@ -# Generated by pub -# See https://www.dartlang.org/tools/pub/glossary#lockfile -packages: - analyzer: - dependency: transitive - description: - name: analyzer - url: "https://pub.dartlang.org" - source: hosted - version: "0.31.2-alpha.2" - args: - dependency: transitive - description: - name: args - url: "https://pub.dartlang.org" - source: hosted - version: "1.4.3" - async: - dependency: transitive - description: - name: async - url: "https://pub.dartlang.org" - source: hosted - version: "2.0.7" - barcode_flutter: - dependency: "direct main" - description: - path: ".." - relative: true - source: path - version: "1.0.0" - boolean_selector: - dependency: transitive - description: - name: boolean_selector - url: "https://pub.dartlang.org" - source: hosted - version: "1.0.3" - charcode: - dependency: transitive - description: - name: charcode - url: "https://pub.dartlang.org" - source: hosted - version: "1.1.1" - collection: - dependency: transitive - description: - name: collection - url: "https://pub.dartlang.org" - source: hosted - version: "1.14.6" - convert: - dependency: transitive - description: - name: convert - url: "https://pub.dartlang.org" - source: hosted - version: "2.0.1" - crypto: - dependency: transitive - description: - name: crypto - url: "https://pub.dartlang.org" - source: hosted - version: "2.0.3" - csslib: - dependency: transitive - description: - name: csslib - url: "https://pub.dartlang.org" - source: hosted - version: "0.14.4" - cupertino_icons: - dependency: "direct main" - description: - name: cupertino_icons - url: "https://pub.dartlang.org" - source: hosted - version: "0.1.2" - flutter: - dependency: "direct main" - description: flutter - source: sdk - version: "0.0.0" - flutter_test: - dependency: "direct dev" - description: flutter - source: sdk - version: "0.0.0" - front_end: - dependency: transitive - description: - name: front_end - url: "https://pub.dartlang.org" - source: hosted - version: "0.1.0-alpha.12" - glob: - dependency: transitive - description: - name: glob - url: "https://pub.dartlang.org" - source: hosted - version: "1.1.5" - html: - dependency: transitive - description: - name: html - url: "https://pub.dartlang.org" - source: hosted - version: "0.13.3" - http: - dependency: transitive - description: - name: http - url: "https://pub.dartlang.org" - source: hosted - version: "0.11.3+16" - http_multi_server: - dependency: transitive - description: - name: http_multi_server - url: "https://pub.dartlang.org" - source: hosted - version: "2.0.4" - http_parser: - dependency: transitive - description: - name: http_parser - url: "https://pub.dartlang.org" - source: hosted - version: "3.1.2" - io: - dependency: transitive - description: - name: io - url: "https://pub.dartlang.org" - source: hosted - version: "0.3.2+1" - js: - dependency: transitive - description: - name: js - url: "https://pub.dartlang.org" - source: hosted - version: "0.6.1" - kernel: - dependency: transitive - description: - name: kernel - url: "https://pub.dartlang.org" - source: hosted - version: "0.3.0-alpha.12" - logging: - dependency: transitive - description: - name: logging - url: "https://pub.dartlang.org" - source: hosted - version: "0.11.3+1" - matcher: - dependency: transitive - description: - name: matcher - url: "https://pub.dartlang.org" - source: hosted - version: "0.12.2+1" - meta: - dependency: transitive - description: - name: meta - url: "https://pub.dartlang.org" - source: hosted - version: "1.1.5" - mime: - dependency: transitive - description: - name: mime - url: "https://pub.dartlang.org" - source: hosted - version: "0.9.6" - multi_server_socket: - dependency: transitive - description: - name: multi_server_socket - url: "https://pub.dartlang.org" - source: hosted - version: "1.0.1" - node_preamble: - dependency: transitive - description: - name: node_preamble - url: "https://pub.dartlang.org" - source: hosted - version: "1.4.1" - package_config: - dependency: transitive - description: - name: package_config - url: "https://pub.dartlang.org" - source: hosted - version: "1.0.3" - package_resolver: - dependency: transitive - description: - name: package_resolver - url: "https://pub.dartlang.org" - source: hosted - version: "1.0.2" - path: - dependency: transitive - description: - name: path - url: "https://pub.dartlang.org" - source: hosted - version: "1.5.1" - plugin: - dependency: transitive - description: - name: plugin - url: "https://pub.dartlang.org" - source: hosted - version: "0.2.0+2" - pool: - dependency: transitive - description: - name: pool - url: "https://pub.dartlang.org" - source: hosted - version: "1.3.4" - pub_semver: - dependency: transitive - description: - name: pub_semver - url: "https://pub.dartlang.org" - source: hosted - version: "1.4.1" - quiver: - dependency: transitive - description: - name: quiver - url: "https://pub.dartlang.org" - source: hosted - version: "0.29.0+1" - shelf: - dependency: transitive - description: - name: shelf - url: "https://pub.dartlang.org" - source: hosted - version: "0.7.3" - shelf_packages_handler: - dependency: transitive - description: - name: shelf_packages_handler - url: "https://pub.dartlang.org" - source: hosted - version: "1.0.3" - shelf_static: - dependency: transitive - description: - name: shelf_static - url: "https://pub.dartlang.org" - source: hosted - version: "0.2.7" - shelf_web_socket: - dependency: transitive - description: - name: shelf_web_socket - url: "https://pub.dartlang.org" - source: hosted - version: "0.2.2" - sky_engine: - dependency: transitive - description: flutter - source: sdk - version: "0.0.99" - source_map_stack_trace: - dependency: transitive - description: - name: source_map_stack_trace - url: "https://pub.dartlang.org" - source: hosted - version: "1.1.4" - source_maps: - dependency: transitive - description: - name: source_maps - url: "https://pub.dartlang.org" - source: hosted - version: "0.10.5" - source_span: - dependency: transitive - description: - name: source_span - url: "https://pub.dartlang.org" - source: hosted - version: "1.4.0" - stack_trace: - dependency: transitive - description: - name: stack_trace - url: "https://pub.dartlang.org" - source: hosted - version: "1.9.2" - stream_channel: - dependency: transitive - description: - name: stream_channel - url: "https://pub.dartlang.org" - source: hosted - version: "1.6.6" - string_scanner: - dependency: transitive - description: - name: string_scanner - url: "https://pub.dartlang.org" - source: hosted - version: "1.0.2" - term_glyph: - dependency: transitive - description: - name: term_glyph - url: "https://pub.dartlang.org" - source: hosted - version: "1.0.0" - test: - dependency: transitive - description: - name: test - url: "https://pub.dartlang.org" - source: hosted - version: "0.12.37" - typed_data: - dependency: transitive - description: - name: typed_data - url: "https://pub.dartlang.org" - source: hosted - version: "1.1.5" - utf: - dependency: transitive - description: - name: utf - url: "https://pub.dartlang.org" - source: hosted - version: "0.9.0+4" - vector_math: - dependency: transitive - description: - name: vector_math - url: "https://pub.dartlang.org" - source: hosted - version: "2.0.6" - watcher: - dependency: transitive - description: - name: watcher - url: "https://pub.dartlang.org" - source: hosted - version: "0.9.7+7" - web_socket_channel: - dependency: transitive - description: - name: web_socket_channel - url: "https://pub.dartlang.org" - source: hosted - version: "1.0.7" - yaml: - dependency: transitive - description: - name: yaml - url: "https://pub.dartlang.org" - source: hosted - version: "2.1.13" -sdks: - dart: ">=2.0.0-dev.58.0 <=2.0.0-dev.58.0.flutter-f981f09760" diff --git a/lib/barcode_flutter.dart b/lib/barcode_flutter.dart index 72a0b98..35cd11b 100644 --- a/lib/barcode_flutter.dart +++ b/lib/barcode_flutter.dart @@ -5,6 +5,5 @@ */ export 'src/barcode_enum.dart'; -export 'src/barcode_img.dart'; +export 'src/barcode_image.dart'; export 'src/barcode_painter.dart'; - diff --git a/lib/src/barcode_img.dart b/lib/src/barcode_image.dart similarity index 52% rename from lib/src/barcode_img.dart rename to lib/src/barcode_image.dart index f907099..2a6ece8 100644 --- a/lib/src/barcode_img.dart +++ b/lib/src/barcode_image.dart @@ -15,23 +15,31 @@ class BarCodeImage extends StatelessWidget { @required this.codeType, this.lineWidth = 2.0, this.barHeight = 100.0, - this.padding = const EdgeInsets.all(5.0), - this.backgroundColor, + this.foregroundColor = const Color(0xFF000000), + this.backgroundColor = const Color(0xFFFFFFFF), this.hasText = false, - Color foregroundColor = const Color(0xFF000000), + this.fontSize = 15.0, this.onError, }) : _painter = new BarCodePainter( - data, codeType, lineWidth, hasText, foregroundColor, onError: onError); + data, + codeType, + lineWidth, + hasText, + foregroundColor, + backgroundColor, + onError: onError, + ); + final BarCodePainter _painter; final String data; final BarCodeType codeType; - final BarCodePainter _painter; + final Color foregroundColor; final Color backgroundColor; - final EdgeInsets padding; final double lineWidth; final double barHeight; final hasText; final BarCodeError onError; + final double fontSize; @override Widget build(BuildContext context) { @@ -41,11 +49,8 @@ class BarCodeImage extends StatelessWidget { width: _calcCanvasWidth(this.data, this.codeType, this.lineWidth), height: hasText ? this.barHeight * 1.08 : this.barHeight, color: backgroundColor, - child: new Padding( - padding: this.padding, - child: new CustomPaint( - painter: _painter, - ), + child: new CustomPaint( + painter: _painter, ), ); }, @@ -54,21 +59,32 @@ class BarCodeImage extends StatelessWidget { double _calcCanvasWidth(String content, BarCodeType type, double lineWidth) { int strLength = content.length; - switch(type) { + double calculatedWidth = 0; + switch (type) { case BarCodeType.Code39: - return (strLength+2)*13*lineWidth; + // The painter renders each character as 12 lines and calculates the + // space for a trailing gap. Every Code39 barcode character ends + // with a single "white" line. + // So, we subtract one more line to remove the trailing gap + calculatedWidth = (strLength + 2) * 13 * lineWidth - lineWidth; + //print("calculated width = $calculatedWidth"); + return calculatedWidth; case BarCodeType.Code93: - return (strLength+5)*9*lineWidth-3; + // We have removed an extra character that was painted at the end + // of this barcode type and replaced it with a single bar + // as per the wikipedia specification. + //return (strLength + 5) * 9 * lineWidth - 3; + return (strLength + 4) * 9 * lineWidth + lineWidth; case BarCodeType.Code128: - return (strLength+2)*11*lineWidth+13*lineWidth; + return (strLength + 2) * 11 * lineWidth + 13 * lineWidth; case BarCodeType.CodeEAN13: - return (lineWidth*113); + return (lineWidth * 113); case BarCodeType.CodeEAN8: - return (lineWidth*81); + return (lineWidth * 81); case BarCodeType.CodeUPCA: - return (lineWidth*113); + return (lineWidth * 113); case BarCodeType.CodeUPCE: - return (lineWidth*67); + return (lineWidth * 67); default: return 0.0; } diff --git a/lib/src/barcode_painter.dart b/lib/src/barcode_painter.dart index 72088cd..5cb2d9e 100644 --- a/lib/src/barcode_painter.dart +++ b/lib/src/barcode_painter.dart @@ -11,9 +11,11 @@ import 'barcode_enum.dart'; typedef void BarCodeError(dynamic error); class BarCodePainter extends CustomPainter { - BarCodePainter(this.data, this.type, this.lineWidth, this.hasText, this.color, {this.onError}); + BarCodePainter(this.data, this.type, this.lineWidth, this.hasText, this.foregroundColor, this.backgroundColor, + {this.onError}); - final Color color; + final Color foregroundColor; + final Color backgroundColor; final String data; final BarCodeType type; final double lineWidth; @@ -22,14 +24,28 @@ class BarCodePainter extends CustomPainter { @override void paint(Canvas canvas, Size size) { - switch(type) { - case BarCodeType.Code39: _drawBarCode39(canvas, size); break; - case BarCodeType.Code93: _drawBarCode93(canvas, size); break; - case BarCodeType.Code128: _drawBarCode128(canvas, size); break; - case BarCodeType.CodeEAN13: _drawBarCodeEAN13(canvas, size); break; - case BarCodeType.CodeEAN8: _drawBarCodeEAN8(canvas, size); break; - case BarCodeType.CodeUPCA: _drawBarCodeUPCA(canvas, size); break; - case BarCodeType.CodeUPCE: _drawBarCodeUPCE(canvas, size); break; + switch (type) { + case BarCodeType.Code39: + _drawBarCode39(canvas, size); + break; + case BarCodeType.Code93: + _drawBarCode93(canvas, size); + break; + case BarCodeType.Code128: + _drawBarCode128(canvas, size); + break; + case BarCodeType.CodeEAN13: + _drawBarCodeEAN13(canvas, size); + break; + case BarCodeType.CodeEAN8: + _drawBarCodeEAN8(canvas, size); + break; + case BarCodeType.CodeUPCA: + _drawBarCodeUPCA(canvas, size); + break; + case BarCodeType.CodeUPCE: + _drawBarCodeUPCE(canvas, size); + break; } } @@ -40,64 +56,188 @@ class BarCodePainter extends CustomPainter { void _drawBarCode39(Canvas canvas, Size size) { final List binSet = [ - 0xa6d, 0xd2b, 0xb2b, 0xd95, 0xa6b, 0xd35, 0xb35, 0xa5b, - 0xd2d, 0xb2d, 0xd4b, 0xb4b, 0xda5, 0xacb, 0xd65, 0xb65, - 0xa9b, 0xd4d, 0xb4d, 0xacd, 0xd53, 0xb53, 0xda9, 0xad3, - 0xd69, 0xb69, 0xab3, 0xd59, 0xb59, 0xad9, 0xcab, 0x9ab, - 0xcd5, 0x96b, 0xcb5, 0x9b5, 0x95b, 0xcad, 0x9ad, 0x925, - 0x929, 0x949, 0xa49, 0x96d + 0xa6d, + 0xd2b, + 0xb2b, + 0xd95, + 0xa6b, + 0xd35, + 0xb35, + 0xa5b, + 0xd2d, + 0xb2d, + 0xd4b, + 0xb4b, + 0xda5, + 0xacb, + 0xd65, + 0xb65, + 0xa9b, + 0xd4d, + 0xb4d, + 0xacd, + 0xd53, + 0xb53, + 0xda9, + 0xad3, + 0xd69, + 0xb69, + 0xab3, + 0xd59, + 0xb59, + 0xad9, + 0xcab, + 0x9ab, + 0xcd5, + 0x96b, + 0xcb5, + 0x9b5, + 0x95b, + 0xcad, + 0x9ad, + 0x925, + 0x929, + 0x949, + 0xa49, + 0x96d ]; int codeValue = 0; bool hasError = false; - final painter = new Paint()..style = PaintingStyle.fill; + final painter = Paint()..style = PaintingStyle.fill; double height = hasText ? size.height * 0.85 : size.height; for (int i = 0; i < data.length; i++) { - switch(data[i]) { - case '0': codeValue = 0; break; - case '1': codeValue = 1; break; - case '2': codeValue = 2; break; - case '3': codeValue = 3; break; - case '4': codeValue = 4; break; - case '5': codeValue = 5; break; - case '6': codeValue = 6; break; - case '7': codeValue = 7; break; - case '8': codeValue = 8; break; - case '9': codeValue = 9; break; - case 'A': codeValue = 10; break; - case 'B': codeValue = 11; break; - case 'C': codeValue = 12; break; - case 'D': codeValue = 13; break; - case 'E': codeValue = 14; break; - case 'F': codeValue = 15; break; - case 'G': codeValue = 16; break; - case 'H': codeValue = 17; break; - case 'I': codeValue = 18; break; - case 'J': codeValue = 19; break; - case 'K': codeValue = 20; break; - case 'L': codeValue = 21; break; - case 'M': codeValue = 22; break; - case 'N': codeValue = 23; break; - case 'O': codeValue = 24; break; - case 'P': codeValue = 25; break; - case 'Q': codeValue = 26; break; - case 'R': codeValue = 27; break; - case 'S': codeValue = 28; break; - case 'T': codeValue = 29; break; - case 'U': codeValue = 30; break; - case 'V': codeValue = 31; break; - case 'W': codeValue = 32; break; - case 'X': codeValue = 33; break; - case 'Y': codeValue = 34; break; - case 'Z': codeValue = 35; break; - case '-': codeValue = 36; break; - case '.': codeValue = 37; break; - case ' ': codeValue = 38; break; - case '\$': codeValue = 39; break; - case '/': codeValue = 40; break; - case '+': codeValue = 41; break; - case '%': codeValue = 42; break; + switch (data[i]) { + case '0': + codeValue = 0; + break; + case '1': + codeValue = 1; + break; + case '2': + codeValue = 2; + break; + case '3': + codeValue = 3; + break; + case '4': + codeValue = 4; + break; + case '5': + codeValue = 5; + break; + case '6': + codeValue = 6; + break; + case '7': + codeValue = 7; + break; + case '8': + codeValue = 8; + break; + case '9': + codeValue = 9; + break; + case 'A': + codeValue = 10; + break; + case 'B': + codeValue = 11; + break; + case 'C': + codeValue = 12; + break; + case 'D': + codeValue = 13; + break; + case 'E': + codeValue = 14; + break; + case 'F': + codeValue = 15; + break; + case 'G': + codeValue = 16; + break; + case 'H': + codeValue = 17; + break; + case 'I': + codeValue = 18; + break; + case 'J': + codeValue = 19; + break; + case 'K': + codeValue = 20; + break; + case 'L': + codeValue = 21; + break; + case 'M': + codeValue = 22; + break; + case 'N': + codeValue = 23; + break; + case 'O': + codeValue = 24; + break; + case 'P': + codeValue = 25; + break; + case 'Q': + codeValue = 26; + break; + case 'R': + codeValue = 27; + break; + case 'S': + codeValue = 28; + break; + case 'T': + codeValue = 29; + break; + case 'U': + codeValue = 30; + break; + case 'V': + codeValue = 31; + break; + case 'W': + codeValue = 32; + break; + case 'X': + codeValue = 33; + break; + case 'Y': + codeValue = 34; + break; + case 'Z': + codeValue = 35; + break; + case '-': + codeValue = 36; + break; + case '.': + codeValue = 37; + break; + case ' ': + codeValue = 38; + break; + case '\$': + codeValue = 39; + break; + case '/': + codeValue = 40; + break; + case '+': + codeValue = 41; + break; + case '%': + codeValue = 42; + break; default: codeValue = 0; hasError = true; @@ -105,112 +245,247 @@ class BarCodePainter extends CustomPainter { } if (hasError) { - String errorMsg = "Invalid content for Code39. Please check https://en.wikipedia.org/wiki/Code_39 for reference."; + String errorMsg = + "Invalid content for Code39. Please check https://en.wikipedia.org/wiki/Code_39 for reference."; if (this.onError != null) { this.onError(errorMsg); } else { print(errorMsg); } - return ; + return; } for (int j = 0; j < 12; j++) { - Rect rect = new Rect.fromLTWH(13*lineWidth+13*i*lineWidth+j*lineWidth, 0.0, lineWidth, height); - ((0x800 & (binSet[codeValue] << j)) == 0x800) ? painter.color = Colors.black : painter.color = Colors.white; + Rect rect = Rect.fromLTWH(13 * lineWidth + 13 * i * lineWidth + j * lineWidth, 0.0, lineWidth, height); + ((0x800 & (binSet[codeValue] << j)) == 0x800) + ? painter.color = this.foregroundColor + : painter.color = this.backgroundColor; canvas.drawRect(rect, painter); } } for (int i = 0; i < 12; i++) { - Rect rect = new Rect.fromLTWH(i*lineWidth, 0.0, lineWidth, height); - ((0x800 & (binSet[43] << i)) == 0x800) ? painter.color = Colors.black : painter.color = Colors.white; + Rect rect = Rect.fromLTWH(i * lineWidth, 0.0, lineWidth, height); + ((0x800 & (binSet[43] << i)) == 0x800) + ? painter.color = this.foregroundColor + : painter.color = this.backgroundColor; canvas.drawRect(rect, painter); } for (int i = 0; i < 12; i++) { - Rect rect = new Rect.fromLTWH((13+i)*lineWidth+13*(data.length)*lineWidth, 0.0, lineWidth, height); - ((0x800 & (binSet[43] << i)) == 0x800) ? painter.color = Colors.black : painter.color = Colors.white; + Rect rect = Rect.fromLTWH((13 + i) * lineWidth + 13 * (data.length) * lineWidth, 0.0, lineWidth, height); + ((0x800 & (binSet[43] << i)) == 0x800) + ? painter.color = this.foregroundColor + : painter.color = this.backgroundColor; canvas.drawRect(rect, painter); } if (hasText) { for (int i = 0; i < data.length; i++) { - TextSpan span = new TextSpan(style: new TextStyle(color: Colors.black, fontSize: 15.0), text: data[i]); - TextPainter textPainter = new TextPainter(text: span, textAlign: TextAlign.left, textDirection: TextDirection.ltr); + TextSpan span = TextSpan(style: TextStyle(color: this.foregroundColor, fontSize: 15.0), text: data[i]); + TextPainter textPainter = TextPainter(text: span, textAlign: TextAlign.left, textDirection: TextDirection.ltr); textPainter.layout(); - textPainter.paint(canvas, new Offset((size.width - data.length * 13 * lineWidth)/2 + 13*i*lineWidth, height)); + textPainter.paint(canvas, Offset((size.width - data.length * 13 * lineWidth) / 2 + 13 * i * lineWidth, height)); } } } void _drawBarCode93(Canvas canvas, Size size) { List binSet = [ - 0x8a, 0xa4, 0xa2, 0xa1, 0x94, 0x92, 0x91, 0xa8, - 0x89, 0x85, 0xd4, 0xd2, 0xd1, 0xca, 0xc9, 0xc5, - 0xb4, 0xb2, 0xb1, 0x9a, 0x8d, 0xac, 0xa6, 0xa3, - 0x96, 0x8b, 0xda, 0xd9, 0xd6, 0xd3, 0xcb, 0xcd, - 0xb6, 0xb3, 0x9b, 0x9d, 0x97, 0xea, 0xe9, 0xe5, - 0xb7, 0xbb, 0xd7, 0x93, 0xed, 0xeb, 0x99, 0xaf + 0x8a, + 0xa4, + 0xa2, + 0xa1, + 0x94, + 0x92, + 0x91, + 0xa8, + 0x89, + 0x85, + 0xd4, + 0xd2, + 0xd1, + 0xca, + 0xc9, + 0xc5, + 0xb4, + 0xb2, + 0xb1, + 0x9a, + 0x8d, + 0xac, + 0xa6, + 0xa3, + 0x96, + 0x8b, + 0xda, + 0xd9, + 0xd6, + 0xd3, + 0xcb, + 0xcd, + 0xb6, + 0xb3, + 0x9b, + 0x9d, + 0x97, + 0xea, + 0xe9, + 0xe5, + 0xb7, + 0xbb, + 0xd7, + 0x93, + 0xed, + 0xeb, + 0x99, + 0xaf ]; int codeValue = 0, checkCodeC, checkCodeK; int sumC = 0, sumK = 0; bool hasError = false; - ByteData strValue = new ByteData(data.length); - final painter = new Paint()..style = PaintingStyle.fill; + ByteData strValue = ByteData(data.length); + final painter = Paint()..style = PaintingStyle.fill; double height = hasText ? size.height * 0.85 : size.height; for (int i = 0; i < 8; i++) { - Rect rect = new Rect.fromLTWH(i*lineWidth, 0.0, lineWidth, height); - ((0x80 & (binSet[47] << i)) == 0x80) ? painter.color = Colors.black : painter.color = Colors.white; + Rect rect = Rect.fromLTWH(i * lineWidth, 0.0, lineWidth, height); + painter.color = ((0x80 & (binSet[47] << i)) == 0x80) ? this.foregroundColor : this.backgroundColor; canvas.drawRect(rect, painter); } - + for (int i = 0; i < data.length; i++) { - switch(data[i]) { - case '0': codeValue = 0; break; - case '1': codeValue = 1; break; - case '2': codeValue = 2; break; - case '3': codeValue = 3; break; - case '4': codeValue = 4; break; - case '5': codeValue = 5; break; - case '6': codeValue = 6; break; - case '7': codeValue = 7; break; - case '8': codeValue = 8; break; - case '9': codeValue = 9; break; - case 'A': codeValue = 10; break; - case 'B': codeValue = 11; break; - case 'C': codeValue = 12; break; - case 'D': codeValue = 13; break; - case 'E': codeValue = 14; break; - case 'F': codeValue = 15; break; - case 'G': codeValue = 16; break; - case 'H': codeValue = 17; break; - case 'I': codeValue = 18; break; - case 'J': codeValue = 19; break; - case 'K': codeValue = 20; break; - case 'L': codeValue = 21; break; - case 'M': codeValue = 22; break; - case 'N': codeValue = 23; break; - case 'O': codeValue = 24; break; - case 'P': codeValue = 25; break; - case 'Q': codeValue = 26; break; - case 'R': codeValue = 27; break; - case 'S': codeValue = 28; break; - case 'T': codeValue = 29; break; - case 'U': codeValue = 30; break; - case 'V': codeValue = 31; break; - case 'W': codeValue = 32; break; - case 'X': codeValue = 33; break; - case 'Y': codeValue = 34; break; - case 'Z': codeValue = 35; break; - case '-': codeValue = 36; break; - case '.': codeValue = 37; break; - case ' ': codeValue = 38; break; - case '\$': codeValue = 39; break; - case '/': codeValue = 40; break; - case '+': codeValue = 41; break; - case '%': codeValue = 42; break; + switch (data[i]) { + case '0': + codeValue = 0; + break; + case '1': + codeValue = 1; + break; + case '2': + codeValue = 2; + break; + case '3': + codeValue = 3; + break; + case '4': + codeValue = 4; + break; + case '5': + codeValue = 5; + break; + case '6': + codeValue = 6; + break; + case '7': + codeValue = 7; + break; + case '8': + codeValue = 8; + break; + case '9': + codeValue = 9; + break; + case 'A': + codeValue = 10; + break; + case 'B': + codeValue = 11; + break; + case 'C': + codeValue = 12; + break; + case 'D': + codeValue = 13; + break; + case 'E': + codeValue = 14; + break; + case 'F': + codeValue = 15; + break; + case 'G': + codeValue = 16; + break; + case 'H': + codeValue = 17; + break; + case 'I': + codeValue = 18; + break; + case 'J': + codeValue = 19; + break; + case 'K': + codeValue = 20; + break; + case 'L': + codeValue = 21; + break; + case 'M': + codeValue = 22; + break; + case 'N': + codeValue = 23; + break; + case 'O': + codeValue = 24; + break; + case 'P': + codeValue = 25; + break; + case 'Q': + codeValue = 26; + break; + case 'R': + codeValue = 27; + break; + case 'S': + codeValue = 28; + break; + case 'T': + codeValue = 29; + break; + case 'U': + codeValue = 30; + break; + case 'V': + codeValue = 31; + break; + case 'W': + codeValue = 32; + break; + case 'X': + codeValue = 33; + break; + case 'Y': + codeValue = 34; + break; + case 'Z': + codeValue = 35; + break; + case '-': + codeValue = 36; + break; + case '.': + codeValue = 37; + break; + case ' ': + codeValue = 38; + break; + case '\$': + codeValue = 39; + break; + case '/': + codeValue = 40; + break; + case '+': + codeValue = 41; + break; + case '%': + codeValue = 42; + break; default: codeValue = 0; hasError = true; @@ -218,182 +493,568 @@ class BarCodePainter extends CustomPainter { } if (hasError) { - String errorMsg = "Invalid content for Code93. Please check https://en.wikipedia.org/wiki/Code_93 for reference."; + String errorMsg = + "Invalid content for Code93. Please check https://en.wikipedia.org/wiki/Code_93 for reference."; if (this.onError != null) { this.onError(errorMsg); } else { print(errorMsg); } - return ; + return; } strValue.setUint8(i, codeValue); - sumC += strValue.getUint8(i)*(data.length-i); - sumK += strValue.getUint8(i)*(data.length-i+1); + sumC += strValue.getUint8(i) * (data.length - i); + sumK += strValue.getUint8(i) * (data.length - i + 1); for (int j = 0; j < 8; j++) { - Rect rect = new Rect.fromLTWH(9*lineWidth+9*i*lineWidth+j*lineWidth, 0.0, lineWidth, height); - ((0x80 & (binSet[codeValue] << j)) == 0x80) ? painter.color = Colors.black : painter.color = Colors.white; + Rect rect = Rect.fromLTWH(9 * lineWidth + 9 * i * lineWidth + j * lineWidth, 0.0, lineWidth, height); + ((0x80 & (binSet[codeValue] << j)) == 0x80) + ? painter.color = this.foregroundColor + : painter.color = this.backgroundColor; canvas.drawRect(rect, painter); } } - + checkCodeC = sumC % 47; for (int i = 0; i < 8; i++) { - Rect rect = new Rect.fromLTWH(9*lineWidth+(data.length*9+i)*lineWidth, 0.0, lineWidth, height); - ((0x80 & (binSet[checkCodeC]< code128 = [ - 0x6cc, 0x66c, 0x666, 0x498, 0x48c, 0x44c, 0x4c8, 0x4c4, - 0x464, 0x648, 0x644, 0x624, 0x59c, 0x4dc, 0x4ce, 0x5cc, - 0x4ec, 0x4e6, 0x672, 0x65c, 0x64e, 0x6e4, 0x674, 0x76e, - 0x74c, 0x72c, 0x726, 0x764, 0x734, 0x732, 0x6d8, 0x6c6, - 0x636, 0x518, 0x458, 0x446, 0x588, 0x468, 0x462, 0x688, - 0x628, 0x622, 0x5b8, 0x58e, 0x46e, 0x5d8, 0x5c6, 0x476, - 0x776, 0x68e, 0x62e, 0x6e8, 0x6e2, 0x6ee, 0x758, 0x746, - 0x716, 0x768, 0x762, 0x71a, 0x77a, 0x642, 0x78a, 0x530, - 0x50c, 0x4b0, 0x486, 0x42c, 0x426, 0x590, 0x584, 0x4d0, - 0x4c2, 0x434, 0x432, 0x612, 0x650, 0x7ba, 0x614, 0x47a, - 0x53c, 0x4bc, 0x49e, 0x5e4, 0x4f4, 0x4f2, 0x7a4, 0x794, - 0x792, 0x6de, 0x6f6, 0x7b6, 0x578, 0x51e, 0x45e, 0x5e8, - 0x5e2, 0x7a8, 0x7a2, 0x5de, 0x5ee, 0x75e, 0x7a2, 0x684, - 0x690, 0x69c + 0x6cc, + 0x66c, + 0x666, + 0x498, + 0x48c, + 0x44c, + 0x4c8, + 0x4c4, + 0x464, + 0x648, + 0x644, + 0x624, + 0x59c, + 0x4dc, + 0x4ce, + 0x5cc, + 0x4ec, + 0x4e6, + 0x672, + 0x65c, + 0x64e, + 0x6e4, + 0x674, + 0x76e, + 0x74c, + 0x72c, + 0x726, + 0x764, + 0x734, + 0x732, + 0x6d8, + 0x6c6, + 0x636, + 0x518, + 0x458, + 0x446, + 0x588, + 0x468, + 0x462, + 0x688, + 0x628, + 0x622, + 0x5b8, + 0x58e, + 0x46e, + 0x5d8, + 0x5c6, + 0x476, + 0x776, + 0x68e, + 0x62e, + 0x6e8, + 0x6e2, + 0x6ee, + 0x758, + 0x746, + 0x716, + 0x768, + 0x762, + 0x71a, + 0x77a, + 0x642, + 0x78a, + 0x530, + 0x50c, + 0x4b0, + 0x486, + 0x42c, + 0x426, + 0x590, + 0x584, + 0x4d0, + 0x4c2, + 0x434, + 0x432, + 0x612, + 0x650, + 0x7ba, + 0x614, + 0x47a, + 0x53c, + 0x4bc, + 0x49e, + 0x5e4, + 0x4f4, + 0x4f2, + 0x7a4, + 0x794, + 0x792, + 0x6de, + 0x6f6, + 0x7b6, + 0x578, + 0x51e, + 0x45e, + 0x5e8, + 0x5e2, + 0x7a8, + 0x7a2, + 0x5de, + 0x5ee, + 0x75e, + 0x7a2, + 0x684, + 0x690, + 0x69c ]; int codeValue, checkCode, strlen = data.length; - ByteData strValue = new ByteData(strlen); + ByteData strValue = ByteData(strlen); int sum = 0, startValue = 0x690, endFlag = 0x18eb; bool hasError = false; - final painter = new Paint()..style = PaintingStyle.fill; + final painter = Paint()..style = PaintingStyle.fill; double height = hasText ? size.height * 0.85 : size.height; for (int i = 0; i < 11; i++) { - Rect rect = new Rect.fromLTWH(i*lineWidth, 0.0, lineWidth, height); - ((0x400 & (startValue << i)) == 0x400) ? painter.color = Colors.black : painter.color = Colors.white; + Rect rect = Rect.fromLTWH(i * lineWidth, 0.0, lineWidth, height); + ((0x400 & (startValue << i)) == 0x400) + ? painter.color = this.foregroundColor + : painter.color = this.backgroundColor; canvas.drawRect(rect, painter); } for (int i = 0; i < strlen; i++) { - switch(data[i]) { - case ' ': codeValue = 0; break; case '!': codeValue = 1; break; case '"': codeValue = 2; break; - case '#': codeValue = 3; break; case '\$': codeValue = 4; break; case '%': codeValue = 5; break; - case '&': codeValue = 6; break; case '…': codeValue = 7; break; case '(': codeValue = 8; break; - case ')': codeValue = 9; break; case '*': codeValue = 10; break; case '+': codeValue = 11; break; - case ',': codeValue = 12; break; case '-': codeValue = 13; break; case '.': codeValue = 14; break; - case '/': codeValue = 15; break; case '0': codeValue = 16; break; case '1': codeValue = 17; break; - case '2': codeValue = 18; break; case '3': codeValue = 19; break; case '4': codeValue = 20; break; - case '5': codeValue = 21; break; case '6': codeValue = 22; break; case '7': codeValue = 23; break; - case '8': codeValue = 24; break; case '9': codeValue = 25; break; case ':': codeValue = 26; break; - case ';': codeValue = 27; break; case '<': codeValue = 28; break; case '=': codeValue = 29; break; - case '>': codeValue = 30; break; case '?': codeValue = 31; break; case '@': codeValue = 32; break; - case 'A': codeValue = 33; break; case 'B': codeValue = 34; break; case 'C': codeValue = 35; break; - case 'D': codeValue = 36; break; case 'E': codeValue = 37; break; case 'F': codeValue = 38; break; - case 'G': codeValue = 39; break; case 'H': codeValue = 40; break; case 'I': codeValue = 41; break; - case 'J': codeValue = 42; break; case 'K': codeValue = 43; break; case 'L': codeValue = 44; break; - case 'M': codeValue = 45; break; case 'N': codeValue = 46; break; case 'O': codeValue = 47; break; - case 'P': codeValue = 48; break; case 'Q': codeValue = 49; break; case 'R': codeValue = 50; break; - case 'S': codeValue = 51; break; case 'T': codeValue = 52; break; case 'U': codeValue = 53; break; - case 'V': codeValue = 54; break; case 'W': codeValue = 55; break; case 'X': codeValue = 56; break; - case 'Y': codeValue = 57; break; case 'Z': codeValue = 58; break; case '[': codeValue = 59; break; - case '、': codeValue = 60; break; case ']': codeValue = 61; break; case '^': codeValue = 62; break; - case '_': codeValue = 63; break; case '`': codeValue = 64; break; case 'a': codeValue = 65; break; - case 'b': codeValue = 66; break; case 'c': codeValue = 67; break; case 'd': codeValue = 68; break; - case 'e': codeValue = 69; break; case 'f': codeValue = 70; break; case 'g': codeValue = 71; break; - case 'h': codeValue = 72; break; case 'i': codeValue = 73; break; case 'j': codeValue = 74; break; - case 'k': codeValue = 75; break; case 'l': codeValue = 76; break; case 'm': codeValue = 77; break; - case 'n': codeValue = 78; break; case 'o': codeValue = 79; break; case 'p': codeValue = 80; break; - case 'q': codeValue = 81; break; case 'r': codeValue = 82; break; case 's': codeValue = 83; break; - case 't': codeValue = 84; break; case 'u': codeValue = 85; break; case 'v': codeValue = 86; break; - case 'w': codeValue = 87; break; case 'x': codeValue = 88; break; case 'y': codeValue = 89; break; - case 'z': codeValue = 90; break; case '{': codeValue = 91; break; case '|': codeValue = 92; break; - case '}': codeValue = 93; break; case '~': codeValue = 94; break; - default: hasError = true; break; + switch (data[i]) { + case ' ': + codeValue = 0; + break; + case '!': + codeValue = 1; + break; + case '"': + codeValue = 2; + break; + case '#': + codeValue = 3; + break; + case '\$': + codeValue = 4; + break; + case '%': + codeValue = 5; + break; + case '&': + codeValue = 6; + break; + case '…': + codeValue = 7; + break; + case '(': + codeValue = 8; + break; + case ')': + codeValue = 9; + break; + case '*': + codeValue = 10; + break; + case '+': + codeValue = 11; + break; + case ',': + codeValue = 12; + break; + case '-': + codeValue = 13; + break; + case '.': + codeValue = 14; + break; + case '/': + codeValue = 15; + break; + case '0': + codeValue = 16; + break; + case '1': + codeValue = 17; + break; + case '2': + codeValue = 18; + break; + case '3': + codeValue = 19; + break; + case '4': + codeValue = 20; + break; + case '5': + codeValue = 21; + break; + case '6': + codeValue = 22; + break; + case '7': + codeValue = 23; + break; + case '8': + codeValue = 24; + break; + case '9': + codeValue = 25; + break; + case ':': + codeValue = 26; + break; + case ';': + codeValue = 27; + break; + case '<': + codeValue = 28; + break; + case '=': + codeValue = 29; + break; + case '>': + codeValue = 30; + break; + case '?': + codeValue = 31; + break; + case '@': + codeValue = 32; + break; + case 'A': + codeValue = 33; + break; + case 'B': + codeValue = 34; + break; + case 'C': + codeValue = 35; + break; + case 'D': + codeValue = 36; + break; + case 'E': + codeValue = 37; + break; + case 'F': + codeValue = 38; + break; + case 'G': + codeValue = 39; + break; + case 'H': + codeValue = 40; + break; + case 'I': + codeValue = 41; + break; + case 'J': + codeValue = 42; + break; + case 'K': + codeValue = 43; + break; + case 'L': + codeValue = 44; + break; + case 'M': + codeValue = 45; + break; + case 'N': + codeValue = 46; + break; + case 'O': + codeValue = 47; + break; + case 'P': + codeValue = 48; + break; + case 'Q': + codeValue = 49; + break; + case 'R': + codeValue = 50; + break; + case 'S': + codeValue = 51; + break; + case 'T': + codeValue = 52; + break; + case 'U': + codeValue = 53; + break; + case 'V': + codeValue = 54; + break; + case 'W': + codeValue = 55; + break; + case 'X': + codeValue = 56; + break; + case 'Y': + codeValue = 57; + break; + case 'Z': + codeValue = 58; + break; + case '[': + codeValue = 59; + break; + case '、': + codeValue = 60; + break; + case ']': + codeValue = 61; + break; + case '^': + codeValue = 62; + break; + case '_': + codeValue = 63; + break; + case '`': + codeValue = 64; + break; + case 'a': + codeValue = 65; + break; + case 'b': + codeValue = 66; + break; + case 'c': + codeValue = 67; + break; + case 'd': + codeValue = 68; + break; + case 'e': + codeValue = 69; + break; + case 'f': + codeValue = 70; + break; + case 'g': + codeValue = 71; + break; + case 'h': + codeValue = 72; + break; + case 'i': + codeValue = 73; + break; + case 'j': + codeValue = 74; + break; + case 'k': + codeValue = 75; + break; + case 'l': + codeValue = 76; + break; + case 'm': + codeValue = 77; + break; + case 'n': + codeValue = 78; + break; + case 'o': + codeValue = 79; + break; + case 'p': + codeValue = 80; + break; + case 'q': + codeValue = 81; + break; + case 'r': + codeValue = 82; + break; + case 's': + codeValue = 83; + break; + case 't': + codeValue = 84; + break; + case 'u': + codeValue = 85; + break; + case 'v': + codeValue = 86; + break; + case 'w': + codeValue = 87; + break; + case 'x': + codeValue = 88; + break; + case 'y': + codeValue = 89; + break; + case 'z': + codeValue = 90; + break; + case '{': + codeValue = 91; + break; + case '|': + codeValue = 92; + break; + case '}': + codeValue = 93; + break; + case '~': + codeValue = 94; + break; + default: + hasError = true; + break; } if (hasError) { - String errorMsg = "Invalid content for Code128. Please check https://en.wikipedia.org/wiki/Code_128 for reference."; + String errorMsg = + "Invalid content for Code128. Please check https://en.wikipedia.org/wiki/Code_128 for reference."; if (this.onError != null) { this.onError(errorMsg); } else { print(errorMsg); } - return ; + return; } strValue.setUint8(i, codeValue); - sum += strValue.getUint8(i)*(i+1); + sum += strValue.getUint8(i) * (i + 1); for (int j = 0; j < 11; j++) { - Rect rect = new Rect.fromLTWH(11*lineWidth+11*i*lineWidth+j*lineWidth, 0.0, lineWidth, height); - ((0x400 & (code128[codeValue] << j)) == 0x400) ? painter.color = Colors.black : painter.color = Colors.white; + Rect rect = Rect.fromLTWH(11 * lineWidth + 11 * i * lineWidth + j * lineWidth, 0.0, lineWidth, height); + ((0x400 & (code128[codeValue] << j)) == 0x400) + ? painter.color = this.foregroundColor + : painter.color = this.backgroundColor; canvas.drawRect(rect, painter); } } checkCode = (sum + 104) % 103; for (int i = 0; i < 11; i++) { - Rect rect = new Rect.fromLTWH(11*lineWidth+(strlen*11+i)*lineWidth, 0.0, lineWidth, height); - ((0x400 & (code128[checkCode] << i)) == 0x400) ? painter.color = Colors.black : painter.color = Colors.white; + Rect rect = Rect.fromLTWH(11 * lineWidth + (strlen * 11 + i) * lineWidth, 0.0, lineWidth, height); + ((0x400 & (code128[checkCode] << i)) == 0x400) + ? painter.color = this.foregroundColor + : painter.color = this.backgroundColor; canvas.drawRect(rect, painter); } for (int i = 0; i < 13; i++) { - Rect rect = new Rect.fromLTWH(22*lineWidth+(strlen*11+i)*lineWidth, 0.0, lineWidth, height); - ((0x1000 & (endFlag << i)) == 0x1000) ? painter.color = Colors.black : painter.color = Colors.white; + Rect rect = Rect.fromLTWH(22 * lineWidth + (strlen * 11 + i) * lineWidth, 0.0, lineWidth, height); + ((0x1000 & (endFlag << i)) == 0x1000) + ? painter.color = this.foregroundColor + : painter.color = this.backgroundColor; canvas.drawRect(rect, painter); } if (hasText) { for (int i = 0; i < data.length; i++) { - TextSpan span = new TextSpan(style: new TextStyle(color: Colors.black, fontSize: 15.0), text: data[i]); - TextPainter textPainter = new TextPainter(text: span, textAlign: TextAlign.left, textDirection: TextDirection.ltr); + TextSpan span = TextSpan(style: TextStyle(color: this.foregroundColor, fontSize: 15.0), text: data[i]); + TextPainter textPainter = TextPainter(text: span, textAlign: TextAlign.left, textDirection: TextDirection.ltr); textPainter.layout(); - textPainter.paint(canvas, new Offset((size.width - data.length*8*lineWidth)/2+8*i*lineWidth, height)); + textPainter.paint(canvas, Offset((size.width - data.length * 8 * lineWidth) / 2 + 8 * i * lineWidth, height)); } } } void _drawBarCodeEAN13(Canvas canvas, Size size) { - List codeA = [0x0d, 0x19, 0x13, 0x3d, 0x23, 0x31, 0x2f, 0x3b, 0x37, 0x0b]; - List codeB = [0x27, 0x33, 0x1b, 0x21, 0x1d, 0x39, 0x05, 0x11, 0x09, 0x17]; - List codeC = [0x72, 0x66, 0x6c, 0x42, 0x5c, 0x4e, 0x50, 0x44, 0x48, 0x74]; - List flagCode = [0x00, 0x0b, 0x0d, 0x0e, 0x13, 0x19, 0x1c, 0x15, 0x17, 0x1a]; + List codeA = [0x0d, 0x19, 0x13, 0x3d, 0x23, 0x31, 0x2f, 0x3b, 0x37, 0x0b]; + List codeB = [0x27, 0x33, 0x1b, 0x21, 0x1d, 0x39, 0x05, 0x11, 0x09, 0x17]; + List codeC = [0x72, 0x66, 0x6c, 0x42, 0x5c, 0x4e, 0x50, 0x44, 0x48, 0x74]; + List flagCode = [0x00, 0x0b, 0x0d, 0x0e, 0x13, 0x19, 0x1c, 0x15, 0x17, 0x1a]; int startCodeSep = 0x05, midCodeSep = 0x0a, endCodeSep = 0x05; int tmpCode, tmpBarCode, checkCode, sum2nd, sum3rd, flagbit, strlen = data.length; - ByteData st = new ByteData(12); + ByteData st = ByteData(12); bool hasError = false; - final painter = new Paint()..style = PaintingStyle.fill; + final painter = Paint()..style = PaintingStyle.fill; double height = hasText ? size.height * 0.85 : size.height; if (strlen > 12) { @@ -410,18 +1071,21 @@ class BarCodePainter extends CustomPainter { } if (hasError) { - String errorMsg = "Invalid content for code EAN13. Please check https://en.wikipedia.org/wiki/International_Article_Number for reference."; + String errorMsg = + "Invalid content for code EAN13. Please check https://en.wikipedia.org/wiki/International_Article_Number for reference."; if (this.onError != null) { this.onError(errorMsg); } else { print(errorMsg); } - return ; + return; } for (int j = 0; j < 3; j++) { - Rect rect = new Rect.fromLTWH(11*lineWidth+j*lineWidth, 0.0, lineWidth, hasText ? height*1.08 : height); - ((0x01 & (startCodeSep >> j)) == 0x01) ? painter.color = Colors.black : painter.color = Colors.white; + Rect rect = Rect.fromLTWH(11 * lineWidth + j * lineWidth, 0.0, lineWidth, hasText ? height * 1.08 : height); + ((0x01 & (startCodeSep >> j)) == 0x01) + ? painter.color = this.foregroundColor + : painter.color = this.backgroundColor; canvas.drawRect(rect, painter); } @@ -430,16 +1094,20 @@ class BarCodePainter extends CustomPainter { if (i == 0) { flagbit = tmpCode; } else { - if ((0x20 & (flagCode[flagbit] << (i-1))) == 0) { + if ((0x20 & (flagCode[flagbit] << (i - 1))) == 0) { for (int j = 0; j < 7; j++) { - Rect rect = new Rect.fromLTWH(14*lineWidth+7*(i-1)*lineWidth+j*lineWidth, 0.0, lineWidth, height); - ((0x40 & (codeA[tmpCode] << j)) == 0x40) ? painter.color = Colors.black : painter.color = Colors.white; + Rect rect = Rect.fromLTWH(14 * lineWidth + 7 * (i - 1) * lineWidth + j * lineWidth, 0.0, lineWidth, height); + ((0x40 & (codeA[tmpCode] << j)) == 0x40) + ? painter.color = this.foregroundColor + : painter.color = this.backgroundColor; canvas.drawRect(rect, painter); } } else { for (int n = 0; n < 7; n++) { - Rect rect = new Rect.fromLTWH(14*lineWidth+7*(i-1)*lineWidth+n*lineWidth, 0.0, lineWidth, height); - ((0x40 & (codeB[tmpCode] << n)) == 0x40) ? painter.color = Colors.black : painter.color = Colors.white; + Rect rect = Rect.fromLTWH(14 * lineWidth + 7 * (i - 1) * lineWidth + n * lineWidth, 0.0, lineWidth, height); + ((0x40 & (codeB[tmpCode] << n)) == 0x40) + ? painter.color = this.foregroundColor + : painter.color = this.backgroundColor; canvas.drawRect(rect, painter); } } @@ -447,16 +1115,20 @@ class BarCodePainter extends CustomPainter { } for (int i = 0; i < 5; i++) { - Rect rect = new Rect.fromLTWH(56*lineWidth+i*lineWidth, 0.0, lineWidth, hasText ? height*1.08 : height); - ((0x01 & (midCodeSep >> i)) == 0x01) ? painter.color = Colors.black : painter.color = Colors.white; + Rect rect = Rect.fromLTWH(56 * lineWidth + i * lineWidth, 0.0, lineWidth, hasText ? height * 1.08 : height); + ((0x01 & (midCodeSep >> i)) == 0x01) + ? painter.color = this.foregroundColor + : painter.color = this.backgroundColor; canvas.drawRect(rect, painter); } for (int i = 0; i < 5; i++) { - tmpBarCode = data.codeUnitAt(i+7) - 48; + tmpBarCode = data.codeUnitAt(i + 7) - 48; for (int j = 0; j < 7; j++) { - Rect rect = new Rect.fromLTWH(61*lineWidth+j*lineWidth+7*i*lineWidth, 0.0, lineWidth, height); - ((0x40 & (codeC[tmpBarCode] << j)) == 0x40) ? painter.color = Colors.black : painter.color = Colors.white; + Rect rect = Rect.fromLTWH(61 * lineWidth + j * lineWidth + 7 * i * lineWidth, 0.0, lineWidth, height); + ((0x40 & (codeC[tmpBarCode] << j)) == 0x40) + ? painter.color = this.foregroundColor + : painter.color = this.backgroundColor; canvas.drawRect(rect, painter); } } @@ -470,14 +1142,18 @@ class BarCodePainter extends CustomPainter { } for (int i = 0; i < 7; i++) { - Rect rect = new Rect.fromLTWH(96*lineWidth+i*lineWidth, 0.0, lineWidth, height); - ((0x40 & (codeC[checkCode] << i)) == 0x40) ? painter.color = Colors.black : painter.color = Colors.white; + Rect rect = Rect.fromLTWH(96 * lineWidth + i * lineWidth, 0.0, lineWidth, height); + ((0x40 & (codeC[checkCode] << i)) == 0x40) + ? painter.color = this.foregroundColor + : painter.color = this.backgroundColor; canvas.drawRect(rect, painter); } for (int i = 0; i < 3; i++) { - Rect rect = new Rect.fromLTWH(103*lineWidth+i*lineWidth, 0.0, lineWidth, hasText ? height*1.08 : height); - ((0x01 & (endCodeSep >> i)) == 0x01) ? painter.color = Colors.black : painter.color = Colors.white; + Rect rect = Rect.fromLTWH(103 * lineWidth + i * lineWidth, 0.0, lineWidth, hasText ? height * 1.08 : height); + ((0x01 & (endCodeSep >> i)) == 0x01) + ? painter.color = this.foregroundColor + : painter.color = this.backgroundColor; canvas.drawRect(rect, painter); } @@ -485,25 +1161,30 @@ class BarCodePainter extends CustomPainter { (data.length > 13) ? strlen = 13 : strlen = data.length; for (int i = 0; i < strlen; i++) { if (i == 0) { - TextSpan span = new TextSpan(style: new TextStyle(color: Colors.black, fontSize: 15.0), text: data[i]); - TextPainter textPainter = new TextPainter(text: span, textAlign: TextAlign.left, textDirection: TextDirection.ltr); + TextSpan span = TextSpan(style: TextStyle(color: this.foregroundColor, fontSize: 15.0), text: data[i]); + TextPainter textPainter = + TextPainter(text: span, textAlign: TextAlign.left, textDirection: TextDirection.ltr); textPainter.layout(); - textPainter.paint(canvas, new Offset(2*lineWidth, height)); + textPainter.paint(canvas, Offset(2 * lineWidth, height)); } else if (i < 7) { - TextSpan span = new TextSpan(style: new TextStyle(color: Colors.black, fontSize: 15.0), text: data[i]); - TextPainter textPainter = new TextPainter(text: span, textAlign: TextAlign.left, textDirection: TextDirection.ltr); + TextSpan span = TextSpan(style: TextStyle(color: this.foregroundColor, fontSize: 15.0), text: data[i]); + TextPainter textPainter = + TextPainter(text: span, textAlign: TextAlign.left, textDirection: TextDirection.ltr); textPainter.layout(); - textPainter.paint(canvas, new Offset(13*lineWidth+7*(i-1)*lineWidth, height)); + textPainter.paint(canvas, Offset(13 * lineWidth + 7 * (i - 1) * lineWidth, height)); } else if (i == 12) { - TextSpan span = new TextSpan(style: new TextStyle(color: Colors.black, fontSize: 15.0), text: checkCode.toString()); - TextPainter textPainter = new TextPainter(text: span, textAlign: TextAlign.left, textDirection: TextDirection.ltr); + TextSpan span = + TextSpan(style: TextStyle(color: this.foregroundColor, fontSize: 15.0), text: checkCode.toString()); + TextPainter textPainter = + TextPainter(text: span, textAlign: TextAlign.left, textDirection: TextDirection.ltr); textPainter.layout(); - textPainter.paint(canvas, new Offset(17*lineWidth+7*(i-1)*lineWidth, height)); + textPainter.paint(canvas, Offset(17 * lineWidth + 7 * (i - 1) * lineWidth, height)); } else { - TextSpan span = new TextSpan(style: new TextStyle(color: Colors.black, fontSize: 15.0), text: data[i]); - TextPainter textPainter = new TextPainter(text: span, textAlign: TextAlign.left, textDirection: TextDirection.ltr); + TextSpan span = TextSpan(style: TextStyle(color: this.foregroundColor, fontSize: 15.0), text: data[i]); + TextPainter textPainter = + TextPainter(text: span, textAlign: TextAlign.left, textDirection: TextDirection.ltr); textPainter.layout(); - textPainter.paint(canvas, new Offset(17*lineWidth+7*(i-1)*lineWidth, height)); + textPainter.paint(canvas, Offset(17 * lineWidth + 7 * (i - 1) * lineWidth, height)); } } } @@ -514,9 +1195,9 @@ class BarCodePainter extends CustomPainter { List codeC = [0x72, 0x66, 0x6c, 0x42, 0x5c, 0x4e, 0x50, 0x44, 0x48, 0x74]; int startCodeSep = 0x05, midCodeSep = 0x0a, endCodeSep = 0x05; int tmpCode, tmpBarCode, checkCode, sum2nd, sum3rd, strlen = data.length; - ByteData st = new ByteData(7); + ByteData st = ByteData(7); bool hasError = false; - final painter = new Paint()..style = PaintingStyle.fill; + final painter = Paint()..style = PaintingStyle.fill; double height = hasText ? size.height * 0.85 : size.height; for (int i = 0; i < 7; i++) { @@ -527,41 +1208,50 @@ class BarCodePainter extends CustomPainter { } if (hasError) { - String errorMsg = "Invalid content for code EAN8. Please check https://en.wikipedia.org/wiki/EAN-8 for reference."; + String errorMsg = + "Invalid content for code EAN8. Please check https://en.wikipedia.org/wiki/EAN-8 for reference."; if (this.onError != null) { this.onError(errorMsg); } else { print(errorMsg); } - return ; + return; } for (int i = 0; i < 3; i++) { - Rect rect = new Rect.fromLTWH(7*lineWidth+i*lineWidth, 0.0, lineWidth, hasText ? height*1.08 : height); - ((0x01 & (startCodeSep >> i)) == 0x01) ? painter.color = Colors.black : painter.color = Colors.white; + Rect rect = Rect.fromLTWH(7 * lineWidth + i * lineWidth, 0.0, lineWidth, hasText ? height * 1.08 : height); + ((0x01 & (startCodeSep >> i)) == 0x01) + ? painter.color = this.foregroundColor + : painter.color = this.backgroundColor; canvas.drawRect(rect, painter); } for (int i = 0; i < 4; i++) { - tmpCode = data.codeUnitAt(i)-48; + tmpCode = data.codeUnitAt(i) - 48; for (int j = 0; j < 7; j++) { - Rect rect = new Rect.fromLTWH(10*lineWidth+j*lineWidth+7*i*lineWidth, 0.0, lineWidth, height); - ((0x40 & (codeA[tmpCode] << j)) == 0x40) ? painter.color = Colors.black : painter.color = Colors.white; + Rect rect = Rect.fromLTWH(10 * lineWidth + j * lineWidth + 7 * i * lineWidth, 0.0, lineWidth, height); + ((0x40 & (codeA[tmpCode] << j)) == 0x40) + ? painter.color = this.foregroundColor + : painter.color = this.backgroundColor; canvas.drawRect(rect, painter); } } for (int i = 0; i < 5; i++) { - Rect rect = new Rect.fromLTWH(38*lineWidth+i*lineWidth, 0.0, lineWidth, height); - ((0x01 & (midCodeSep >> i)) == 0x01) ? painter.color = Colors.black : painter.color = Colors.white; + Rect rect = Rect.fromLTWH(38 * lineWidth + i * lineWidth, 0.0, lineWidth, height); + ((0x01 & (midCodeSep >> i)) == 0x01) + ? painter.color = this.foregroundColor + : painter.color = this.backgroundColor; canvas.drawRect(rect, painter); } for (int i = 4; i < 7; i++) { tmpBarCode = data.codeUnitAt(i) - 48; for (int j = 0; j < 7; j++) { - Rect rect = new Rect.fromLTWH(43*lineWidth+j*lineWidth+7*(i-4)*lineWidth, 0.0, lineWidth, height); - ((0x40 & (codeC[tmpBarCode] << j)) == 0x40) ? painter.color = Colors.black : painter.color = Colors.white; + Rect rect = Rect.fromLTWH(43 * lineWidth + j * lineWidth + 7 * (i - 4) * lineWidth, 0.0, lineWidth, height); + ((0x40 & (codeC[tmpBarCode] << j)) == 0x40) + ? painter.color = this.foregroundColor + : painter.color = this.backgroundColor; canvas.drawRect(rect, painter); } } @@ -575,14 +1265,18 @@ class BarCodePainter extends CustomPainter { } for (int i = 0; i < 7; i++) { - Rect rect = new Rect.fromLTWH(64*lineWidth+i*lineWidth, 0.0, lineWidth, height); - ((0x40 & (codeC[checkCode] << i)) == 0x40) ? painter.color = Colors.black : painter.color = Colors.white; + Rect rect = Rect.fromLTWH(64 * lineWidth + i * lineWidth, 0.0, lineWidth, height); + ((0x40 & (codeC[checkCode] << i)) == 0x40) + ? painter.color = this.foregroundColor + : painter.color = this.backgroundColor; canvas.drawRect(rect, painter); } for (int i = 0; i < 3; i++) { - Rect rect = new Rect.fromLTWH(71*lineWidth+i*lineWidth, 0.0, lineWidth, hasText ? height*1.08 : height); - ((0x01 & (endCodeSep >> i)) == 0x01) ? painter.color = Colors.black : painter.color = Colors.white; + Rect rect = Rect.fromLTWH(71 * lineWidth + i * lineWidth, 0.0, lineWidth, hasText ? height * 1.08 : height); + ((0x01 & (endCodeSep >> i)) == 0x01) + ? painter.color = this.foregroundColor + : painter.color = this.backgroundColor; canvas.drawRect(rect, painter); } @@ -590,20 +1284,24 @@ class BarCodePainter extends CustomPainter { strlen > 8 ? strlen = 8 : strlen = data.length; for (int i = 0; i < strlen; i++) { if (i < 4) { - TextSpan span = new TextSpan(style: new TextStyle(color: Colors.black, fontSize: 15.0), text: data[i]); - TextPainter textPainter = new TextPainter(text: span, textAlign: TextAlign.left, textDirection: TextDirection.ltr); + TextSpan span = TextSpan(style: TextStyle(color: this.foregroundColor, fontSize: 15.0), text: data[i]); + TextPainter textPainter = + TextPainter(text: span, textAlign: TextAlign.left, textDirection: TextDirection.ltr); textPainter.layout(); - textPainter.paint(canvas, new Offset(11*lineWidth+7*i*lineWidth, height)); + textPainter.paint(canvas, Offset(11 * lineWidth + 7 * i * lineWidth, height)); } else if (i == 7) { - TextSpan span = new TextSpan(style: new TextStyle(color: Colors.black, fontSize: 15.0), text: checkCode.toString()); - TextPainter textPainter = new TextPainter(text: span, textAlign: TextAlign.left, textDirection: TextDirection.ltr); + TextSpan span = + TextSpan(style: TextStyle(color: this.foregroundColor, fontSize: 15.0), text: checkCode.toString()); + TextPainter textPainter = + TextPainter(text: span, textAlign: TextAlign.left, textDirection: TextDirection.ltr); textPainter.layout(); - textPainter.paint(canvas, new Offset(15*lineWidth+7*i*lineWidth, height)); + textPainter.paint(canvas, Offset(15 * lineWidth + 7 * i * lineWidth, height)); } else { - TextSpan span = new TextSpan(style: new TextStyle(color: Colors.black, fontSize: 15.0), text: data[i]); - TextPainter textPainter = new TextPainter(text: span, textAlign: TextAlign.left, textDirection: TextDirection.ltr); + TextSpan span = TextSpan(style: TextStyle(color: this.foregroundColor, fontSize: 15.0), text: data[i]); + TextPainter textPainter = + TextPainter(text: span, textAlign: TextAlign.left, textDirection: TextDirection.ltr); textPainter.layout(); - textPainter.paint(canvas, new Offset(15*lineWidth+7*i*lineWidth, height)); + textPainter.paint(canvas, Offset(15 * lineWidth + 7 * i * lineWidth, height)); } } } @@ -614,9 +1312,9 @@ class BarCodePainter extends CustomPainter { List codeC = [0x72, 0x66, 0x6c, 0x42, 0x5c, 0x4e, 0x50, 0x44, 0x48, 0x74]; int startCodeSep = 0x05, midCodeSep = 0x0a, endCodeSep = 0x05; int tmpCode, tmpBarCode, checkCode, sum2nd, sum3rd, strlen = data.length; - ByteData st = new ByteData(11); + ByteData st = ByteData(11); bool hasError = false; - final painter = new Paint()..style = PaintingStyle.fill; + final painter = Paint()..style = PaintingStyle.fill; double height = hasText ? size.height * 0.85 : size.height; for (int i = 0; i < 11; i++) { @@ -627,41 +1325,50 @@ class BarCodePainter extends CustomPainter { } if (hasError) { - String errorMsg = "Invalid content for coe UPC-A. Please check https://en.wikipedia.org/wiki/Universal_Product_Code for reference."; + String errorMsg = + "Invalid content for coe UPC-A. Please check https://en.wikipedia.org/wiki/Universal_Product_Code for reference."; if (this.onError != null) { this.onError(errorMsg); } else { print(errorMsg); } - return ; + return; } for (int i = 0; i < 3; i++) { - Rect rect = new Rect.fromLTWH(9*lineWidth+i*lineWidth, 0.0, lineWidth, hasText ? height*1.08 : height); - ((0x01 & (startCodeSep >> i)) == 0x01) ? painter.color = Colors.black : painter.color = Colors.white; + Rect rect = Rect.fromLTWH(9 * lineWidth + i * lineWidth, 0.0, lineWidth, hasText ? height * 1.08 : height); + ((0x01 & (startCodeSep >> i)) == 0x01) + ? painter.color = this.foregroundColor + : painter.color = this.backgroundColor; canvas.drawRect(rect, painter); } for (int i = 0; i < 6; i++) { tmpCode = data.codeUnitAt(i) - 48; for (int j = 0; j < 7; j++) { - Rect rect = new Rect.fromLTWH(12*lineWidth+7*i*lineWidth+j*lineWidth, 0.0,lineWidth, height); - ((0x40 & (codeA[tmpCode] << j)) == 0x40) ? painter.color = Colors.black : painter.color = Colors.white; + Rect rect = Rect.fromLTWH(12 * lineWidth + 7 * i * lineWidth + j * lineWidth, 0.0, lineWidth, height); + ((0x40 & (codeA[tmpCode] << j)) == 0x40) + ? painter.color = this.foregroundColor + : painter.color = this.backgroundColor; canvas.drawRect(rect, painter); } } for (int i = 0; i < 5; i++) { - Rect rect = new Rect.fromLTWH(54*lineWidth+i*lineWidth, 0.0, lineWidth, hasText ? height*1.08 : height); - ((0x01 & (midCodeSep >> i)) == 0x01) ? painter.color = Colors.black : painter.color = Colors.white; + Rect rect = Rect.fromLTWH(54 * lineWidth + i * lineWidth, 0.0, lineWidth, hasText ? height * 1.08 : height); + ((0x01 & (midCodeSep >> i)) == 0x01) + ? painter.color = this.foregroundColor + : painter.color = this.backgroundColor; canvas.drawRect(rect, painter); } for (int i = 0; i < 5; i++) { - tmpBarCode = data.codeUnitAt(i+6) - 48; + tmpBarCode = data.codeUnitAt(i + 6) - 48; for (int j = 0; j < 7; j++) { - Rect rect = new Rect.fromLTWH(59*lineWidth+j*lineWidth+7*i*lineWidth, 0.0, lineWidth, height); - ((0x40 & (codeC[tmpBarCode] << j)) == 0x40) ? painter.color = Colors.black : painter.color = Colors.white; + Rect rect = Rect.fromLTWH(59 * lineWidth + j * lineWidth + 7 * i * lineWidth, 0.0, lineWidth, height); + ((0x40 & (codeC[tmpBarCode] << j)) == 0x40) + ? painter.color = this.foregroundColor + : painter.color = this.backgroundColor; canvas.drawRect(rect, painter); } } @@ -675,14 +1382,18 @@ class BarCodePainter extends CustomPainter { } for (int i = 0; i < 7; i++) { - Rect rect = new Rect.fromLTWH(94*lineWidth+i*lineWidth, 0.0, lineWidth, height); - ((0x40 & (codeC[checkCode] << i)) == 0x40) ? painter.color = Colors.black : painter.color = Colors.white; + Rect rect = Rect.fromLTWH(94 * lineWidth + i * lineWidth, 0.0, lineWidth, height); + ((0x40 & (codeC[checkCode] << i)) == 0x40) + ? painter.color = this.foregroundColor + : painter.color = this.backgroundColor; canvas.drawRect(rect, painter); } for (int i = 0; i < 3; i++) { - Rect rect = new Rect.fromLTWH(101*lineWidth+i*lineWidth, 0.0, lineWidth, hasText ? height*1.08 : height); - ((0x01 & (endCodeSep >> i)) == 0x01) ? painter.color = Colors.black : painter.color = Colors.white; + Rect rect = Rect.fromLTWH(101 * lineWidth + i * lineWidth, 0.0, lineWidth, hasText ? height * 1.08 : height); + ((0x01 & (endCodeSep >> i)) == 0x01) + ? painter.color = this.foregroundColor + : painter.color = this.backgroundColor; canvas.drawRect(rect, painter); } @@ -690,25 +1401,30 @@ class BarCodePainter extends CustomPainter { strlen > 12 ? strlen = 12 : strlen = data.length; for (int i = 0; i < strlen; i++) { if (i == 0) { - TextSpan span = new TextSpan(style: new TextStyle(color: Colors.black, fontSize: 15.0), text: data[i]); - TextPainter textPainter = new TextPainter(text: span, textAlign: TextAlign.left, textDirection: TextDirection.ltr); + TextSpan span = TextSpan(style: TextStyle(color: this.foregroundColor, fontSize: 15.0), text: data[i]); + TextPainter textPainter = + TextPainter(text: span, textAlign: TextAlign.left, textDirection: TextDirection.ltr); textPainter.layout(); - textPainter.paint(canvas, new Offset(3*lineWidth, height)); + textPainter.paint(canvas, Offset(3 * lineWidth, height)); } else if (i < 6) { - TextSpan span = new TextSpan(style: new TextStyle(color: Colors.black, fontSize: 15.0), text: data[i]); - TextPainter textPainter = new TextPainter(text: span, textAlign: TextAlign.left, textDirection: TextDirection.ltr); + TextSpan span = TextSpan(style: TextStyle(color: this.foregroundColor, fontSize: 15.0), text: data[i]); + TextPainter textPainter = + TextPainter(text: span, textAlign: TextAlign.left, textDirection: TextDirection.ltr); textPainter.layout(); - textPainter.paint(canvas, new Offset(16*lineWidth+7*(i-1)*lineWidth, height)); + textPainter.paint(canvas, Offset(16 * lineWidth + 7 * (i - 1) * lineWidth, height)); } else if (i < 11) { - TextSpan span = new TextSpan(style: new TextStyle(color: Colors.black, fontSize: 15.0), text: data[i]); - TextPainter textPainter = new TextPainter(text: span, textAlign: TextAlign.left, textDirection: TextDirection.ltr); + TextSpan span = TextSpan(style: TextStyle(color: this.foregroundColor, fontSize: 15.0), text: data[i]); + TextPainter textPainter = + TextPainter(text: span, textAlign: TextAlign.left, textDirection: TextDirection.ltr); textPainter.layout(); - textPainter.paint(canvas, new Offset(26*lineWidth+7*(i-1)*lineWidth, height)); + textPainter.paint(canvas, Offset(26 * lineWidth + 7 * (i - 1) * lineWidth, height)); } else { - TextSpan span = new TextSpan(style: new TextStyle(color: Colors.black, fontSize: 15.0), text: checkCode.toString()); - TextPainter textPainter = new TextPainter(text: span, textAlign: TextAlign.left, textDirection: TextDirection.ltr); + TextSpan span = + TextSpan(style: TextStyle(color: this.foregroundColor, fontSize: 15.0), text: checkCode.toString()); + TextPainter textPainter = + TextPainter(text: span, textAlign: TextAlign.left, textDirection: TextDirection.ltr); textPainter.layout(); - textPainter.paint(canvas, new Offset(35*lineWidth+7*(i-1)*lineWidth, height)); + textPainter.paint(canvas, Offset(35 * lineWidth + 7 * (i - 1) * lineWidth, height)); } } } @@ -720,18 +1436,18 @@ class BarCodePainter extends CustomPainter { List checkCodeFlag = [0x38, 0x34, 0x32, 0x31, 0x2c, 0x26, 0x23, 0x2a, 0x29, 0x25]; int startCodeSep = 0x05, endCodeSep = 0x15; int tmpCode, checkCode, sum2nd, sum3rd, strlen; - ByteData st = new ByteData(11); + ByteData st = ByteData(11); bool hasError = false; - final painter = new Paint()..style = PaintingStyle.fill; + final painter = Paint()..style = PaintingStyle.fill; double height = hasText ? size.height * 0.85 : size.height; if (data.length != 8) { hasError = true; - } + } String upce2upca; - switch(data.codeUnitAt(6) - 48) { + switch (data.codeUnitAt(6) - 48) { case 0: upce2upca = data[0] + data[1] + data[2] + '00000' + data[3] + data[4] + data[5]; break; @@ -757,7 +1473,7 @@ class BarCodePainter extends CustomPainter { } for (int i = 0; i < 11; i++) { - st.setUint8(i, upce2upca.codeUnitAt(i)-48); + st.setUint8(i, upce2upca.codeUnitAt(i) - 48); if (st.getUint8(i) > 9) { hasError = true; break; @@ -765,18 +1481,21 @@ class BarCodePainter extends CustomPainter { } if (hasError) { - String errorMsg = "Invalid content for code UPCE. Please check https://en.wikipedia.org/wiki/Universal_Product_Code#UPC-E for reference."; + String errorMsg = + "Invalid content for code UPCE. Please check https://en.wikipedia.org/wiki/Universal_Product_Code#UPC-E for reference."; if (this.onError != null) { this.onError(errorMsg); } else { print(errorMsg); } - return ; + return; } for (int i = 0; i < 3; i++) { - Rect rect = new Rect.fromLTWH(8*lineWidth+i*lineWidth, 0.0, lineWidth, hasText ? height*1.08 : height); - ((0x04 & (startCodeSep << i)) == 0x04) ? painter.color = Colors.black : painter.color = Colors.white; + Rect rect = Rect.fromLTWH(8 * lineWidth + i * lineWidth, 0.0, lineWidth, hasText ? height * 1.08 : height); + ((0x04 & (startCodeSep << i)) == 0x04) + ? painter.color = this.foregroundColor + : painter.color = this.backgroundColor; canvas.drawRect(rect, painter); } @@ -789,25 +1508,29 @@ class BarCodePainter extends CustomPainter { } for (int i = 0; i < 6; i++) { - tmpCode = data.codeUnitAt(i+1) - 48; + tmpCode = data.codeUnitAt(i + 1) - 48; if ((0x20 & (checkCodeFlag[checkCode] << i)) == 0x20) { for (int j = 0; j < 7; j++) { - Rect rect = new Rect.fromLTWH(11*lineWidth+7*i*lineWidth+j*lineWidth, 0.0, lineWidth, height); - ((0x40 & (codeB[tmpCode] << j)) == 0x40) ? painter.color = Colors.black : painter.color = Colors.white; + Rect rect = Rect.fromLTWH(11 * lineWidth + 7 * i * lineWidth + j * lineWidth, 0.0, lineWidth, height); + ((0x40 & (codeB[tmpCode] << j)) == 0x40) + ? painter.color = this.foregroundColor + : painter.color = this.backgroundColor; canvas.drawRect(rect, painter); } } else { for (int k = 0; k < 7; k++) { - Rect rect = new Rect.fromLTWH(11*lineWidth+7*i*lineWidth+k*lineWidth, 0.0, lineWidth, height); - ((0x40 & (codeA[tmpCode] << k)) == 0x40) ? painter.color = Colors.black : painter.color = Colors.white; + Rect rect = Rect.fromLTWH(11 * lineWidth + 7 * i * lineWidth + k * lineWidth, 0.0, lineWidth, height); + painter.color = ((0x40 & (codeA[tmpCode] << k)) == 0x40) ? this.foregroundColor : this.backgroundColor; canvas.drawRect(rect, painter); } } } for (int i = 0; i < 6; i++) { - Rect rect = new Rect.fromLTWH(53*lineWidth+i*lineWidth, 0.0, lineWidth, hasText ? height*1.08 : height); - ((0x20 & (endCodeSep << i)) == 0x20) ? painter.color = Colors.black : painter.color = Colors.white; + Rect rect = Rect.fromLTWH(53 * lineWidth + i * lineWidth, 0.0, lineWidth, hasText ? height * 1.08 : height); + ((0x20 & (endCodeSep << i)) == 0x20) + ? painter.color = this.foregroundColor + : painter.color = this.backgroundColor; canvas.drawRect(rect, painter); } @@ -815,20 +1538,44 @@ class BarCodePainter extends CustomPainter { (data.length > 8) ? strlen = 8 : strlen = data.length; for (int i = 0; i < strlen; i++) { if (i == 0) { - TextSpan span = new TextSpan(style: new TextStyle(color: Colors.black, fontSize: 15.0), text: data[i]); - TextPainter textPainter = new TextPainter(text: span, textAlign: TextAlign.left, textDirection: TextDirection.ltr); + TextSpan span = TextSpan( + style: TextStyle(color: this.foregroundColor, fontSize: 15.0), + text: data[i], + ); + TextPainter textPainter = TextPainter( + text: span, + textAlign: TextAlign.left, + textDirection: TextDirection.ltr, + ); textPainter.layout(); - textPainter.paint(canvas, new Offset(2*lineWidth, height)); + textPainter.paint(canvas, Offset(2 * lineWidth, height)); } else if (i < 7) { - TextSpan span = new TextSpan(style: new TextStyle(color: Colors.black, fontSize: 15.0), text: data[i]); - TextPainter textPainter = new TextPainter(text: span, textAlign: TextAlign.left, textDirection: TextDirection.ltr); + TextSpan span = TextSpan( + style: TextStyle(color: this.foregroundColor, fontSize: 15.0), + text: data[i], + ); + TextPainter textPainter = TextPainter( + text: span, + textAlign: TextAlign.left, + textDirection: TextDirection.ltr, + ); textPainter.layout(); - textPainter.paint(canvas, new Offset(12*lineWidth+7*(i-1)*lineWidth, height)); + textPainter.paint( + canvas, + Offset(12 * lineWidth + 7 * (i - 1) * lineWidth, height), + ); } else { - TextSpan span = new TextSpan(style: new TextStyle(color: Colors.black, fontSize: 15.0), text: checkCode.toString()); - TextPainter textPainter = new TextPainter(text: span, textAlign: TextAlign.left, textDirection: TextDirection.ltr); + TextSpan span = TextSpan( + style: TextStyle(color: this.foregroundColor, fontSize: 15.0), + text: checkCode.toString(), + ); + TextPainter textPainter = TextPainter( + text: span, + textAlign: TextAlign.left, + textDirection: TextDirection.ltr, + ); textPainter.layout(); - textPainter.paint(canvas, new Offset(60*lineWidth, height)); + textPainter.paint(canvas, Offset(60 * lineWidth, height)); } } } diff --git a/pubspec.yaml b/pubspec.yaml index 51f9aef..fd2644c 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -1,7 +1,7 @@ name: barcode_flutter description: > Barcode flutter is a Flutter library for simple and fast Bar code rendering via a custom painter. -version: 1.0.3 +version: 2.0.0 author: Colin Chen homepage: https://github.com/bigship/barcode.flutter