https://www.jianshu.com/p/6801ecbee03d
/** * const RepaintBoundary({ Key key, Widget child }) */
//import 'dart:io'; //import 'dart:typed_data'; //import 'package:flutter/material.dart'; //import 'package:flutter/rendering.dart'; //import 'dart:ui'; //import 'package:path_provider/path_provider.dart'; class Widget_RepaintBoundary_State extends State<Widget_RepaintBoundary_Page> { GlobalKey globalKey = new GlobalKey(); Future<File> _capture() async { try { RenderRepaintBoundary boundary = globalKey.currentContext .findRenderObject(); //boundary.toImage()轉化為ui.Image對象,不會自動為包裹的組件添加背景,不設置可能會缺失背景 var image = await boundary.toImage(pixelRatio: window.devicePixelRatio); //將image轉化為byteData ByteData byteData = await image.toByteData(format: ImageByteFormat.png); //這個對象就是圖片數據 Uint8List pngBytes = byteData.buffer.asUint8List(); String sTempDir = (await getTemporaryDirectory()).path; bool isDirExist = await Directory(sTempDir).exists(); if (!isDirExist) { Directory(sTempDir).create(); } Future<File> file = File(sTempDir + "/abc.png").writeAsBytes(pngBytes); return file; } catch (e) { print(e); } return null; }
作者:習慣了_就好
鏈接:https://www.jianshu.com/p/6801ecbee03d
來源:簡書
著作權歸作者所有。商業轉載請聯系作者獲得授權,非商業轉載請注明出處。
如何截圖
前面說到本篇會用到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, ), ) ], ), ), ); } }
作者:NightFarmer
鏈接:https://www.jianshu.com/p/da3f23d0843b
來源:簡書
著作權歸作者所有。商業轉載請聯系作者獲得授權,非商業轉載請注明出處。