【问题标题】:How to take screenshot in Flutter?如何在 Flutter 中截屏?
【发布时间】:2020-02-19 17:02:19
【问题描述】:

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

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

【问题讨论】:

  • 请发布您使用的包,以避免任何多余的建议

标签: flutter dart


【解决方案1】:

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

【讨论】:

  • 这里如何处理异常?是否可以在 try catch 块中使用此代码?
  • @Mahesh 你绝对可以使用 try-catch 块,在我看来你可能不需要它,因为应用程序只是将这些设备像素转换为图像。
猜你喜欢
  • 2020-10-18
  • 2021-05-13
  • 2020-10-24
  • 2023-02-05
  • 1970-01-01
  • 2018-12-09
  • 1970-01-01
  • 2023-04-04
  • 1970-01-01
相关资源
最近更新 更多