美文网首页
【Flutter】二维码生成,Widget转图片,图片保存相册

【Flutter】二维码生成,Widget转图片,图片保存相册

作者: 在这蓝色天空下 | 来源:发表于2020-09-17 10:18 被阅读0次

    【Flutter】二维码生成,Widget转图片,图片保存相册
    https://blog.csdn.net/tianzhilan0/article/details/108519768

    1、生成二维码

    使用qr_flutter:https://pub.dev/packages/qr_flutter

        Container(
          padding: EdgeInsets.only(
              left: MediaQuery.of(context).size.width / 5,
              right: MediaQuery.of(context).size.width / 5),
          child: QrImage(
            data: 'This QR code has an embedded image as well',
            version: QrVersions.auto,
            gapless: false,
            embeddedImage: AssetImage('assets/images/demo_head_f02.jpg'),
            embeddedImageStyle: QrEmbeddedImageStyle(
              size: Size(80, 80),
            ),
          ),
        )
    

    2、Widget转图片

    
    class WidgetToImage extends StatefulWidget {
      WidgetToImage({Key key}) : super(key: key);
    
      @override
      _WidgetToImageState createState() => _WidgetToImageState();
    }
    
    class _WidgetToImageState extends State<WidgetToImage> {
      GlobalKey _globalKey = new GlobalKey();
    
      @override
      Widget build(BuildContext context) {
        return RepaintBoundary(
            key: _globalKey,
            child: Column(children: [
              Container(
                margin: EdgeInsets.only(top: 10, bottom: 20),
                child: Text(
                  "味多美A店会员注册",
                  style: Theme.of(context)
                      .textTheme
                      .headline2
                      .copyWith(fontSize: 20, fontWeight: FontWeight.w500),
                ),
              ),
              Container(
                padding: EdgeInsets.only(
                    left: MediaQuery.of(context).size.width / 5,
                    right: MediaQuery.of(context).size.width / 5),
                child: QrImage(
                  data: 'This QR code has an embedded image as well',
                  version: QrVersions.auto,
                  gapless: false,
                  embeddedImage: AssetImage('assets/images/demo_head_f02.jpg'),
                  embeddedImageStyle: QrEmbeddedImageStyle(
                    size: Size(80, 80),
                  ),
                ),
              ),
            ]));
      }
    
      Future<Uint8List> _capturePng() async {
        try {
          print('inside');
          RenderRepaintBoundary boundary =
              _globalKey.currentContext.findRenderObject();
          ui.Image image = await boundary.toImage(pixelRatio: 3.0);
          ByteData byteData =
              await image.toByteData(format: ui.ImageByteFormat.png);
          Uint8List pngBytes = byteData.buffer.asUint8List();
          return pngBytes;
        } catch (e) {
          return Uint8List(10);
        }
      }
    }
    
    

    3、图片保存至相册

    图片保存至相册:https://blog.csdn.net/tianzhilan0/article/details/108278021

    相关文章

      网友评论

          本文标题:【Flutter】二维码生成,Widget转图片,图片保存相册

          本文链接:https://www.haomeiwen.com/subject/gyjcyktx.html