【问题标题】:How do I monitor the clipboard in Flutter?如何在 Flutter 中监控剪贴板?
【发布时间】:2018-11-11 20:23:36
【问题描述】:

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

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

【问题讨论】:

    标签: flutter


    【解决方案1】:

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

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

    【讨论】:

      猜你喜欢
      • 2021-11-18
      • 2010-10-11
      • 1970-01-01
      • 2013-07-01
      • 1970-01-01
      • 1970-01-01
      • 2023-02-21
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多