Search code examples
flutterflutter-web

Prevent copy paste feature when right click the mouse in textfields flutter web


I prevent copy paste in textformfield(Flutter web) using Ctrl+C and Ctrl+V by adding

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

But still, it is possible to copy-paste by the following method, enter image description here

Is it possible to prevent ?


Solution

  • work around is, if user right click on textfield, unfocus, or even disable it:

    Widget build(BuildContext context) {
            return SizedBox(
              width: 300,
              child: Listener(
                onPointerDown: (event){
                  if(event.kind == PointerDeviceKind.mouse && event.buttons == kSecondaryMouseButton){
                    print('yoo the user try to right click unfocus this so he cant paste');
                    _focusNode.unfocus();
                  }
                },
                child: TextField(
                  enableInteractiveSelection: false,
                  toolbarOptions: ToolbarOptions(
                    paste: false
                  ),
                  onChanged: (data){
                    print(data);
                  },
                  focusNode: _focusNode,
                  controller: _textEditingController,
                ),
              ),
            );
          }