How do I monitor the clipboard in Flutter?

后端 未结 1 1420
时光取名叫无心
时光取名叫无心 2021-01-19 11:56

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,

does anybody know ho

1条回答
  •  再見小時候
    2021-01-19 12:57

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

    0 讨论(0)
提交回复
热议问题