close

Flutter Get Only Numeric Number Value From TextField Text Input

TextField gadget has a property named as keyboardType which support a contention TextInputType.number. Utilizing this strategy we can without much of a stretch change conduct of Keypad of both android and iOS gadgets. TextInputType.number strategy permits us to open possibly Number sort console when TextField gadget is chosen. So in this instructional exercise we would Flutter Get Only Numeric Number Value From TextField Text Input Widget Android iOS Example Tutorial.

Flutter Get Only Numeric Number Value From TextField Text Input

  • Import material.dart package in your app’s main.dart file.
import ‘package:flutter/material.dart’;
  • Create void main runApp() method and here we would call our main Root MyApp class.
void main() => runApp(MyApp());
  • Make our fundamental Root Class named as MyApp stretches out with StatelessWidget. In this class we would call another class named as TextFieldNumeric().

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
        home: Scaffold(
        appBar: AppBar(
        title: Text('Get Only Numeric Value')),
        body: Center(
          child: TextFieldNumeric()
          )
        )
      );
  }
}
  • Making a class named as TextFieldNumeric broadens StatefulWidget. In this class we would make createState() strategy and get through next class NumericTextFieldWidget name alongside it.

class TextFieldNumeric extends StatefulWidget {
 
  NumericTextFieldWidget createState() => NumericTextFieldWidget();
 
}
  • Making another class named as NumericTextFieldWidget expands State. This class is our fundamental Children class in which we would make the TextField gadget.

class NumericTextFieldWidget extends State {
 
}
  • Making a last sort number variable TextEditingController() to get entered esteem from Text Field gadget.
  final number = TextEditingController();

Here Is My Code

  @override
  Widget build(BuildContext context) {
    final number = TextEditingController();

    return MaterialApp(
      title: 'Show Hide Widget - exerror',
      home: Scaffold(
        appBar: AppBar(
          title: Text("Number Input field - exerror.com"),
        ),
        body: Center(
          child: Column(
            mainAxisAlignment: MainAxisAlignment.center,
            children: <Widget>[
              Container(
                  width: 280,
                  padding: EdgeInsets.all(10.0),
                  child: TextField(
                    controller: number,
                    autocorrect: true,
                    keyboardType: TextInputType.number,
                    decoration:
                        InputDecoration(hintText: 'Enter Your Number Here'),
                  )),
            ],
          ),
        ),
      ),
      debugShowCheckedModeBanner: false,
    );
  }

Here Is My Output

Flutter Get Only Numeric Number Value From TextField Text Input

Method 2


For those who are looking for making TextField or TextFormField accept only numbers as input, try this code block :

for flutter 1.20 or newer versions

TextFormField(
              controller: _controller,
              keyboardType: TextInputType.number,
              inputFormatters: <TextInputFormatter>[
                FilteringTextInputFormatter.allow(RegExp(r'[0-9]')),
              ],
              decoration: InputDecoration(
                  labelText: "whatever you want",
                  hintText: "whatever you want",
                  icon: Icon(Icons.phone_iphone)))

for earlier versions of 1.20

TextFormField(
    controller: _controller,
    keyboardType: TextInputType.number,
    inputFormatters: <TextInputFormatter>[
        WhitelistingTextInputFormatter.digitsOnly
    ],
    decoration: InputDecoration(
        labelText:"whatever you want", 
        hintText: "whatever you want",
        icon: Icon(Icons.phone_iphone)
    )
)

Method 3

For those who need to work with money format in the text fields:

To use only: , (comma) and . (period)

and block the symbol: – (hyphen, minus or dash)

as well as the: ⌴ (blank space)

In your TextField, just set the following code:

keyboardType: TextInputType.numberWithOptions(decimal: true),
inputFormatters: [BlacklistingTextInputFormatter(new RegExp('[\\-|\\ ]'))],

The simbols hyphen and space will still appear in the keyboard, but will become blocked.

Summery

So it’s all About All possible solutions. Hope this above all solution helped you a lot. Comment below Your thoughts and your queries. Comment Below on your suggestion.

Check Out Below Article

TypeError: this.getOptions is not a function in vue.js

exerror.com specifically for sharing programming issues and examples. We’ll be sharing some chunks of codes of PHP, Laravel Framework, CSS3, HTML5, MYSQL, Bootstrap, CodeIgniter Framework, JQuery, Javascript, Server, Ionic Framework, Python, flutter, macOS, Angular, etc Programming Language. On our site, I am sure you will find something good solution and a fine example of topics of PHP, Laravel Framework, CSS3, HTML5, MYSQL, Bootstrap, CodeIgniter Framework, JQuery, Javascript, Server, Ionic Framework, Python, flutter, macOS, Angular, etc Programming Language.

Leave a Comment