drawBarCode method

  1. @override
dynamic drawBarCode(
  1. String context,
  2. PrinterOffset offset,
  3. int height, [
  4. int lineWidth = 1,
])
override

打印二维码

context 内容

offset 偏移位置

height 高度

lineWidth 窄条的单位宽度,默认1

Implementation

@override
drawBarCode(String context, PrinterOffset offset, int height, [int lineWidth = 1]) {
  var barcode = Code128(context);
  var bits = barcode.getEncoding();

  Paint paint = Paint();
  paint.color = Color(0xFF000000);
  double x = offset.x!;
  lineWidth = lineWidth + 1;
  for (int i = 0; i < bits.length; i++) {
    if (bits[i] == '1') {
      PrinterRect rect = PrinterRect.fromLTWH(
          x, offset.y!, lineWidth.toDouble(), height.toDouble());
      canvas!.drawRect(Rect.fromLTRB(rect.left, rect.top, rect.right, rect.bottom), paint);
    }
    x += lineWidth;
  }
}