如何在Flutter中拍摄屏幕截图?

时间:2020-02-19 17:02:19

标签: flutter dart

是否有一种方法或程序包可以帮助拍摄全屏屏幕截图,或包装的小部件的屏幕截图,或者至少可以通过本机共享选项共享屏幕图片?

有些软件包我已经尝试过,没有找到有用的软件包。

1 个答案:

答案 0 :(得分:2)

RepaintBoundary 是您要查找的Widget,可以将其转换为图像。

示例:

Future<CaptureResult> captureImage() async {
    final pixelRatio = MediaQuery.of(context).devicePixelRatio;
    final boundary = _boundaryKey.currentContext.findRenderObject() as RenderRepaintBoundary;
    final image = await boundary.toImage(pixelRatio: pixelRatio);
    final data = await image.toByteData(format: ui.ImageByteFormat.png);
    return CaptureResult(data.buffer.asUint8List(), image.width, image.height);
}

final _boundaryKey = GlobalKey();

RepaintBoundary(
   key: _boundaryKey,
   child: Container(),// Your Widgets to be captured.
)

链接:capture_widget.dart