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,
)),
],
)
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,
),
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,
),
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With