How to create number input field in Flutter?

后端 未结 14 2094
被撕碎了的回忆
被撕碎了的回忆 2020-12-12 21:47

I\'m unable to find a way to create an input field in Flutter that would open up a numeric keyboard. Is this possible with Flutter material widgets? Some github discussions

相关标签:
14条回答
  • 2020-12-12 22:00

    You can try this:

    TextFormField(
         keyboardType: TextInputType.number,
         decoration: InputDecoration(
                  prefixIcon: Text("Enter your number: ")
         ),
         initialValue: "5",
         onSaved: (input) => _value = num.tryParse(input),
    ),
    
    0 讨论(0)
  • 2020-12-12 22:01

    You can specify the number as keyboardType for the TextField using:

    keyboardType: TextInputType.number
    

    Check my main.dart file

    import 'package:flutter/material.dart';
    import 'package:flutter/services.dart';
    
    void main() => runApp(new MyApp());
    
    class MyApp extends StatelessWidget {
      @override
      Widget build(BuildContext context) {
        // TODO: implement build
        return new MaterialApp(
          home: new HomePage(),
          theme: new ThemeData(primarySwatch: Colors.blue),
        );
      }
    }
    
    class HomePage extends StatefulWidget {
      @override
      State<StatefulWidget> createState() {
        return new HomePageState();
      }
    }
    
    class HomePageState extends State<HomePage> {
      @override
      Widget build(BuildContext context) {
        return new Scaffold(
          backgroundColor: Colors.white,
          body: new Container(
              padding: const EdgeInsets.all(40.0),
              child: new Column(
            mainAxisAlignment: MainAxisAlignment.center,
            children: <Widget>[
              new TextField(
                decoration: new InputDecoration(labelText: "Enter your number"),
                keyboardType: TextInputType.number,
                inputFormatters: <TextInputFormatter>[
        FilteringTextInputFormatter.digitsOnly
    ], // Only numbers can be entered
              ),
            ],
          )),
        );
      }
    }
    

    0 讨论(0)
  • 2020-12-12 22:05

    Set the keyboard and a validator

    String numberValidator(String value) {
      if(value == null) {
        return null;
      }
      final n = num.tryParse(value);
      if(n == null) {
        return '"$value" is not a valid number';
      }
      return null;
    }
    
    new TextFormField(
        keyboardType: TextInputType.number, 
        validator: numberValidator, 
        textAlign: TextAlign.right
        ...
    
    • https://docs.flutter.io/flutter/material/TextFormField/TextFormField.html
    • https://docs.flutter.io/flutter/services/TextInputType-class.html
    0 讨论(0)
  • 2020-12-12 22:10

    You can Easily change the Input Type using the keyboardType Parameter and you have a lot of possibilities check the documentation TextInputType so you can use the number or phone value

     new TextField(keyboardType: TextInputType.number)
    
    0 讨论(0)
  • 2020-12-12 22:13

    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)
        )
    )
    
    0 讨论(0)
  • 2020-12-12 22:13

    Here is code for numeric keyboard : keyboardType: TextInputType.phone When you add this code in textfield it will open numeric keyboard.

      final _mobileFocus = new FocusNode();
      final _mobile = TextEditingController();
    
    
         TextFormField(
              controller: _mobile,
              focusNode: _mobileFocus,
              maxLength: 10,
              keyboardType: TextInputType.phone,
              decoration: new InputDecoration(
                counterText: "",
                counterStyle: TextStyle(fontSize: 0),
                hintText: "Mobile",
                border: InputBorder.none,
                hintStyle: TextStyle(
                  color: Colors.black,
                    fontSize: 15.0.
                ),
              ),
              style: new TextStyle(
                  color: Colors.black,
                  fontSize: 15.0,
               ),
              );
    
    0 讨论(0)
提交回复
热议问题