当前位置: 首页 > news >正文

flutter生成二维码并截图保存到图库

在这里插入图片描述在这里插入图片描述

引入库:flutter_screenutil、image_gallery_saver、qr_flutter弹窗布局

import 'dart:async';
import 'dart:typed_data';
import 'package/generated/l10n.dart';
import 'package:jade/configs/PathConfig.dart';
import 'package:jade/utils/ImageWaterMarkUtil.dart';
import 'package:jade/utils/JadeColors.dart';
import 'package:jade/utils/Utils.dart';
import 'package:util/easy_loading_util.dart';
import 'package:util/navigator_util.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/rendering.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:image_gallery_saver/image_gallery_saver.dart';
import 'package:qr_flutter/qr_flutter.dart';import 'dart:ui' as ui;
/*
* 普通说明弹窗(可隐藏取消按钮)
* */
Widget productQrcodeDialog (BuildContext context,{ String descTitle,String subTitle,String desc,String qrStr,GlobalKey globalKey,Function callback,Function cancelCallback,bool showCancel = false,String sureBtnText,Color sureBtnTextColor}){return UnconstrainedBox(child: Container(alignment: Alignment.center,width: Utils().screenWidth(context)*0.8,decoration: BoxDecoration(color: Colors.white,borderRadius: BorderRadius.circular(20),),child: Column(mainAxisSize: MainAxisSize.min,children: [if(descTitle != null)Container(margin: EdgeInsets.only(top: 50.w),child: Text(descTitle??'',style: TextStyle(color: JadeColors.grey_2,fontSize: 38.sp,fontWeight: FontWeight.w600))),if(subTitle != null)Container(margin: EdgeInsets.only(top: 20.w),child: Text(subTitle,style: TextStyle(color: JadeColors.grey,fontSize: 24.sp))),if(desc != null)Container(margin: EdgeInsets.only(top: 30.w,bottom: 30.w),padding: EdgeInsets.symmetric(horizontal: 30.w),child: Text(desc,style: TextStyle(color: JadeColors.grey_2,fontSize: 30.sp),maxLines: 300,textAlign: TextAlign.center,)),RepaintBoundary(key: globalKey,child: Container(width: Utils().screenWidth(context)*0.6,height: Utils().screenWidth(context)*0.6,decoration: BoxDecoration(color: Colors.white,borderRadius: BorderRadius.circular(10),border: Border.all(width: 1,color: JadeColors.blue_2)),child: Stack(alignment: Alignment.center,children: [QrImage(padding: EdgeInsets.all(11.w),data: qrStr,version: QrVersions.auto,size: Utils().screenWidth(context) * 0.6,),Container(width: 60.w,height: 60.w,decoration: BoxDecoration(color: Colors.white,borderRadius: BorderRadius.circular(5),border: Border.all(width: 1.w,color:Colors.white)),child: Utils().roundedImage(PathConfig.httpAppLogo, 5),)],)),),Container(margin: EdgeInsets.only(top: 40.w),height: 0.5,color: JadeColors.grey_13,),Container(height: 92.w,child: Row(children: [if(showCancel)Expanded(child: GestureDetector(child: Container(color: Colors.transparent,alignment: Alignment.center,child: Text(S.current.quxiao,style: TextStyle(color: JadeColors.grey,fontSize: 34.sp,fontWeight: FontWeight.w300))),onTap: (){NavigatorUtil.pop();if(cancelCallback != null){cancelCallback();}},)),if(showCancel)Container(width: 0.5,color: JadeColors.grey_13,height: double.infinity,),Expanded(child: GestureDetector(child: Container(alignment: Alignment.center,color: Colors.transparent,child: Text(sureBtnText??S.current.close,style: TextStyle(color: sureBtnTextColor??Colors.blue,fontSize: 34.sp,fontWeight: FontWeight.w600),textAlign: TextAlign.center),),onTap: () async {_saveScreenshot(globalKey);if(callback != null){callback();}},)),],),)],),));
}Future<Uint8List> takeScreenshot(GlobalKey _globalKey) async {try {RenderRepaintBoundary boundary = _globalKey.currentContext.findRenderObject() as RenderRepaintBoundary;ui.Image image = await boundary.toImage(pixelRatio: 3.0); // 调整分辨率以适应高像素密度设备ByteData byteData = await image.toByteData(format: ui.ImageByteFormat.png);if (byteData != null) {Uint8List pngBytes = byteData.buffer.asUint8List();return pngBytes;}} catch (e) {print(e);}return null;
}_saveScreenshot(GlobalKey globalKey) async {Uint8List screenshotBytes = await takeScreenshot(globalKey);if (screenshotBytes != null) {final result = await ImageGallerySaver.saveImage(screenshotBytes);print('result= $result');// 打印保存结果esLoadingToast('已保存截图至本地');}else{esLoadingToast('截图保存失败');}NavigatorUtil.pop();
}

弹窗

 /** 产品二维码弹窗* */Future<void> productQrCodeDialog(BuildContext context,{String descTitle,String subTitle,String desc,String qrStr,GlobalKey globalKey,Function callback,Function cancelCallback,bool showCancel = false,String sureBtnText,Color sureBtnTextColor}) async {showDialog(context: context,barrierDismissible: false,builder: (BuildContext context) {return productQrcodeDialog(context,descTitle: descTitle,subTitle: subTitle,desc: desc,qrStr: qrStr,globalKey: globalKey,showCancel: showCancel,sureBtnText: sureBtnText,sureBtnTextColor:sureBtnTextColor,callback: callback,cancelCallback : cancelCallback);});}

调用

GlobalKey _globalKey = new GlobalKey();
GestureDetector(
child:_btnView(),
onTap: (){
productQrCodeDialog(context,descTitle: '产品二维码',desc: '请保存下载该二维码并打印,随同产品一起寄送至站点。',qrStr: '二维码的内容',globalKey: _globalKey,showCancel: true,sureBtnText: '下载',sureBtnTextColor: JadeColors.grey_2,callback: (){});}
}
)
http://www.lryc.cn/news/327348.html

相关文章:

  • EasyExcel Converter实现java对象和excel单元格转换
  • stamac Ethernet DTS配置
  • Svg Flow Editor 原生svg流程图编辑器(四)
  • Verilog语法之assign语句学习
  • Cocos2dx-lua ScrollView[三]高级篇
  • 后端之卡尔曼滤波
  • Docker 夺命连环 15 问
  • 2024最新版克魔助手抓包教程(9) - 克魔助手 IOS 数据抓包
  • Spring Boot 防止XSS攻击
  • aidl文件生成Java、C++[android]、C++[ndk]、Rust接口
  • 多源统一视频融合可视指挥调度平台VMS/smarteye系统概述
  • PyTorch简介:与TensorFlow的比较
  • 虚拟机-从头配置Ubuntu18.04(包括anaconda,cuda,cudnn,pycharm,ros,vscode)
  • uniApp使用XR-Frame创建3D场景(8)粒子系统
  • 【JMeter入门】—— JMeter介绍
  • C# 多线程编程:线程锁与无锁并发
  • React.FC
  • 使用pytorch构建一个无监督的深度卷积GAN网络模型
  • [AI]文心一言出圈的同时,NLP处理下的ChatGPT-4.5最新资讯
  • vue.js设计与实现(分支切换与cleanup)
  • 206基于matlab的无人机航迹规划(UAV track plannin)
  • 【Linux 】查看veth-pair对的映射关系
  • Cisco Firepower FMCv修改管理Ip方法
  • PHP开发全新29网课交单平台源码修复全开源版本,支持聚合登陆易支付
  • 【Web前端】CSS基本语法规范和引入方式常见选择器用法常见元素属性
  • SnapGene 5 for Mac 分子生物学软件
  • 本地部署大模型的几种工具(上-相关使用)
  • Spring Boot集成itext实现html生成PDF功能
  • Java 多态、包、final、权限修饰符、静态代码块
  • 基于Spring boot + Vue协同过滤算法的电影推荐系统