In this article, I will be explaining and implementing Flutter textfield only numbers, what it means, its role and how to implement Flutter textfield only numbers in Flutter app. I will explain each and everything step by step so you don’t have any confusions when you finish reading this post. Now without wasting anymore of your valuable time, let’s get right into implementing Flutter textfield only numbers in Flutter app.
What is Flutter Textfield Only Numbers?
In our previous articles, we have discussed other properties of Flutter textfield, if you want to check them then click here. So what is Flutter textfield only numbers, as we know already that by default the input value that a user give to the Flutter textfield is a string, means it can be text, characters, special characters, numbers etc.
What if we want only numbers to be inputted by the user. We can specify that so the Flutter textfield will only take numbers. Let’s start implementing it through proper code and example in the next sections.
Default Flutter Textfield Input Type
Let’s see what the default input type of the Flutter textfield through an example:
TextField()

Implementing Flutter Textfield Only Numbers
Let’s now implement Flutter textfield only numbers so the input that the user can give is only numbers. For that, you have to use the keyboard type constructor of the Flutter textfield widget. Then by passing it the text input type class and accessing the number property of the text input type class, you can implement the Flutter textfield only numbers.
keyboardType: TextInputType.number

Beautiful Flutter Textfield Source Code
import 'package:flutter/material.dart'; void main() { runApp(const MyApp()); } class MyApp extends StatelessWidget { const MyApp({Key? key}) : super(key: key); @override Widget build(BuildContext context) { return MaterialApp( debugShowCheckedModeBanner: false, home: Homepage(), ); } } class Homepage extends StatelessWidget { @override Widget build(BuildContext context) { return SafeArea( child: Scaffold( body: Center( child: Padding( padding: EdgeInsets.symmetric(horizontal: 40), child: TextField( style: TextStyle(color: Colors.pink), keyboardType: TextInputType.number, decoration: InputDecoration( prefixIcon: Icon( Icons.phone_android, color: Colors.pink, ), suffixIcon: Icon( Icons.visibility, color: Colors.pink, ), labelText: 'Enter number', labelStyle: TextStyle(color: Colors.pink.withOpacity(.8)), enabledBorder: OutlineInputBorder( borderSide: BorderSide(color: Colors.pink), borderRadius: BorderRadius.circular(30).copyWith( bottomRight: Radius.circular(0), topLeft: Radius.circular(0))), focusedBorder: OutlineInputBorder( borderSide: BorderSide(color: Colors.pink), borderRadius: BorderRadius.circular(30).copyWith( bottomRight: Radius.circular(0), topLeft: Radius.circular(0)))), ))), )); } }
Conclusion
In conclusion, we have successfully implemented and now have a complete understanding of how to implement Flutter textfield only numbers in Flutter app. I would be looking forward to have your valuable feedback and encourage you to have a look at my other informative posts on amazing Flutter widgets, beautiful Flutter templates, and amazing Flutter animations. That’s it for this article. Thanks for reading.