项目要求分享带二维码的宣传图,二维码是账号生成的放在宣传图上,要求分享时生成一张图片。
首先是先将布局生成图片,这需要用到系统的
RepaintBoundary
将需要生成图片的布局用RepaintBoundary嵌套:
RepaintBoundary(
key: globalKey,
child: Container(
···
),
)
他需要一个globalkey:
var globalKey = new GlobalKey();
然后需要生成图片并保存到本地:
/// 保存图片
void _save(globalKey) async {
RenderRepaintBoundary boundary = globalKey.currentContext.findRenderObject();
ui.Image image = await boundary.toImage();
ByteData byteData = await image.toByteData(format: ui.ImageByteFormat.png);
Uint8List picBytes = byteData.buffer.asUint8List();
final result = await ImageGallerySaver.saveImage(
picBytes,
quality: 100,
name: "hello");
print(result);
if(result['isSuccess']){
EasyLoading.showToast('保存成功');
} else {
EasyLoading.showToast('保存失败');
}
}
这里用到保存图片到本地的一个插件:
image_gallery_saver: ^1.6.8
然后还需要加入权限操作,最后你就可以实现布局截图保存本地了,如果对你有帮助的话,请给个赞吧,作者拜谢!!!