10

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

1
  • 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. Commented Mar 26, 2019 at 4:54

5 Answers 5

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,
        ),
Sign up to request clarification or add additional context in comments.

3 Comments

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
@MilesAdamson - Can you try it on different phone or Emulator & see the what keyboard comes up.
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
13

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,
),

1 Comment

Not worked for me on samsung phone chrome
1

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,
                       
                      ),

Comments

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,

Comments

0

If you use FocusNode then delete it. Now it's working fine.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.