如何在提交时更改文本字段的焦点?

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

How do I change the focus of the text field on Submit?

问题

我有一个文本字段,它有一个onSubmit方法,在其中我进行验证,然后聚焦到另一个字段,但出于某种原因,焦点不起作用。以下是代码部分的翻译:

onSubmitted: (value) {
    //print("ga test");

    if (!widget.validator?.call(value)) {
      setState(() {
        showError = true;
      });
    }
    if (widget.nextFocus != null) {
      FocusScope.of(context).requestFocus(widget.nextFocus);
    }
  },
英文:

I have a text field and it has an onSubmit method, inside which I check for validation and then focus on another field, but for some reason the focus does not work

onSubmitted: (value) {
        //print("ga test");

        if (!widget.validator?.call(value)) {
          setState(() {
            showError = true;
          });
        }
        if (widget.nextFocus != null) {
          FocusScope.of(context).requestFocus(widget.nextFocus);
        }

      },

答案1

得分: 0

我这样做了,而且它有效果:

if (widget.validator != null) {
  setState(() {
    showError = !widget.validator?.call(value);
  });
}
if (widget.nextFocus != null) {
  FocusScope.of(context).requestFocus(widget.nextFocus);
}
英文:

I did so and it worked

if (widget.validator != null) {
          setState(() {
            showError = !widget.validator?.call(value);
          });
        }
        if (widget.nextFocus != null) {
          FocusScope.of(context).requestFocus(widget.nextFocus);
        }

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

发表评论

匿名网友

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

确定