【问题标题】:how to save my QR generated image into gallery? using Flutter如何将我生成的 QR 图像保存到图库中?使用颤振
【发布时间】:2021-04-14 01:34:36
【问题描述】:

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

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

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

正在生成二维码的代码块:

RepaintBoundary( key: globalKey,child: QrImage(data: _dataString,size: 0.3 * bodyHeight,), );

【问题讨论】:

    标签: flutter dart flutter-layout dart-pub


    【解决方案1】:

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

    Container(
        color: Colors.white,
        child: QrImage(....
    )
    

    【讨论】:

    • 你为我节省了很多时间。谢谢。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-11-05
    • 1970-01-01
    • 2016-10-06
    • 1970-01-01
    • 2017-06-21
    • 2022-01-17
    相关资源
    最近更新 更多