imagedartflutterqr-code

Flutter qrImage convert to Image


I'm using qr_flutter to create QrImage. It's ok but I would like to convert QrImage into image in order to create a PDF file to print on the printer. Please kindly help!

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

Solution

  • Use a RepaintBoundary widget with a key to export the widget to a a b64 string which then you can export as an image.

    Example:

    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();
           })
         ]));
    

    }