【问题标题】:TextField errorText not showing文本字段错误文本未显示
【发布时间】:2019-06-14 22:20:16
【问题描述】:

我正在做一个基本的应用程序,但它不能正常工作。我有一个 TextField 但我不希望它是空的。我有一个 textController 并且我使用 errorText 但它不能正常工作。 小米代码是:

 void changeDesc() { showDialog(
      context: context,
      builder: (context) {
        return AlertDialog(
          title: Text('New description'),
          content: TextField(
            controller: _textController,
            decoration: InputDecoration(
              hintText: "description",
              errorText: descriptionIncorrect
                  ? 'Description cannot be empty'
                  : null,
            ),
          ),
          actions: <Widget>[
            FlatButton(
              child: new Text('Ok'),
              onPressed: () {
                setState(() {_textController.text.length == 0
                    ? descriptionIncorrect= true
                    : descriptionIncorrect= false;});
                if (_textController.text.length != 0) {
                  alert.description = _textController.text;
                  Navigator.of(context).pop();
                }
              },
            ),
            FlatButton(
              child: new Text('Cancel'),
              onPressed: () {
                setState(() {
                _textController.text.length == 0
                    ? descriptionIncorrect= true
                    : descriptionIncorrect= false;});
                if (_textController.text.length == 0) {
                  _textController.text = alert.description;
                  Navigator.of(context).pop();
                }
              },
            )
          ],
        );
      });}

当我按下 OK 按钮并且 textField 为空时,应该会出现错误,但不会出现。我已经尝试了一些东西,但我无法按照我的需要进行操作。

谢谢。

【问题讨论】:

    标签: flutter dart


    【解决方案1】:

    您需要执行两个步骤 1- 刷新警报对话框,您需要将其包装在 StatefulBuilder 小部件中。 2-将自动对焦添加到您的文本字段。 这是完整的代码。

    bool descriptionIncorrect = false;
      var _textController = TextEditingController();
      void changeDesc() {
        showDialog(
            context: context,
            builder: (context) {
              return StatefulBuilder(
                builder: (context, setState) {
                  return AlertDialog(
                    title: Text('New description'),
                    content: TextField(
                      autofocus: true,
                      controller: _textController,
                      decoration: InputDecoration(
                        hintText: "description",
                        errorText: descriptionIncorrect
                            ? 'Description cannot be empty'
                            : null,
                      ),
                    ),
                    actions: <Widget>[
                      FlatButton(
                        child: new Text('Ok'),
                        onPressed: () {
                          setState(() {
                            _textController.text.length == 0
                                ? descriptionIncorrect = true
                                : descriptionIncorrect = false;
                            print(_textController.text.length.toString());
                            print(descriptionIncorrect.toString());
                          });
                          if (_textController.text.length != 0) {
                            alert.description = _textController.text;
                            Navigator.of(context).pop();
                          }
                        },
                      ),
                      FlatButton(
                        child: new Text('Cancel'),
                        onPressed: () {
                          setState(() {
                            _textController.text.length == 0
                                ? descriptionIncorrect = true
                                : descriptionIncorrect = false;
                          });
                          if (_textController.text.length == 0) {
                             _textController.text = alert.description;
                            Navigator.of(context).pop();
                          }
                        },
                      )
                    ],
                  );
                },
              );
            });
      }
    

    【讨论】:

      猜你喜欢
      • 2019-08-24
      • 2023-04-03
      • 2014-10-26
      • 2012-01-02
      • 2019-12-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多