【问题标题】:TextInputType.numberWithOptions doesn't show comma or dotTextInputType.numberWithOptions 不显示逗号或点
【发布时间】:2019-11-11 13:56:23
【问题描述】:
TextInputType.numberWithOptions(decimal: true),

我在TextFormField 中将上述代码用作keyboardType,但在某些设备中,该设备不显示逗号或点。截图如下:

考虑使用这个包:https://pub.dev/packages/virtual_keyboard

你知道如何解决吗?

这并不能解决我的问题,因为就我而言,它发生在 Android 上。 Flutter TextField with number keyboard, comma is needed instead of period (Only iOS)

【问题讨论】:

标签: flutter


【解决方案1】:

这个参数可以解决问题

keyboardType: TextInputType.numberWithOptions(decimal: true),

【讨论】:

  • 看看我的问题的第一行。我已经给出了那个参数。
  • 看起来像是特定于设备的问题,我遇到了我在某些设备上遇到的问题以及点和逗号 - 它们相互冲突
  • 我认为这与本地化有关。有些国家使用小数,有些使用点。
【解决方案2】:

这是三星键盘中众所周知的 Android 问题
https://github.com/flutter/flutter/issues/61175

TLDR;

通常的解决方法是回退到文本键盘。

我用这个变通方法开发了一个包,以便轻松获取键盘供应商名称,然后制作一些狡猾的if samsung then TextInputType.text
包:keyboard_name
但是,它并没有解决问题。

长而更酷方式

首先:格式化输入器
注意:我没有找到启用逗号的方法

将输入的点转换为逗号,不用担心禁用的逗号
为此,您需要扩展文本输入格式化程序

class CommaFormatter extends TextInputFormatter {
  @override
  TextEditingValue formatEditUpdate(
    TextEditingValue oldValue,
    TextEditingValue newValue,
  ) {
    String _text = newValue.text;
    //This is only if you need signed numbers. Will convert the first '.'(dot) to '-'(minus)
    //if (_text.isNotEmpty && _text[0] == '.') 
    //  _text = _text.replaceFirst('.', '-');
    return newValue.copyWith(
      text: _text.replaceAll('.', ','),
    );
  }
}

另外,您可以在此之后放置另一个 smartass 正则表达式来格式化

TextFormField(
    inputFormatters: [
        CommaFormatter(),
        FilteringTextInputFormatter.allow(
                        RegExp(
                          //r'^[-]{0,1}[0-9]*[,]?[0-9]*', //signed regex    
                          r'^[0-9]*[,]?[0-9]*', 
                        ),
                      ),
    ],
    /.../
    }

【讨论】:

    【解决方案3】:

    在 iOS 上,您必须在 ios 构建设置中启用 de(或除 en_US 之外的任何其他语言环境)语言环境,即使对于 Flutter 应用也是如此。

    1. 在你的flutter项目中打开IOS文件夹
    2. 在 IOS 文件夹中,使用 xCode 打开 Runner.xcworkspace 文件夹。
    3. 点击 runner 你会看到一堆选项,点击 info 并在 Localization native development region 下选择 United States。
    4. 重建项目 Here is the image attached

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-06-28
      • 2012-06-09
      • 1970-01-01
      • 1970-01-01
      • 2013-01-24
      • 1970-01-01
      • 2015-10-15
      相关资源
      最近更新 更多