Disable copy/paste/cut, and select all options on TextFormField

In this post, we are going to learn about the way of disabling copy, cut, paste, and select all options on TextFormField. In TextFormField, we have a parameter called enableInteractiveSelection that controls the disable and enables these options. It will take the boolean value. for example, Setting this to true enables selecting text and displaying the cut/copy/paste option by long-pressing the TextFormField, as well as moving the text caret by tapping. When this property is false, the user can’t change the text selection, copy text, or paste from the clipboard into the text field. The default value of enableInteractiveSelection is True.

Enable Copy, Paste, Cut, and SelectAll options on TextFormField:

As previously stated, all settings on the TextFormField are enabled by default.

TextFormField(
              // by default the value is true
              enableInteractiveSelection: true,
              decoration: InputDecoration(labelText: "Sample field"),
              keyboardType: TextInputType.text,
            )
disable copy
Options are enabled

All enable options for the TextFormField widget are shown in the output image above.

Disable Copy, Paste, Cut, and SelectAll option on TextFormField:

We can disable these options by setting the enableInteractiveSelection parameter in TextformField to false.

TextFormField(
              // by default the value is true
              enableInteractiveSelection: false,
              decoration: InputDecoration(labelText: "Sample field"),
              keyboardType: TextInputType.text,
            )
disable copy
Options are disabled

ToolbarOptions property:

On TextformField, we can use the toolbarOptions property to enable and disable the selected option. If we don’t set any value on toolbarOptions, selectAll and paste will be enabled by default. If obscureText is true, copy and cut will be disabled. If readOnly is true, paste and cut will be disabled.

Set true to the copy parameter of the ToolbarOptions if we just want the copy option only to appear.

TextFormField(
              // by default the value is true
              enableInteractiveSelection: true,
              decoration: InputDecoration(labelText: "Sample field"),
              keyboardType: TextInputType.text,
              toolbarOptions: ToolbarOptions(
                copy: true
              ),
            )
disable copy
Enabled “Copy” only

Set true the relevant settings to ToolbarOptions if we want to allow cut and select all options.

 TextFormField(
              // by default the value is true
              enableInteractiveSelection: true,
              decoration: InputDecoration(labelText: "Sample field"),
              keyboardType: TextInputType.text,
              toolbarOptions: ToolbarOptions(
                copy: false,
                cut: true,
                selectAll: true
              ),
            )
disable copy
Enabled “cut” and select all”

Like that we can enable or disable selected options on the TextFormField widget by specifying the ToolbarOptions property.

For more information click here

Thanks for reading … 🙂

1 thought on “Disable copy/paste/cut, and select all options on TextFormField”

Leave a Comment