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

【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

©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

友情链接更多精彩内容