How to create number input field in Flutter?

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

提交回复
热议问题