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.mzph.cn/news/782970.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

瑞吉外卖实战学习--8、人员禁用和启用

前言 1、通过前端页面查看接口 会发现请求方式是put 请求接口是employee 2、检查页面传值 根据浏览器的请求可以看到传值为id和status 2、写put请求&#xff0c;添加修改时间和修改人的id然后传回给后台 /*** 启用和禁用员工账号* param request* param employee* return…

【Consul】Linux安装Consul保姆级教程

【Consul】Linux安装Consul保姆级教程 大家好 我是寸铁&#x1f44a; 总结了一篇【Consul】Linux安装Consul保姆级教程✨ 喜欢的小伙伴可以点点关注 &#x1f49d; 前言 今天要把编写的go程序放到linux上进行测试Consul服务注册与发现&#xff0c;那怎么样才能实现这一过程&am…

25Ramdisk 启动模式简介

Ramdisk 启动模式简介 ramdisk是一种虚拟磁盘技术,我们的PE系统几乎都是使用ramdisk方式从计算机启动的.那么,ramdisk有哪些特点呢? Ramdisk 将内存虚拟为一个磁盘 Ramdisk技术会将你的一部分内存虚拟成一块磁盘分区.使用U盘启动pe系统时,打开pe系统里的文件资源管理器,你会看…

CKS之安全沙箱运行容器:gVisor

目录 一、gVisor介绍 二、gVisor架构 三、gVisor使用前置条件 四、Docker中使用gVisor 五、containerd中使用gVisor 六、Kubernetes结合gVisor使用 一、gVisor介绍 gVisor是Google开源的一种容器沙箱技术&#xff0c;其设计初衷是在提供较高安全性的同时&#xff0c;尽量…

从输入url到页面展示的过程

唠唠叨&#xff1a;我不想误人子弟&#xff0c;我这篇算是搬运工&#xff0c;加上自己的理解做点总结&#xff0c;所以还请大家科学上网去看这篇&#xff1a;https://aws.amazon.com/cn/blogs/mobile/what-happens-when-you-type-a-url-into-your-browser/ 是这六个步骤&#…

杰发科技——Jlink插件使用

0. 简介 杰发自带的烧录工具是ATCLink&#xff0c;基于DapLink适配。个人不太喜欢ATCLink&#xff0c;推荐使用Jlink&#xff0c;毕竟自己买&#xff0c;不用问原厂要&#xff0c;而且带Jlink&#xff0c;至少5Mhz以上。 V9烧录器使用7.50以下版本驱动。 V11烧录器可以使用7…

热烈祝贺阿里云PolarDB登顶2024最新一期中国数据库流行榜

热烈祝贺阿里云PolarDB登顶2024最新一期中国数据库流行榜 墨天轮墨天轮国产数据库流行度排行PolarDB首度夺魁关于话题的讨论数据库流行度排行榜会影响你的数据库选型吗&#xff1f;对于 PolarDB 的本次登顶&#xff0c;你认为关键因素是什么&#xff1f;PolarDB“三层分离”新版…

【Review】小米汽车发布会

动力方面 顶配双电机四驱&#xff0c;小米超级电机21000转&#xff0c;最大马力673ps&#xff0c;峰值功率495kW&#xff0c;峰值扭矩838N.m。风阻系数最低0.195&#xff0c;零百加速最快2.78秒&#xff0c;时速最高265km/h。 底盘方面 前双叉臂&#xff0c;后无五连杆&#xf…

C++—vector的介绍及使用 vector的模拟实现

提示&#xff1a;文章写完后&#xff0c;目录可以自动生成&#xff0c;如何生成可参考右边的帮助文档 目录 文章目录 前言 一、vector的介绍及使用 1.1 vector的介绍 1.2 vector的使用 1.2.1 vector的定义 1.2.2 vector iterator 的使用 1.2.3 vector 空间增长问题 1.2.4 vecto…

【C语言】贪吃蛇【附源码】

欢迎来到英杰社区https://bbs.csdn.net/topics/617804998 一、游戏说明&#xff1a; 一个基于C语言链表开发的贪吃蛇游戏&#xff1a; 1. 按方向键上下左右&#xff0c;可以实现蛇移动方向的改变。 2. 短时间长按方向键上下左右其中之一&#xff0c;可实现蛇向该方向的短时间…

