0

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

2 Answers 2

2

Add any of these line inside your TextField widget


          keyboardType: TextInputType.number,

             or 

         keyboardType: TextInputType.phone,
Sign up to request clarification or add additional context in comments.

1 Comment

worked thanks ..
0

You should declare TextEditingController variable inside class, not inside Container Widget. For numeric input only, set ' keyboardType: TextInputType.number ' in TextField.

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.