首页 > 解决方案 > 如何在 Flutter 中截屏?

问题描述

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

有一些包,我试过了,没有找到有用的包。

标签: flutterdart

解决方案


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


推荐阅读