Unity 基于Rigidbody2D模块的角色移动

制作好站立和移动的动画后 控制器设计 站立 移动 角色移动代码如下&#xff1a; using System.Collections; using System.Collections.Generic; using Unity.VisualScripting; using UnityEngine;public class p1_c : MonoBehaviour {// 获取动画组件private Animator …

最短路径——Floyd算法、Dijkstra算法(未完...)

这里写目录标题 例题引入&#xff1a; 路径——蓝桥2021省赛题目分析题解&#xff01;&#xff01;&#xff01;求最短路径问题&#xff01;&#xff01;&#xff01;应用场景图的基础Floyd算法Acwing-843.有边数限制的最短路简单的思路讲解 Dijkstra算法 例题引入&#xff1a;…

车载以太网AVB交换机 gptp透明时钟 8口 千兆/百兆可切换 SW1100TR

SW1100TR车载以太网交换机 一、产品简要分析 8端口千兆和百兆混合车载以太网交换机&#xff0c;其中包含2个通道的1000BASE-T1采用罗森博格H-MTD接口&#xff0c;5通道100BASE-T1泰科MATEnet接口和1个通道1000BASE-T标准以太网(RJ45接口)&#xff0c;可以实现车载以太网多通道…

Acrobat Pro DC 2023 for Mac PDF编辑管理软件

Acrobat Pro DC 2023 for Mac是一款功能强大的PDF编辑和管理软件&#xff0c;旨在帮助用户轻松处理PDF文件。它提供了丰富的工具和功能&#xff0c;使用户可以创建、编辑、转换和注释PDF文件&#xff0c;以及填写和签署PDF表单。 软件下载&#xff1a;Acrobat Pro DC 2023 for …

Matlab将日尺度数据转化为月尺度数据

日尺度转化为月尺度 clcclear all% load datadata xlread(data.xlsx) % 例如该数据为1961-01-01至2022-12-31&#xff0c;共计22645天data data(:,1:3) % 该数据有22645行&#xff0c;数据分别为降水&#xff0c;气温&#xff0c;湿度等三列dt datetime(1961-01-01):datatim…

一种调用所有LLM模型API的方式

公众号&#xff1a;【可乐前端】&#xff0c;每天3分钟学习一个优秀的开源项目&#xff0c;分享web面试与实战知识&#xff0c;也有全栈交流学习摸鱼群&#xff0c;期待您的关注! 每天3分钟开源 hi&#xff0c;这里是每天3分钟开源&#xff0c;很高兴又跟大家见面了&#xff0…

FDM3D打印系列——手涂水性漆和补土

大家好&#xff0c;我是阿赵。   继续探索FDM3D打印。这次要打印的是拳皇系列的经典角色&#xff0c;八神庵&#xff1a;   上次那个卢卡尔打印的尺寸有点小&#xff0c;为了配合着卢卡尔的高度&#xff0c;所以这次这个八神庵也打印的特别的小&#xff1a;   特别小的…

C语言 键盘输入与屏幕输出——数据的格式化屏幕输出

目录 顺序结构 C语言如何实现数据的输入和输出&#xff1f; 数据的格式化屏幕输出 printf&#xff08;&#xff09;格式字符 printf&#xff08;&#xff09;的格式修饰符 顺序结构 一般而言&#xff0c;顺序结构程序涉及如下三个基本操作&#xff1a; *输入数据 *处理数…

C++STLmap,set

我最近开了几个专栏&#xff0c;诚信互三&#xff01; > |||《算法专栏》&#xff1a;&#xff1a;刷题教程来自网站《代码随想录》。||| > |||《C专栏》&#xff1a;&#xff1a;记录我学习C的经历&#xff0c;看完你一定会有收获。||| > |||《Linux专栏》&#xff1…

什么是公网IP?

公网IP&#xff0c;即公开网络IP地址&#xff0c;是指在互联网中公开可见、可访问的IP地址。每个设备在连接互联网时&#xff0c;都需要一个唯一的公网IP地址&#xff0c;以便其他设备可以定位并与之通信。 尽管公网IP在网络通信中具有重要作用&#xff0c;但它也带来了一些安全…