如何将我的二维码生成的图像保存到图库中?使用颤振

Jay*_*ary 4 dart dart-pub flutter flutter-layout

我正在尝试使用生成图像RenderRepaintBoundary并希望将图像保存在我的本地目录中。
我可以保存图像,但得到的是黑色图像而不是二维图像。

我将图像写入目录的代码块:

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);    
}
    
Run Code Online (Sandbox Code Playgroud)

生成QR码的代码块:

RepaintBoundary( key: globalKey,child: QrImage(data: _dataString,size: 0.3 * bodyHeight,), );
Run Code Online (Sandbox Code Playgroud)

Nut*_*uts 6

这是由于背景透明造成的;简单的解决方案是用具有白色背景的容器包装 QrImage:

Container(
    color: Colors.white,
    child: QrImage(....
)
Run Code Online (Sandbox Code Playgroud)