引入库: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: (){}
);
}
}
)