flutter : Maxlength not working on Textfield

标签 flutter dart

我正在创建自己的 PinCode 小部件,基本上当我按下按钮时,按钮的值将传递给 TextField。例如,如果我按下按钮“1”,它会将值“1”传递给 TextField。

我有配置最大长度 = 4 的 TextField。问题是当我写超过 4 时它仍然在写并且没有阻止它写 TextField。

我做错了吗?

编辑

我尝试使用 this但不会阻止写入。

TextFormField(
      inputFormatters: [
        LengthLimitingTextInputFormatter(4),
      ]
    )

enter image description here

这是我的代码

class _PinCodeScreenState extends State<PinCodeScreen> {

  TextEditingController _pinCodeController = TextEditingController();
  bool _obsecureToggle = false;
  @override
  void dispose() {
    _pinCodeController.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    double mqHeight = MediaQuery.of(context).size.height;
    double mqWidth = MediaQuery.of(context).size.width;
    final textTheme = Theme.of(context).textTheme;
    return Scaffold(
      backgroundColor: Theme.of(context).primaryColor,
      body: Padding(
        padding: const EdgeInsets.all(16.0),
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.stretch,
          mainAxisSize: MainAxisSize.min,
          children: <Widget>[
            Flexible(
              flex: 4,
              child: Container(
                height: double.infinity,
                child: TextFormField(
                  maxLengthEnforced: true,
                  maxLength: 4,
                  textAlign: TextAlign.center,
                  controller: _pinCodeController,
                  readOnly: true,
                  decoration: InputDecoration(
                    border: InputBorder.none,
                  ),
                  style: textTheme.display3
                      .copyWith(color: Colors.white, letterSpacing: 40),
                ),
              ),
            ),
            Flexible(
              flex: 14,
              child: Container(
                height: double.infinity,
                decoration: BoxDecoration(
                  border: Border.all(color: Colors.grey, width: .5),
                  borderRadius: BorderRadius.circular(30),
                ),
                child: Wrap(
                  alignment: WrapAlignment.spaceEvenly,
                  spacing: 4,
                  children: _listPinCodeNumber
                      .map(
                        (f) => ButtonPinCode(
                          child: f.number == "backspace"
                              ? Icon(Icons.backspace)
                              : Text(
                                  f.number == "backspace" ? "<" : f.number,
                                  style: textTheme.display1
                                      .copyWith(color: Colors.black),
                                ),
                          mediaQueryHeight: mqHeight,
                          mediaQueryWidth: mqWidth,
                          onPressed: () => _actionPinCode("${f.number}"),
                        ),
                      )
                      .toList(),
                ),
              ),
            ),
          ],
        ),
      ),
    );
  }

  _actionPinCode(parameter) {
    switch (parameter) {
      case "backspace":
        setState(() {
          if (_pinCodeController.text.length <= 1) {
            _pinCodeController.text = "";
            print(_pinCodeController.text);
          } else {
            _pinCodeController.text = _pinCodeController.text
                .substring(0, (_pinCodeController.text.length - 1));
            print(_pinCodeController.text);
          }
        });
        break;
      default:
        setState(() {
          if (_pinCodeController.text.length != 0) {
            _pinCodeController.text += parameter;
            print(_pinCodeController.text);
          } else {
            _pinCodeController.text = parameter;
            print(_pinCodeController.text);
          }
        });
    }
  }
}

最佳答案

试试这个

 TextFormField(
      maxLength: 4,
      inputFormatters: [
         LengthLimitingTextInputFormatter(4),],
      textAlign: TextAlign.center,
      controller: _pinCodeController,
      readOnly: true,
      decoration: InputDecoration(
            border: InputBorder.none,
            hintText: '',
            counterStyle: TextStyle(color: Colors.black)
           ),
      style: TextStyle(color: Colors.black, letterSpacing: 40),
        ),

关于 flutter : Maxlength not working on Textfield,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59340496/

相关文章:

macos - 无法在 macOS Catalina 10.15.7 中打开 Android Studio

flutter 导航器处于 initState 状态,无法正常工作

flutter - 如何在 just_audio 中扩展后流式传输带有额外信息的 mp3 文件

flutter - 命名路由如何在 flutter web 中具有 URL 参数?

Dart 'The function isn' t defined' 尽管已定义

javascript - 不同的 *Target 属性的目的是什么?

android-studio - Flutter App不会显示内容,除非单击某些按钮

Flutter 设置一周中特定日期的通知

dart - 如何更改最小化时出现的flutter应用程序的主题

dart - Dart 可以用作通用语言吗?