在iOS设备上使用TextInputType.number时,我遇到了键盘上缺少"."(小数点)字符的问题.我已经正确地设置了输入格式化程序,只允许数字和".性格,但仍然是"."键盘上看不到字符.以下是我的代码片段:

Container(
                                              width: 65.w,
                                              height: 6.w,
                                              child: TextFormField(
                                                // The validator receives the text that the user has entered.
                                                controller:
                                                    _currentWeightController,
                                                textAlign: TextAlign.left,
                                                keyboardType: TextInputType.number,
                                                inputFormatters: [
                                                  FilteringTextInputFormatter
                                                      .allow(RegExp('[0-9.]')),
                                                ],
                                                style: TextStyle(
                                                    fontSize: 20,
                                                    color: Colors.black),

                                                // decoration: InputDecoration(
                                                //   hintText:
                                                //       '${num.parse(measurementRes[targetWeight] ?? "0").ceil().toString()}',
                                                // ),
                                              ),
                                            ),

该代码在Android上运行良好,既显示数字又显示".键盘上的字符.然而,在iOS设备上,".键盘上看不到字符.任何帮助都将不胜感激.

推荐答案

问题是在您的代码中,您使用的是:TextInputType.number

...针对无符号数字信息进行优化without a decimal point.

...
keyboardType: TextInputType.number,

相反,您应该使用:

keyboardType: const TextInputType.numberWithOptions(decimal: true),


Container(
      width: 65.w,
      height: 6.w,
      child: TextFormField(
        // The validator receives the text that the user has entered.
        controller: _currentWeightController,
        textAlign: TextAlign.left,
        keyboardType: const TextInputType.numberWithOptions(decimal: true),
        inputFormatters: [
          FilteringTextInputFormatter.allow(RegExp('[0-9.]')),
        ],
        style: TextStyle(fontSize: 20, color: Colors.black),

        // decoration: InputDecoration(
        //   hintText:
        //       '${num.parse(measurementRes[targetWeight] ?? "0").ceil().toString()}',
        // ),
      ),
    )

Ios相关问答推荐

如何加密字符串使用AES. GCM在Dart和解密相同的Swift?

Flutter WebRTC:GetUserMedia()不适用于Safari和IOS设备

在iOS中禁用URLSession自动重试机制

在视图上执行手势会阻止相机操作

ScrollViewReader Scrollto Not Working

IOS-获取本地化系统映像

在flutter中使用flatter_screenutil这样的软件包的目的是什么?

如何在 Android 上的 Flutter 应用中录制内部音频?

拔下设备后,DriverKit USB 驱动程序 (dext) 进程不会终止

xcode-select:找不到clang

小部件链接有效,但不执行任何操作

UIViewControllerRepresentable 在 Xcode 14 iOS 16 上崩溃

如何使用 swift 显示 .svg 图像

在 Xcode 5 中为超级视图添加间距约束

使用 AVAudioPlayer 播放声音

Xcode 11 向后兼容性:UIWindowScene 仅在 iOS 13 或更高版本中可用

将 iPhone xib 转换为 iPad xib?

如何防止 iOS 设备上的显示屏变暗和关闭?

如何使用 presentModalViewController 创建透明视图

从 Swift 转换为 Objective-c 的工具