Created
January 25, 2021 14:04
-
-
Save JaveedIshaq/91345ed17dfb766167c49b52661b2688 to your computer and use it in GitHub Desktop.
How to create number input field in Flutter?
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'package:flutter/material.dart'; | |
import 'package:flutter/services.dart'; | |
void main() => runApp(new MyApp()); | |
class MyApp extends StatelessWidget { | |
@override | |
Widget build(BuildContext context) { | |
return MaterialApp( | |
home: HomePage(), | |
theme: ThemeData(primarySwatch: Colors.blue), | |
); | |
} | |
} | |
class HomePage extends StatefulWidget { | |
@override | |
State<StatefulWidget> createState() { | |
return HomePageState(); | |
} | |
} | |
class HomePageState extends State<HomePage> { | |
@override | |
Widget build(BuildContext context) { | |
return Scaffold( | |
backgroundColor: Colors.white, | |
body: Container( | |
padding: const EdgeInsets.all(40.0), | |
child: Column( | |
mainAxisAlignment: MainAxisAlignment.center, | |
children: <Widget>[ | |
Text("This Input accepts Numbers only"), | |
SizedBox(height: 20), | |
TextField( | |
decoration: InputDecoration( | |
focusedBorder: OutlineInputBorder( | |
borderSide: | |
BorderSide(color: Colors.greenAccent, width: 5.0), | |
), | |
enabledBorder: OutlineInputBorder( | |
borderSide: BorderSide(color: Colors.red, width: 5.0), | |
), | |
hintText: 'Mobile Number', | |
), | |
keyboardType: TextInputType.number, | |
inputFormatters: <TextInputFormatter>[ | |
FilteringTextInputFormatter.digitsOnly | |
], // Only numbers can be entered | |
), | |
SizedBox(height: 20), | |
Text("You can test be Typing"), | |
], | |
)), | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment