Flutter TextField 仅输入十进制数字

Sag*_*ala 14 textfield flutter

我只想在TextFieldin 中输入十进制数Flutter。我试过下面的代码,但这不起作用。它允许使用字母 (az) 和特殊字符。

TextField(
  controller:
      new TextEditingController(text: listDisplay[position].getBlockQty(),
  ),       
  textAlign: TextAlign.center,
  maxLines: 1,       
  keyboardType: TextInputType.numberWithOptions(
      decimal: true,
      signed: false),       
),
Run Code Online (Sandbox Code Playgroud)

Joh*_*tty 16

您可以TextInputFormatter用于这种情况。

这是一个相同的例子,

子类 TextInputFormatter

import 'package:flutter/services.dart';

class RegExInputFormatter implements TextInputFormatter {
  final RegExp _regExp;

  RegExInputFormatter._(this._regExp);

  factory RegExInputFormatter.withRegex(String regexString) {
    try {
      final regex = RegExp(regexString);
      return RegExInputFormatter._(regex);
    } catch (e) {
      // Something not right with regex string.
      assert(false, e.toString());
      return null;
    }
  }

  @override
  TextEditingValue formatEditUpdate(
      TextEditingValue oldValue, TextEditingValue newValue) {
    final oldValueValid = _isValid(oldValue.text);
    final newValueValid = _isValid(newValue.text);
    if (oldValueValid && !newValueValid) {
      return oldValue;
    }
    return newValue;
  }

  bool _isValid(String value) {
    try {
      final matches = _regExp.allMatches(value);
      for (Match match in matches) {
        if (match.start == 0 && match.end == value.length) {
          return true;
        }
      }
      return false;
    } catch (e) {
      // Invalid regex
      assert(false, e.toString());
      return true;
    }
  }
}

Run Code Online (Sandbox Code Playgroud)

将它与您的文本字段一起使用

final _amountValidator = RegExInputFormatter.withRegex('^\$|^(0|([1-9][0-9]{0,}))(\\.[0-9]{0,})?\$');
...
TextField(
  inputFormatters: [_amountValidator],
  keyboardType: TextInputType.numberWithOptions(
    decimal: true,
     signed: false,
   ),
 )
Run Code Online (Sandbox Code Playgroud)

  • 对于限制:RegExInputFormatter.withRegex('^[0-9]{0,6}(\\.[0-9]{0,2})?\$') (2认同)

Cho*_*san 10

尝试这个:

inputFormatters: [
        FilteringTextInputFormatter.allow(RegExp(r"[0-9.]")),
        TextInputFormatter.withFunction((oldValue, newValue) {
          try {
            final text = newValue.text;
            if (text.isNotEmpty) double.parse(text);
            return newValue;
          } catch (e) {}
          return oldValue;
        }),
      ],
Run Code Online (Sandbox Code Playgroud)

演示:

TextFormField(
      keyboardType:
          TextInputType.numberWithOptions(decimal: true, signed: false),
      onChanged: _yourOnChange,
      inputFormatters: [
        FilteringTextInputFormatter.allow(RegExp(r"[0-9.]")),
        TextInputFormatter.withFunction((oldValue, newValue) {
          try {
            final text = newValue.text;
            if (text.isNotEmpty) double.parse(text);
            return newValue;
          } catch (e) {}
          return oldValue;
        }),
      ],
    )
Run Code Online (Sandbox Code Playgroud)


小智 5

我正在使用这段代码,它给出了不同的结果,在某些手机上它只显示数字,而在其他手机上它显示破折号和其他字符,所以这取决于启动器或类似的东西,我猜,在 iPhone 中,我猜它会显示正确的键盘(我在 iPhone 7s plus 上试过)。

inputFormatters只允许数字,所以你会得到你想要的东西。

   TextField(
     textAlign: TextAlign.start,
     keyboardType: TextInputType.numberWithOptions(decimal: true),
     inputFormatters: <TextInputFormatter>[
                          FilteringTextInputFormatter.digitsOnly
                       ],
   ),
Run Code Online (Sandbox Code Playgroud)

  • `FilteringTextInputFormatter.digitsOnly` 允许仅输入数字,不能输入逗号或句点 (3认同)

Har*_*e G 5

我也一直在寻找这个问题的答案,并经过多次尝试和努力找到了答案。

您可以使用此正则表达式表示十进制。

inputFormatters: [
        FilteringTextInputFormatter.allow(RegExp(r'^[0-9]+.?[0-9]*'))
      ],
Run Code Online (Sandbox Code Playgroud)


Sun*_*nny 1

我在我的一个项目中使用了这段代码,并且工作正常。我希望它能帮助你。

TextField(
         decoration: InputDecoration(
         border: InputBorder.none,
         hintText: "Amount",
         hintStyle:
         TextStyle(color: Colors.grey, fontSize: 12.0),
         ),
         style: TextStyle(color: Colors.black, fontSize: 12.0),
         controller: _amountController[index],
         textInputAction: TextInputAction.done,
         keyboardType: TextInputType.numberWithOptions(decimal: true),)
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

  • TextInputType.numberWithOptions 不起作用。我已经在问题中提到过。 (2认同)