截取屏幕用到的组件是本次用到的组件是RepaintBoundary,注意要设置一个key来获取截屏
Widget build(BuildContext context) {
return RepaintBoundary(
key: _repaintKey,
child: Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: Column(
.....
),
),
);
}
通过rootWidgetKey可以拿到RenderRepaintBoundary的引用,进来拿到内部组件的截图:
class ScreenshotWatermarkState extends State<ScreenshotWatermark> {
GlobalKey _repaintKey = GlobalKey();
Future<Uint8List> _capturePng() async {
try {
RenderRepaintBoundary boundary =
_repaintKey.currentContext.findRenderObject();
var image = await boundary.toImage(pixelRatio: 3.0);
ByteData byteData = await image.toByteData(format: ImageByteFormat.png);
Uint8List pngBytes = byteData.buffer.asUint8List();
return pngBytes;//这个对象就是图片数据
} catch (e) {
print(e);
}
return null;
}
...
}
截取屏幕之后将截取的图片保存到本地,直接上代码吧
import 'dart:typed_data';
import 'dart:ui';
import 'package:flutter/material.dart';
import 'package:flutter/rendering.dart';
import 'package:flutter_screenshotswatermark/commons/constants.dart';
import 'package:flutter_screenshotswatermark/commons/utils.dart';
import 'package:image_gallery_saver/image_gallery_saver.dart';
class ScreenshotWatermark extends StatefulWidget {
@override
ScreenshotWatermarkState createState() => new ScreenshotWatermarkState();
}
class ScreenshotWatermarkState extends State<ScreenshotWatermark> {
///截取屏幕 并不是截取页面所有的内容
final GlobalKey _repaintKey = new GlobalKey();
String _content = "到了农历的年末,城市的超市里挂满了玲珑华美的红灯笼,玻璃橱窗上也贴上了各式花样的剪纸,这些都是年的符号,也是年的名片。我内心深藏的年味儿犹如一只脆弱不堪的老酒坛被这些符号与名片猛然击碎,老酒倾泻满地,浓郁醇厚的味道漫然飘散。我小的时候,盼望着过年。从农历的腊月二十三开始,接下来的每一天似乎都是色彩斑斓的,都散发着温馨绵厚的香味儿。千百年来,太阳沿着亘古不变的轨迹东升西落。";
@override
Widget build(BuildContext context) {
return RepaintBoundary(
key: _repaintKey,
child: Scaffold(
appBar: AppBar(
title: Text("截屏",
style: new TextStyle(
color: Color(AppColors.c_333333),
fontSize: AutoSize.f_36,
)),
brightness: Brightness.light,
elevation: 0,
backgroundColor: Color(AppColors.c_FFFFFF),
leading: Utils.backButton(context),
),
body: Container(
child: ListView(
children: <Widget>[
Container(
color: Colors.green,
padding: EdgeInsets.all(AutoSize.w_20),
child: FlatButton(
onPressed: () {
_capturePng();
},
child: Text(
'全屏截图',
style: TextStyle(
color: Colors.white
),
),
),
),
_contentWidget('故乡的年味', _content),
_contentWidget('故乡的年味', _content),
_contentWidget('故乡的年味', _content),
],
)
)
)
);
}
_capturePng() async {
try {
RenderRepaintBoundary boundary =
_repaintKey.currentContext.findRenderObject();
var image = await boundary.toImage(pixelRatio: 3.0);
ByteData byteData = await image.toByteData(format: ImageByteFormat.png);
Uint8List pngBytes = byteData.buffer.asUint8List();
///注意:截取之后图片在内存中 如果需要展示截取的图片需要使用 Image.memory(pngBytes)
///
_saveImage(pngBytes);
return pngBytes;
} catch (e) {
print(e);
}
return null;
}
void _saveImage(Uint8List pngBytes) async {
final result = await ImageGallerySaver.saveImage(pngBytes);
print('result:$result');
if(Utils.isEmpty(result)){
print('保存失败');
}else{
print('保存成功');
}
}
Widget _contentWidget(String text,String content){
return Container(
margin: EdgeInsets.all(AutoSize.w_20),
child: Column(
children: <Widget>[
Text(
text,
style: TextStyle(
fontSize: AutoSize.f_30
)),
Text(
content,
style: TextStyle(
fontSize: AutoSize.f_30
)),
],
),
);
}
@override
void initState() {
// TODO: implement initState
super.initState();
}
@override
void dispose() {
// TODO: implement dispose
super.dispose();
}
@override
void didUpdateWidget(ScreenshotWatermark oldWidget) {
// TODO: implement didUpdateWidget
super.didUpdateWidget(oldWidget);
}
@override
void didChangeDependencies() {
// TODO: implement didChangeDependencies
super.didChangeDependencies();
}
}