【问题标题】:Flutter textformfield has 2 lines at the bottomFlutter textformfield 底部有 2 行
【发布时间】:2020-10-13 09:04:19
【问题描述】:

我正在尝试使用 TextFormField 来管理用户输入,但底部有两行,我想摆脱位于文本中间的第一条细线,但无法这样做。

我的代码是这样的:

           Padding(
            padding: const EdgeInsets.symmetric(horizontal: 50.0),
            child: Form(
              key: _formKey,
              child: ConstrainedBox(
                constraints: BoxConstraints.tight(const Size(250, 50)),
                child: TextFormField(
                  maxLines: 1,
                  maxLength: 5,
                  maxLengthEnforced: true,
                  initialValue: '',
                  keyboardType: TextInputType.text,
                  autofocus: false,
                  decoration: InputDecoration(
                    helperText: 'Enter ref code',
                  ),
                  validator: _codeValidator,
                  onSaved: (value) => _inputCode = value.trim(),
                  style: textTheme.headline6.copyWith(letterSpacing: 40),
                ),
              ),
            ),
          ),

在搜索了一些答案后,我尝试像这样修改InputDecoration

                  decoration: InputDecoration(
                    border: InputBorder.none,
                    helperText: 'Enter ref code',
                  ),

但它会去掉第二行,第一丑行还在。

扑扑医生:

[✓] Flutter (Channel stable, 1.22.1, on Mac OS X 10.15.6 19G2021, locale en-AU)
 
[✓] Android toolchain - develop for Android devices (Android SDK version 29.0.3)
[✓] Xcode - develop for iOS and macOS (Xcode 12.0.1)
[✓] Android Studio (version 3.6)
[✓] VS Code (version 1.50.0)
[✓] Connected device (1 available)

• No issues found!

另一个问题是,即使使用maxLength: 5,maxLengthEnforced: true,我仍然可以输入超过 5 个字符,它不会自动停止。

【问题讨论】:

  • flutter 错误与flutter doctor 无关,flutter doctor 仅用于安装flutter 而不是代码和错误,并且您没有使用一般的TextFormField 您正在使用包吗?
  • 您提供的代码工作正常,没有问题,我用 dartpad 检查过没有问题发生。您还指出,即使 maxlength 也可以输入,您也可以输入 5 个数字。你能显示更多代码吗@xmanreturn

标签: flutter


【解决方案1】:

这不是 Flutter 问题,这是因为设备键盘正在尝试拼写检查和自动更正。

要禁用它,我们必须从输入设置中手动禁用这些选项。

我在虚拟键盘中禁用的设置:

结果:

你可以试试这个包:

pin_code_fields

截图:

更多参考资料:

StackOverflow Answer

GitHub Issue

【讨论】:

  • 您建议的库绝对有效,谢谢!
猜你喜欢
  • 2019-10-24
  • 2020-01-27
  • 1970-01-01
  • 2021-08-15
  • 2021-08-26
  • 1970-01-01
  • 2018-11-11
  • 2018-10-04
  • 2020-05-18
相关资源
最近更新 更多