TextFormField验证器文本

huangapple go评论50阅读模式
英文:

TextFormField validator text

问题

I'm new to flutter. Is there any way to know if the validator text of a text form field is currently being shown on the screen? I would like to setState of a value if the validator is being shown. Thoughts?

英文:

I'm new to flutter. Is there any way to know if the validator text of a text form field is currently being shown on the screen? I would like to setState of a value if the validator is being shown. Thoughts?

答案1

得分: 2

你可以声明一个状态,然后更新它的值。

TextFormField(
validator: (value) {
if (value.isEmpty) {
setState(() {
_isValidatorVisible = true;
});
return '请输入一个值';
}
setState(() {
_isValidatorVisible = false;
});
return null;
},
),

在这个示例中,如果文本字段为空,你会显示一个指示器。由于你知道你正在显示指示器,你可以使用这个信息并设置状态。

英文:

You can declare a state and then update it's value.

    TextFormField(
        validator: (value) {
          if (value.isEmpty) {
            setState(() {
              _isValidatorVisible = true;
            });
            return 'Please enter a value';
          }
          setState(() {
            _isValidatorVisible = false;
          });
          return null;
        },
      ),

In this example, if the TextField is empty you display an indicator. Since you know you showing the indicator, you can use that information and set the state.

huangapple
  • 本文由 发表于 2023年6月8日 13:00:52
  • 转载请务必保留本文链接:https://go.coder-hub.com/76428736.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定