我正在寻找一种添加拦截器的方法,在粘贴剪贴板的内容之前,我想添加一个可以记录内容信息的拦截器。
我用谷歌搜索了很多结果,但没有找到。
我的期待: 例如,在剪贴板粘贴之前添加回调
beforePaste(dynamic data) {
...
}
然后每次粘贴剪贴板内容时,都会调用
beforePaste
据我从您所写的内容中了解到,您需要的方法不在剪贴板库内。您应该做的是使用您将使用的文本字段处理粘贴任务。您可以通过使用可以在
onChanged
方法之前运行的方法来实现您想要的场景。一种方法如下;
class CustomFormater extends TextInputFormatter {
@override
TextEditingValue formatEditUpdate(
TextEditingValue oldValue, TextEditingValue newValue) {
print("triggered before textfield changed");
return newValue;
}
}
您将在您将要处理的文本字段上使用它。如图所示;
TextField(
onChanged: (y) {
print("text field changed");
},
inputFormatters: [
CustomFormater(), // here you can use this as interceptor
],
),
一旦您输入或粘贴任何内容,您将获得自定义格式化程序的打印值。然后
onChanged
事件就会被触发。