How to create number input field in Flutter?

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

    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.

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

    keyboardType: TextInputType.number would open a num pad on focus, I would clear the text field when the user enters/past anything else.

    keyboardType: TextInputType.number,
    onChanged: (String newVal) {
        if(!isNumber(newVal)) {
            editingController.clear();
        }
    }
    
    // Function to validate the number
    bool isNumber(String value) {
        if(value == null) {
            return true;
        }
        final n = num.tryParse(value);
        return n!= null;
    }
    
    0 讨论(0)
提交回复
热议问题