8

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

Miles Adamson
  • 357
  • 1
  • 3
  • 9
  • 1
    `keyboardType: TextInputType.number` is showing number keyboard for me. Try to uninstall and reinstall the app again. Also try to use some other keyboard then SwiftKey. – divyanshu bhargava Mar 26 '19 at 04:54

4 Answers4

14

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,
        ),
anmol.majhail
  • 48,256
  • 14
  • 136
  • 105
  • 1
    This does make it so that the field doesn't accept anything but a number, but the keyboard still opens fully where I can press any character. Its an improvement for sure but I would really like it to just open like [this number only keyboard](https://stackoverflow.com/a/49578197/8061908) – Miles Adamson Mar 26 '19 at 03:59
  • @MilesAdamson - Can you try it on different phone or Emulator & see the what keyboard comes up. – anmol.majhail Mar 26 '19 at 04:03
  • 1
    It appears it is an issue with my phone, P20 Pro. It works as expected on other phones, where they keyboard is only numbers. But your addition of WhitelistingTextInputFormatter is still great, thanks – Miles Adamson Mar 26 '19 at 18:16
10

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,
),
Kenneth Murerwa
  • 778
  • 12
  • 16
0

Use this as older one is Depressed

maxLength: 10,
inputFormatters: [
   // only accept letters from 0 to 9
   FilteringTextInputFormatter(RegExp(r'[0-9]'), allow: true)
   // Using for Text Only ==>    (RegExp(r'[a-zA-Z]'))
],
keyboardType: TextInputType.number,
Sonu kumar
  • 351
  • 3
  • 7
0

TextFormField(

                       keyboardType: TextInputType.number,
                       inputFormatters:  <TextInputFormatter> [
                        FilteringTextInputFormatter.allow(RegExp(r'[0-9]')),
                        FilteringTextInputFormatter.digitsOnly,                          
                        ],
                       decoration: const InputDecoration(contentPadding: EdgeInsets.all(5),
                       labelText: "TextInputType.number:", prefixIcon: Icon(Icons.wallet),
                       ),
                       maxLength: 10,
                       
                      ),