如何在Flutter中监视剪贴板? [英] How do I monitor the clipboard in Flutter?

查看:555
本文介绍了如何在Flutter中监视剪贴板?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一种在Flutter中监视剪贴板的方法,我发现与Flutter上的剪贴板交互有关的所有内容是:

I'm looking for a way to monitor the Clipboard in Flutter, all I could find relating to clipboard interaction on Flutter was: Clipboard class,

有人知道我该如何监视Flutter中的新项目,最好使用插件?

does anybody know how can I monitor the system clipboard for new items in Flutter, preferably using a plugin?

推荐答案

可能要晚一些,但仍然可以. 不需要插件或库,该解决方案可能非常简单. 这里是一个如何监视ClipBoard内容的基本示例:

It might be a little late but still. There is no need for a plugin or library, the solution could be very trivial. Here a basic example of how you can monitor ClipBoard content:

#creating a listening Stream:
final clipboardContentStream = StreamController<String>.broadcast();

#creating a timer for updates:
Timer clipboardTriggerTime;

clipboardTriggerTime = Timer.periodic(
# you can specify any duration you want, roughly every 20 read from the system
      const Duration(seconds: 5),
      (timer) {
        Clipboard.getData('text/plain').then((clipboarContent) {
          print('Clipboard content ${clipboarContent.text}');

          # post to a Stream you're subscribed to
          clipboardContentStream.add(clipboarContent.text);
        });
      },
    );

# subscribe your view with
Stream get clipboardText => clipboardController.stream

# and don't forget to clean up on your widget
@override
void dispose() {
  clipboardContentStream.close();

  clipboardTriggerTime.cancel();
}

这篇关于如何在Flutter中监视剪贴板?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