gpt4 book ai didi

Flutter Web - TextFormField 禁用复制和粘贴

转载 作者:行者123 更新时间:2023-12-04 14:53:12 34 4
gpt4 key购买 nike

我想在我的文本字段上禁用复制和粘贴功能,但遗憾的是它在 web 上没有按预期工作。我尝试了以下 enableInteractiveSelectiontoolbarOptions 但我仍然可以复制并粘贴到网络中的文本字段。这是什么解决方案。谢谢

  TextFormField(
enableInteractiveSelection: false,
toolbarOptions: ToolbarOptions(
copy: false,
paste: false,
cut: false,
selectAll: false,
),
)

最佳答案

如果你正在考虑快捷键,我们需要监听LogicalKeySet

结果

enter image description here

我是这样做的:

用于复制粘贴键

///* for mac replace  LogicalKeyboardKey.control, with LogicalKeyboardKey.meta
final selectableKeySetwindows = LogicalKeySet(
LogicalKeyboardKey.control,
LogicalKeyboardKey.keyA,
);
final pasteKeySetwindows = LogicalKeySet(
LogicalKeyboardKey.control,
LogicalKeyboardKey.keyV,
);

/// i dont have any ios device 😅,let me know what it produce.
final selectableKeySetMac = LogicalKeySet(
LogicalKeyboardKey.meta,
LogicalKeyboardKey.keyA,
);
class SelectionIntent extends Intent {}

class PasteIntent extends Intent {}

将处理事件的小部件


class DisableShortcut extends StatelessWidget {
final Widget child;

const DisableShortcut({
Key? key,
required this.child,
}) : super(key: key);

@override
Widget build(BuildContext context) {
return FocusableActionDetector(
shortcuts: {
selectableKeySetwindows: SelectionIntent(),
pasteKeySetwindows: PasteIntent(),
},
actions: {
SelectionIntent: CallbackAction(
onInvoke: (intent) {
ScaffoldMessenger.of(context)
.showSnackBar(SnackBar(content: Text("Copy is forbidden")));
return FocusScope.of(context).requestFocus(FocusNode());
},
),
PasteIntent: CallbackAction(
onInvoke: (intent) async {
// ClipboardData? data = await Clipboard.getData('text/plain');
// print(" paste callBack ${data!.text}");
return ScaffoldMessenger.of(context)
.showSnackBar(SnackBar(content: Text("Paste is forbidden")));
},
)
},
autofocus: true,
child: child,
);
}
}

我的测试小工具

 @override
Widget build(BuildContext context) {
return Scaffold(
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
TextField(),
TextField(),
DisableShortcut(
child: TextField(
enableInteractiveSelection: false,
toolbarOptions: ToolbarOptions(
copy: false,
cut: false,
paste: false,
selectAll: false,
),
),
),
],
),
),
);

关于Flutter Web - TextFormField 禁用复制和粘贴,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68688646/

34 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com