為您解碼網(wǎng)站建設(shè)的點(diǎn)點(diǎn)滴滴
發(fā)表日期:2018-12 文章編輯:小燈 瀏覽次數(shù):3682
繼續(xù)更新Flutter系列,本篇記錄如何在Flutter中進(jìn)行截圖,在Flutter中萬物皆組件,不但高斯模糊是套一層組件,截圖也是套一層組件,所以屏幕截圖和組件截圖其實(shí)是一個(gè)意思。雖然Flutter的這種嵌套UI很繁瑣,但是用習(xí)慣了反而會(huì)感覺結(jié)構(gòu)很清晰,不用擔(dān)心布局相關(guān)代碼的混亂,在FlutterInspector識(shí)圖下更是一目了然,可以在不翻閱代碼的情況下快速理解別人寫的布局。
本次用到的組件是RepaintBoundary,效果圖:
依照慣例,創(chuàng)建一個(gè)簡單的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(), ); } }
這就是一個(gè)帶有標(biāo)題欄的空界面。
便于演示,在這個(gè)界面中加入一個(gè)gif圖片,當(dāng)然你用普通圖片或者視頻也是可以的:
class _MyHomePageState extends State<MyHomePage> { Future<Uint8List> _capturePng() async { //TODO 進(jìn)行截圖 }@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("全屏截圖"), ), ], ), ); } }
加入圖片之后我順便加入了一個(gè)FlatButton組件,通過這個(gè)點(diǎn)擊這個(gè)按鈕來觸發(fā)截圖的邏輯。
當(dāng)然到目前為止這還是只是一個(gè)簡單的界面布局,沒有用到任何新的東西。
前面說到本篇會(huì)用到RepaintBoundary
組件,接下來把它套在你想要截圖的組件的外層,想截全屏的話就套在最外面就可以,F(xiàn)lutter的這種寫法習(xí)慣就好。
同時(shí)定義一個(gè)Key用來操作這個(gè)組件
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的引用,進(jìn)來拿到內(nèi)部組件的截圖:
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;//這個(gè)對象就是圖片數(shù)據(jù) } catch (e) { print(e); } return null; } ... }
通過上面一系列的方法調(diào)用,就拿到了一個(gè)Unit8List類型的圖片數(shù)據(jù)。
而Unit8List類型的圖片數(shù)據(jù)的顯示也非常簡單,通過Image.memory方法從內(nèi)存中加載圖片,下面附上完整的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, ), ) ], ), ), ); } }
更多干貨移步我的個(gè)人博客 https://www.nightfarmer.top/