How to create number input field in Flutter?

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

提交回复
热议问题