Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flutter TextInputType.number is not forcing a number only keyboard

Tags:

flutter

I don't understand what I'm missing here. I put TextInputType to number, and can still type any letter, special character and even an emoji. I have also tried making the child a TextInputType.numberWithOptions() widget, and it opens the same keyboard. Is this just a bug on my phone? P20 Pro

      Row(
          children: <Widget>[
            Text('TextInputType.number:'),
            Flexible(
                child: TextField(
              maxLength: 3,
              keyboardType: TextInputType.number,
            )),
          ],
        )

Should be numbers only

like image 258
Miles Adamson Avatar asked Mar 26 '19 03:03

Miles Adamson


2 Answers

To Only Enable Numbers - You need to add - inputFormatters: also adding - keyboardType: won't help standalone.

Code:

TextField(
          maxLength: 3,
          inputFormatters: <TextInputFormatter>[
            WhitelistingTextInputFormatter.digitsOnly,
          ],
          keyboardType: TextInputType.number,
        ),
like image 178
anmol.majhail Avatar answered Nov 15 '22 10:11

anmol.majhail


The approach above in anmol.majhail's answer is deprecated as of Flutter 2.0. Flutter recommends a shift from WhitelistingTextInputFormatter.* to FilteringTextInputFormatter.*. As such, in order to achieve the same effect in a Flutter 2.0 app follow the example in the code excerpt below.

TextField(
  maxLength: 3,
  inputFormatters: <TextInputFormatter>[
    FilteringTextInputFormatter.digitsOnly,
  ],
  keyboardType: TextInputType.number,
),
like image 21
Kenneth Murerwa Avatar answered Nov 15 '22 11:11

Kenneth Murerwa