skip to Main Content

Hi, In flutter, How to create a text field that allows only numbers?

I’m unable to find a way to create an input field in Flutter that would open up a numeric keyboard and should take numeric input only. I tried. But I am getting errors.

Container(
              width: 310,
              TextEditingController _numberController = TextEditingController();

              child: TextField(
                controller: _numberController,
                keyboardType: TextInputType.number,
                inputFormatters: <TextInputFormatter>[
                  FilteringTextInputFormatter.digitsOnly,
                  
                  ],
                decoration: InputDecoration(
                    enabledBorder: UnderlineInputBorder(
                      borderSide: BorderSide(color: Color(0xffbcbcbc)),
                    ),
                    focusedBorder: UnderlineInputBorder(
                      borderSide: BorderSide(color: Colors.cyan),
                    ),
                    hintText: '+919876567876',
                    hintStyle: GoogleFonts.lato(
                        color: Color(0xff000000),
                        fontSize: 20,
                        letterSpacing: 0.5,
                        decorationThickness: 3)),
              ),
            ),

2

Answers


  1. Add any of these line inside your TextField widget

    
              keyboardType: TextInputType.number,
    
                 or 
    
             keyboardType: TextInputType.phone,
    
    Login or Signup to reply.
  2. You should declare TextEditingController variable inside class, not inside Container Widget. For numeric input only, set ‘ keyboardType: TextInputType.number ‘ in TextField.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search