Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to save my QR generated image into gallery? using Flutter

I am trying to generate an image using RenderRepaintBoundary and want to save the image in my local directory.
I am able to save the image but I get a black image instead of a QR image.

Code block where I am writing the image to the directory:

try {
    RenderRepaintBoundary boundary =  
        globalKey.currentContext.findRenderObject();  
    var image = await boundary.toImage();
    
    ByteData byteData = await image.toByteData(format: ImageByteFormat.png);
    
    Uint8List pngBytes = byteData.buffer.asUint8List();
         
    final file =
              await new File('/<localpath>/image.png').create();
          
    await file.writeAsBytes(pngBytes);
} catch (e) {
    print(e);    
}
    

Code block where QR code is being generated:

RepaintBoundary( key: globalKey,child: QrImage(data: _dataString,size: 0.3 * bodyHeight,), );
like image 785
Jayesh Choudhary Avatar asked Oct 28 '25 03:10

Jayesh Choudhary


1 Answers

This is caused by having transparent background; Simple solution is to wrap your QrImage with Container that has white backround:

Container(
    color: Colors.white,
    child: QrImage(....
)
like image 77
Nuts Avatar answered Oct 30 '25 00:10

Nuts