14

I need a numberInputKeyboard which only has numbers (without decimal symbol). I have tried with keyboardType: TextInputType.numberWithOptions(decimal: false) but still this dosen't help me

4 Answers 4

37

One answer recommends using a BlacklistingTextInputFormatter which certainly works in your case with English locale. This however might not work with other locales which use a , instead of a . as decimal seperator.

So I'd recommend using a WhitelistingTextInputFormatter:

import "package:flutter/services.dart";
TextFormField(
  keyboardType: TextInputType.number,
  inputFormatters: [WhitelistingTextInputFormatter.digitsOnly],
)

This will only allows the digits [0-9].

UPDATE:

The WhitelistingTextInputFormatter as been renamed! The following is the up-to date version and no longer deprecated by FilteringTextInputFormatter.allow and provides a shortcut for the digits as well:

import "package:flutter/services.dart";
TextFormField(
  keyboardType: TextInputType.number,
  inputFormatters: [FilteringTextInputFormatter.digitsOnly],
)
2
  • 1
    Most Simpler.. Makes my work much easier..! Thank you Bro
    – Santhosh
    Commented Aug 21, 2019 at 9:39
  • This is cleaner as opposed to doing custom validations.
    – wamae
    Commented May 19, 2021 at 11:52
4

BlacklistingTextInputFormatter and WhitelistingTextInputFormatter are deprecated. If you want to admit digits only, you could use:

FilteringTextInputFormatter.digitsOnly

If you want to admit numbers with decimal point:

FilteringTextInputFormatter.allow(RegExp(r'^(\d+)?\.?\d{0,2}')
2

To aport this thread i did this, helped with the link provided by Amit Prajapati, its a but more manual but in my case worked perfectly

inputFormatters: [
          TextInputFormatter.withFunction((oldValue, newValue) {
            if (textInputType.decimal == true) {
              return newValue.copyWith(
                text: newValue.text.replaceAll(RegExp(r"\,"), "."),
              );
            }
            return newValue;
          }),
        ],

EDIT: textInputType is a variable for a custom TextField

2

First Approach :-

As per this stackoverflow answer you must have to create your custom keyboard for it.

Android - is it possible to hide certain characters from the soft keyboard?

Second Approach :-

You can do black list that stuff with RegExp, in this case you can't input dot(.).

For more you can refer this : https://api.flutter.dev/flutter/services/TextInputFormatter-class.html

        body: Center(
            child: TextFormField(
            decoration: InputDecoration(
                labelText: "Title",
                suffixIcon: GestureDetector(
                onTap: () {
                    setState(() {

                    });
                },
                child: Icon(Icons.clear),
                )),
            inputFormatters: [
               FilteringTextInputFormatter.deny(RegExp("[.]")),
            ],
        )));
1
  • 2
    Great..! It works like charm. But you have to import package:flutter/services.dart before using it. Thank you..!!
    – Santhosh
    Commented Aug 21, 2019 at 9:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.