mr_sh
mr_sh

Reputation: 369

How to disable text replacement in TextFormField in Flutter?

I have some text replacements registered from General -> Keyboard -> Text Replacement. I want to prevent auto text replacement in some of my fields inside my app.

autocorrect: false & enableSuggestions: false does not work.

Form(
                  child: TextFormField(

                      validator: (value) {
                        if (widget.arguments.isNumber) {
                          return Validators.weightValidator(value, context);
                        }

                        return Validators.emptyValidator(value, context);
                      },                       
                      keyboardType: widget.arguments.isNumber
                          ? const TextInputType.numberWithOptions(
                              decimal: true)
                          : TextInputType.text,
                      obscureText: false,
                      autocorrect: false,
                      enableSuggestions: false,
                      controller: _controller,
                      decoration: InputDecoration(
                          hintText: "kg",
                          fillColor: Colors.white,
                          filled: true,
                          focusedBorder: OutlineInputBorder(
                            borderSide:
                                const BorderSide(color: Colors.white),
                            borderRadius: BorderRadius.circular(20),
                          ),
                          enabledBorder: UnderlineInputBorder(
                            borderSide:
                                const BorderSide(color: Colors.white),
                            borderRadius: BorderRadius.circular(20),
                          ),
                          errorBorder: UnderlineInputBorder(
                            borderSide:
                                const BorderSide(color: Colors.white),
                            borderRadius: BorderRadius.circular(20),
                          ),
                          focusedErrorBorder: UnderlineInputBorder(
                            borderSide:
                                const BorderSide(color: Colors.white),
                            borderRadius: BorderRadius.circular(20),
                          ))),
                  key: _formKey,
                ),

enter image description here

Upvotes: 1

Views: 205

Answers (1)

Abdul Muneeb
Abdul Muneeb

Reputation: 101

To prevent users from copying or pasting text in a TextFormField, you can create a custom class that extends MaterialTextSelectionControls. Here's how you can achieve this:

class NoCopyTextSelectionControls extends MaterialTextSelectionControls 
{
  @override
  bool canCopy(TextSelectionDelegate delegate) => false;

  @override
  bool canCut(TextSelectionDelegate delegate) => false;

  @override
  bool canPaste(TextSelectionDelegate delegate) => false;

  @override
  bool canSelectAll(TextSelectionDelegate delegate) => false;
}

Now, to use this custom class in your TextFormField to disable text copying and pasting, do the following:

TextFormField(
  // Pass the custom class instance to selectionControls
  selectionControls: NoCopyTextSelectionControls(),
  // Other properties for your TextFormField
)

By passing the NoCopyTextSelectionControls instance to selectionControls, you are essentially replacing the default text selection controls with your custom controls, which disable copying, cutting, pasting, and selecting all text in the TextFormField.

Upvotes: 0

Related Questions