繼續更新Flutter系列,本篇記錄如何在Flutter中進行截圖,在Flutter中萬物皆組件,不但高斯模糊是套一層組件,截圖也是套一層組件,所以屏幕截圖和組件截圖其實是一個意思。雖然Flutter的這種嵌套UI很繁瑣,但是用習慣了反而會感覺結構很清晰,不用擔心布局相關代碼的混亂,在FlutterInspector識圖下更是一目了然,可以在不翻閱代碼的情況下快速理解別人寫的布局。
本次用到的組件是RepaintBoundary,效果圖:

效果展示
創建Flutter工程
依照慣例,創建一個簡單的Flutter工程,清理main.dart中無用的代碼便于演示:
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: MyHomePage(title: 'Flutter Demo'),
);
}
}
class MyHomePage extends StatefulWidget {
MyHomePage({Key key, this.title}) : super(key: key);
final String title;
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: Column(),
);
}
}
這就是一個帶有標題欄的空界面。
寫一個簡單的場景
便于演示,在這個界面中加入一個gif圖片,當然你用普通圖片或者視頻也是可以的:
class _MyHomePageState extends State<MyHomePage> {
Future<Uint8List> _capturePng() async {
//TODO 進行截圖
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: Column(
children: <Widget>[
Image.network(
"http://qiniu.nightfarmer.top/test.gif",
width: 300,
height: 300,
),
FlatButton(
onPressed: () {
this._capturePng();
},
child: Text("全屏截圖"),
),
],
),
);
}
}
加入圖片之后我順便加入了一個FlatButton組件,通過這個點擊這個按鈕來觸發截圖的邏輯。
當然到目前為止這還是只是一個簡單的界面布局,沒有用到任何新的東西。
如何截圖
前面說到本篇會用到RepaintBoundary
組件,接下來把它套在你想要截圖的組件的外層,想截全屏的話就套在最外面就可以,Flutter的這種寫法習慣就好。
同時定義一個Key用來操作這個組件
class _MyHomePageState extends State<MyHomePage> {
GlobalKey rootWidgetKey = GlobalKey();
...
@override
Widget build(BuildContext context) {
return RepaintBoundary(
key: rootWidgetKey,
child: Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: Column(
.....
),
),
);
}
}
通過rootWidgetKey可以拿到RenderRepaintBoundary的引用,進來拿到內部組件的截圖:
class _MyHomePageState extends State<MyHomePage> {
GlobalKey rootWidgetKey = GlobalKey();
Future<Uint8List> _capturePng() async {
try {
RenderRepaintBoundary boundary =
rootWidgetKey.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;
}
...
}
通過上面一系列的方法調用,就拿到了一個Unit8List類型的圖片數據。
顯示截圖
而Unit8List類型的圖片數據的顯示也非常簡單,通過Image.memory方法從內存中加載圖片,下面附上完整的State代碼:
class _MyHomePageState extends State<MyHomePage> {
GlobalKey rootWidgetKey = GlobalKey();
List<Uint8List> images = List();
_capturePng() async {
try {
RenderRepaintBoundary boundary =
rootWidgetKey.currentContext.findRenderObject();
var image = await boundary.toImage(pixelRatio: 3.0);
ByteData byteData = await image.toByteData(format: ImageByteFormat.png);
Uint8List pngBytes = byteData.buffer.asUint8List();
images.add(pngBytes);
setState(() {});
return pngBytes;
} catch (e) {
print(e);
}
return null;
}
@override
Widget build(BuildContext context) {
return RepaintBoundary(
key: rootWidgetKey,
child: Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: Column(
children: <Widget>[
Image.network(
"http://qiniu.nightfarmer.top/test.gif",
width: 300,
height: 300,
),
FlatButton(
onPressed: () {
this._capturePng();
},
child: Text("全屏截圖"),
),
Expanded(
child: ListView.builder(
itemBuilder: (context, index) {
return Image.memory(
images[index],
fit: BoxFit.cover,
);
},
itemCount: images.length,
scrollDirection: Axis.horizontal,
),
)
],
),
),
);
}
}
完工。
更多干貨移步我的個人博客 https://www.nightfarmer.top/