我正在使用qr_flutter创建QrImage。可以,但是我想将QrImage转换为图像,以便创建要在打印机上打印的PDF文件。请帮忙!

QrImage(
  data: qrString,
  size: 300.0,
  version: 10,
  backgroundColor: Colors.white,
),

最佳答案

使用带有键的 RepaintBoundary 小部件将小部件导出为b64字符串,然后可以将其导出为图像。

例:

Future<Uint8List> _getWidgetImage() async {
 try {
   RenderRepaintBoundary boundary =
      _renderObjectKey.currentContext.findRenderObject();
   ui.Image image = await boundary.toImage(pixelRatio: 3.0);
   ByteData byteData =
      await image.toByteData(format: ui.ImageByteFormat.png);
   var pngBytes = byteData.buffer.asUint8List();
   var bs64 = base64Encode(pngBytes);
   debugPrint(bs64.length.toString());
   return pngBytes;
 } catch (exception) {}

}
@override
Widget build(BuildContext context) {
    return Scaffold(
        body: Column(children: [
          RepaintBoundary(
            key: _renderObjectKey,
            child: QrImage(
            data: "some text",
            size: 300.0,
            version: 10,
            backgroundColor: Colors.white,
         ),
       ),
       RaisedButton(onPressed: () {
         _getWidgetImage();
       })
     ]));

}

关于image - Flutter qrImage转换为Image,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/53078493/

10-08 23:26