skip to Main Content

Is any update for this question Flutter TextField value always uppercase & debounce ?

I’ve already tried some of the code to make the input text become uppercase.

class UpperCaseTextFormatter extends TextInputFormatter {
  @override
  TextEditingValue formatEditUpdate(TextEditingValue oldValue, TextEditingValue newValue) {
    return TextEditingValue(
      text: newValue.text.toUpperCase(),
      selection: newValue.selection,
    );
  }
}
class UpperCaseTextFormatter extends TextInputFormatter {
  @override
  TextEditingValue formatEditUpdate(TextEditingValue oldValue, TextEditingValue newValue) {
    return newValue.copyWith(text: newValue.text.toUpperCase());
  }
}

But it works not correct when I type "QWER", it will always become "QQWQWEQWER"

Here is the video : https://drive.google.com/file/d/1q3dwuVMPzt_kBP4c2lyiahQ6znkSbM-n/view?usp=sharing

I prefer not to use textCapitalization because the user still can change it to lowercase, if the formatter dosen’t work.

Use TextInputFormatter to make textfield input become uppercase

2

Answers


  1. You can use TextEditingController to make user input always UPPERCASE, With the below code the user can’t change it to lowercase.

    TextEditingController _controller = TextEditingController();
    
    TextFormField(
      controller: _controller,
      onChanged: (String text) {
        _controller.text = text.toUpperCase();
      },
    );
    

    This will work Happy Coding!

    Login or Signup to reply.
    1. Assign a controller to TextField.

    2. Convert the text value in controller toUpperCase() in onChanged function.

      TextEditingController _controller = TextEditingController();
      
      TextFormField(
        controller: _controller,
        onChanged: (String text) {
          _controller.text = text.toUpperCase();
        },
      );
      
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search