Flutter 如何在文本字段中仅显示双值

Flutter 如何在文本字段中仅显示双值,flutter,dart,Flutter,Dart,我有一个文本字段,它在onChange中将一个值变量更新为双格式。。。我希望文本字段仅显示更新后的值变量。。我不希望文本字段通过按错误的键盘键来显示任何字符串值或不是双格式的值。即使使用启用了十进制的数字键盘,我们也可以多次按十进制键,这是我不想在文本字段中显示的 TextFormField( decoration: InputDecoration(labelText: 'Value'), onChanged: (s) { if (double

我有一个文本字段,它在onChange中将一个值变量更新为双格式。。。我希望文本字段仅显示更新后的值变量。。我不希望文本字段通过按错误的键盘键来显示任何字符串值或不是双格式的值。即使使用启用了十进制的数字键盘,我们也可以多次按十进制键,这是我不想在文本字段中显示的

TextFormField(
        decoration: InputDecoration(labelText: 'Value'),
        onChanged: (s) {
          if (double.tryParse(s) != null)
            setState(() {
              value = double.parse(s);
            });
        },
      )

如何在TextField中仅显示解析后的值

您好,您不能为此使用
onChange
,因为它只调用了一次
TextField

你应该试试这个

TextFormField(
     inputFormatters: <TextInputFormatter>[
     LengthLimitingTextInputFormatter(12), //max length of 12 characters
     WhitelistingTextInputFormatter.digitsOnly,//Only numbers
     BlacklistingTextInputFormatter.singleLineFormatter,//No line break
     WhitelistingTextInputFormatter(RegExp("[0-9]+.[0-9]")) //only double values
                ],
              )
TextFormField(
输入格式化程序:[
LengthLimitingTextInputFormatter(12),//最大长度为12个字符
WhiteListingDeputFormatter.digitsOnly,//仅数字
BlacklistingDeputFormatter.singleLineFormatter,//无换行符
WhiteListingTestExputFormatter(RegExp(“[0-9]+[0-9]”)//仅双值
],
)

Lear more或

@user8773560是正确的,但是十进制数的RegExp是错误的,不起作用,因此正确答案是

inputFormatters: [
          WhitelistingTextInputFormatter(RegExp(r'(^\d*\.?\d*)'))
        ]

在我的另一个问题中也得到了回答

请将以下属性添加到TextFormField小部件

TextFormField(
 keyboardType:TextInputType.numberWithOptions(decimal: true),
     inputFormatters: <TextInputFormatter>[
        FilteringTextInputFormatter.allow(RegExp(r'^(\d+)?\.?\d{0,2}'))
      ],
    );
TextFormField(
键盘类型:TextInputType.numberWithOptions(十进制:true),
输入格式化程序:[
允许(RegExp(r'^(\d+?\。?\d{0,2}'))
],
);

根据@delmin的答案进行回答,但也允许使用负数

inputFormatters: [
   FilteringTextInputFormatter.allow(RegExp(r'(^-?\d*\.?\d*)'))
]

谢谢你的回复。。。但是,要加倍的格式化程序似乎不起作用
whitelistingdeputFormatter(RegExp(“[0-9]+[0-9]”)