Flutter-如何获取屏幕小部件的图片?

Flutter- how to get pictures of screen widgets?

我正在使用不同的面部图像在 flutter 中制作面部,我想在面部创建完成后将其导出为 jpg。我可以用什么来实现它?

你可以看到这里创建了一张脸,我只想将脸导出为 jpeg。

在此 article 中,将 GlobalKey 与您的小部件一起使用,并通过以下代码保存图像:

takeScreenShot() async {
  RenderRepaintBoundary boundary =
      previewContainer.currentContext.findRenderObject();
  double pixelRatio = originalSize / MediaQuery.of(context).size.width;
  ui.Image image = await boundary.toImage(pixelRatio: pixelRatio);
  ByteData byteData = await image.toByteData(format: ui.ImageByteFormat.png);
  Uint8List pngBytes = byteData.buffer.asUint8List();
  setState(() {
    _image2 = Image.memory(pngBytes.buffer.asUint8List());
  });
  final directory = (await getApplicationDocumentsDirectory()).path;
  File imgFile = new File('$directory/screenshot.png');
  imgFile.writeAsBytes(pngBytes);
  final snackBar = SnackBar(
    content: Text('Saved to ${directory}'),
    action: SnackBarAction(
      label: 'Ok',
      onPressed: () {
        // Some code
      },
    ),
  );

  Scaffold.of(context).showSnackBar(snackBar);
}