How to take a screenshot of the current widget - Flutter

前端 未结 4 698
感动是毒
感动是毒 2020-12-01 06:02

I need to take a screenshot of the current screen or widget and I need to write it into a file.

4条回答
  •  隐瞒了意图╮
    2020-12-01 06:23

    let's say you want to take a screenshot of the FlutterLogo widget . wrap it in a RepaintBoundary with will creates a separate display list for its child . and provide with a key

    var scr= new GlobalKey();
    RepaintBoundary(
             key: scr,
             child: new FlutterLogo(size: 50.0,))
    

    and then you can get the pngBytes by converting the boundary to an image

    takescrshot() async {
      RenderRepaintBoundary boundary = scr.currentContext.findRenderObject();
      var image = await boundary.toImage();
      var byteData = await image.toByteData(format: ImageByteFormat.png);
      var pngBytes = byteData.buffer.asUint8List();
      print(pngBytes);
      }
    

提交回复
热议问题