如何在Flutter中监控剪贴板?

时间:2018-06-01 16:35:55

标签: flutter

我正在寻找一种方法来监控Flutter中的剪贴板,我在Flutter上找到与剪贴板交互有关的所有内容:Clipboard class

是否有人知道如何在Flutter中监控系统剪贴板中的新项目,最好是使用插件?

1 个答案:

答案 0 :(得分:0)

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

#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();
}