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
Add any of these line inside your TextField widget
You should declare TextEditingController variable inside class, not inside Container Widget. For numeric input only, set ‘ keyboardType: TextInputType.number ‘ in TextField.