How to create number input field in Flutter?

后端 未结 14 2092
被撕碎了的回忆
被撕碎了的回忆 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 21:48

    You can use this two attributes together with TextFormField

     TextFormField(
             keyboardType: TextInputType.number
             inputFormatters: [WhitelistingTextInputFormatter.digitsOnly],
    

    It's allow to put only numbers, no thing else ..

    https://api.flutter.dev/flutter/services/TextInputFormatter-class.html

    0 讨论(0)
  • 2020-12-12 21:48

    You can use Form Builder Package to display any type of form field. For number input field,

    FormBuilderTextField(
         keyboardType: TextInputType.number,
         validators: [
              FormBuilderValidators.numeric(),
         ],
    ),
    
    0 讨论(0)
  • 2020-12-12 21:54

    Through this option you can strictly restricted another char with out number.

     inputFormatters: [FilteringTextInputFormatter.digitsOnly],
     keyboardType: TextInputType.number,
    

    For using above option you have to import this

    import 'package:flutter/services.dart';
    

    using this kind of option user can not paste char in a textfield

    0 讨论(0)
  • 2020-12-12 21:54

    If you need to use a double number:

    keyboardType: TextInputType.numberWithOptions(decimal: true),
    inputFormatters: [FilteringTextInputFormatter.allow(RegExp('[0-9.,]')),],
    onChanged: (value) => doubleVar = double.parse(value),
    

    RegExp('[0-9.,]') allows for digits between 0 and 9, also comma and dot.

    double.parse() converts from string to double.

    Don't forget you need: import 'package:flutter/services.dart';

    0 讨论(0)
  • 2020-12-12 21:55

    Here is code for actual Phone keyboard on Android:

    Key part: keyboardType: TextInputType.phone,

      TextFormField(
        style: TextStyle(
          fontSize: 24
        ),
        controller: _phoneNumberController,
        keyboardType: TextInputType.phone,
        decoration: InputDecoration(
          prefixText: "+1 ",
          labelText: 'Phone number'),
        validator: (String value) {
          if (value.isEmpty) {
            return 'Phone number (+x xxx-xxx-xxxx)';
          }
          return null;
        },
      ),
    
    0 讨论(0)
  • 2020-12-12 21:55

    For number input or numeric keyboard you can use keyboardType: TextInputType.number

    TextFormField(
      decoration: InputDecoration(labelText:'Amount'),
        controller: TextEditingController(
      ),
      validator: (value) {
        if (value.isEmpty) {
          return 'Enter Amount';
        }
      },
      keyboardType: TextInputType.number
    )
    
    0 讨论(0)
提交回复
热议问题