system_screenshot

从系统界面获取原生截图。

目前仅支持 MacOS。

入门

步骤 1

在任何您喜欢的地方创建一个新的 SystemScreenshot 对象。一个好的放置位置是在您的 Widget 的 State 类顶部。

class _MyAppState extends State<MyApp> {
  
  final SystemScreenshot _systemScreenshotPlugin = SystemScreenshot();

步骤 2

调用 takeScreenshot 方法。

await _systemScreenshotPlugin.takeScreenshot()

请注意,它是一个 Future,并且执行是异步的。务必正确处理 Future 的结果,因为它也可能是可空的。

函数调用的结果数据是一个 Uint8List,您可以随意操作它。

  // Platform messages are asynchronous, so we initialize in an async method.
  Future<void> takeScreenshot() async {
    Uint8List? screenshot;

    // Platform messages may fail, so we use a try/catch PlatformException.
    // We also handle the message potentially returning null.
    try {
      isError = false;
      screenshot = await _systemScreenshotPlugin.takeScreenshot();
    } on PlatformException {
      isError = true;
    }

    // If the widget was removed from the tree while the asynchronous platform
    // message was in flight, we want to discard the reply rather than calling
    // setState to update our non-existent appearance.
    if (!mounted) return;

    setState(() {
      _screenshot = screenshot;
    });
  }

您可以使用 Image.memory 组件在您的应用中渲染图像

Image.memory(_screenshot)

您还可以以任何方式操作数据。如果您需要,可以使用不同的包来存储数据。

GitHub

查看 Github