【问题标题】:Generate and print barcode by flutterFlutter生成并打印条码
【发布时间】:2022-01-05 08:53:27
【问题描述】:

我需要生成条码,最后打印生成的条码。但不知道如何打印。

代码如下:

  var BarcodeController = TextEditingController();

 Widget build(BuildContext context) {
    var pageWidth = MediaQuery.of(context).size.width;
    var pageHeight = MediaQuery.of(context).size.height;
    return Scaffold(
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.spaceEvenly,
          children: [
            Container(
              height: pageHeight/4,
              width: pageWidth,
              child: SfBarcodeGenerator(
                value: BarcodeController.text,
                showValue: true,
                textSpacing: pageWidth/20,
              ),
            ),
            Padding(
              padding: EdgeInsets.symmetric(horizontal: pageWidth/20),
            child: Container(
              padding: EdgeInsets.symmetric(horizontal: pageWidth/15),
              decoration: BoxDecoration(
                  color: Colors.black12,
                  borderRadius: BorderRadius.circular(15)
              ),
              child:  TextField(
                textAlign: TextAlign.center,
                controller: BarcodeController,
                decoration: InputDecoration(
                  border: InputBorder.none,
                ),
              ),
            ),
            )
          ],
        )
      ),
    );
  }

我使用这个包生成条形码:syncfusion_flutter_barcodes package 现在我需要用真正的打印机在真正的纸上打印这个条形码。那么有什么要打印的包吗?以及我怎样才能通过那个包裹打印这个条形码?

【问题讨论】:

  • 您可以使用这个库barcode: ^2.2.0轻松生成和打印条形码

标签: android ios flutter dart


【解决方案1】:

您可以捕获Widget包含二维码,将此图像保存到路径。然后,您使用带有此库printing 的 iOS 或 Android 设备的服务打印文档:

final doc = pw.Document();
final image = await imageFromAssetBundle('assets/image.png');

doc.addPage(pw.Page(
build: (pw.Context context) {
  return pw.Center(
    child: pw.Image(image),
  ); // Center
})); // Page
await Printing.layoutPdf(onLayout: (PdfPageFormat format) async => doc.save());

关于捕获小部件的详细信息,请参阅以下说明

static GlobalKey previewContainer = new GlobalKey();

//
RepaintBoundary(
          key: previewContainer,
          child: SfBarcodeGenerator()//
        )

//
Future<String> takeScreenShot() async {
  RenderRepaintBoundary boundary = previewContainer.currentContext.findRenderObject();
  ui.Image image = await boundary.toImage();
  // final directory = (await getApplicationDocumentsDirectory()).path;
  ByteData byteData = await image.toByteData(format: ui.ImageByteFormat.png);
  Uint8List pngBytes = byteData.buffer.asUint8List();
  print(pngBytes);
  String imagePath = join((await getTemporaryDirectory()).path, '${timestamp()}.png');
  File imgFile = new File(imagePath);
  // File imgFile = new File('$directory/'+'${timestamp()}.png');
  imgFile.writeAsBytes(pngBytes);
  return imagePath;
}

String timestamp() => DateTime.now().millisecondsSinceEpoch.toString();

【讨论】:

  • 是否可以在不保存到路径的情况下执行此操作?还是建议您在保存后将其删除?理想情况下,我很担心权限问题,我宁愿只打印生成的二维码,而不必将其保存到设备中。
猜你喜欢
  • 2023-02-14
  • 2019-10-20
  • 2016-11-01
  • 2021-03-03
  • 1970-01-01
  • 1970-01-01
  • 2013-10-05
  • 2019-08-15
  • 2015-12-23
相关资源
最近更新 更多