在按下另一个小部件时删除 TextField 焦点

Remove TextField focus while on pressing another widget

我有一个表单页面包含一个小部件,该小部件将return根据api参数形成各种小部件,例如:参数是'type':'text_box 因此,如果参数为 'type',小部件将 return TextField 小部件:'dropdown_box' 它将 return 下拉小部件。

这只是我制作的小部件的示例,真正的问题出在 TextField 小部件中,因为每次我在该字段中输入然后按任何其他表单小部件(而不是按文本字段上的完整按钮)和 select 它将 return 焦点转移到 TextField 之前,如果我按下完成按钮,焦点又是中性的,我想要的是当我按下另一个小部件时,TextField 的焦点是清晰的。

代码

TextFormField(
              minLines: widget.minLines,
              inputFormatters: [
                if (widget.isPrice)
                  MoneyInputFormatter(
                    thousandSeparator: ThousandSeparator.Period,
                    mantissaLength: 0,
                  ),
              ],
              controller: widget.controllerName,
              onTap: () {
                widget.onTap();
              },
              enabled: widget.enabled,
              obscureText: widget.isObsecure,
              style: TextStyle(
                fontSize: 14.0,
                fontWeight: FontWeight.w400,
                color: (widget.textColor != null)
                    ? widget.textColor
                    : widget.enabled
                        ? Colors.black
                        : Colors.black38,
              ),
              keyboardType: widget.keyboardType,
              maxLines: widget.maxLines,
              decoration: InputDecoration(
                  isDense: true, //remove default padding
                  fillColor:
                      widget.inputColor ?? Constants.colorAppointmentTextInput,
                  filled: true, // activate bg color
                  // hintText: widget.hintText,
                  hintText: widget.placeholder ?? 'Masukan ' + widget.hintText,
                  hintStyle: TextStyle(
                    fontSize: 14.0,
                    color: (widget.textColor != null)
                        ? widget.textColor
                        : widget.enabled
                            ? Colors.black54
                            : Colors.black38,
                  ),
                  prefix: widget.prefix,
                  border: OutlineInputBorder(
                      borderRadius: BorderRadius.circular(10),
                      borderSide: BorderSide(
                          color: Constants.colorAppointmentBorderInput,
                          width: 0.5)),
                  enabledBorder: OutlineInputBorder(
                      borderRadius: BorderRadius.circular(10),
                      borderSide: BorderSide(
                          color: Constants.colorAppointmentBorderInput,
                          width: 0.5)),
                  disabledBorder: OutlineInputBorder(
                      borderRadius: BorderRadius.circular(10),
                      borderSide: BorderSide(
                          color: Constants.colorAppointmentBorderInput,
                          width: 0.5)),
                  focusedBorder: OutlineInputBorder(
                    borderRadius: BorderRadius.circular(10),
                    borderSide:
                        BorderSide(color: Constants.redTheme, width: 0.5),
                  ),
                  suffixIcon: widget.suffixIcon),
              validator: (value) {
                if (value.isEmpty) {
                  return 'Please enter some text';
                }
                return null;
              },
            ),

这就是我的意思,小部件将 return 小部件根据 api

@override
  Widget build(BuildContext context) {
    if (widget.questionType == 'text_box') {
      return TextFild();
    } else if (widget.questionType == 'dropdown') {
      return Dropdown();
    } else if (widget.questionType == 'radio') {
      return radioType();
    }
}

您可以使用焦点节点:

myFocusNode = FocusNode()

将其添加到您的 TextFormField:

TextFormField(
  focusNode: myFocusNode,
  ...
);

当您想取消文本字段焦点的事件发生时,例如点击其他按钮等,请使用:

myFocusNode.unfocus();

确保在处理方法中处理 FocusNode:

  @override
  void dispose() {
    myFocusNode.dispose();
    //...
    super.dispose();
  }

您可以使用 FocusNode。 将其添加到 TextformField。

TextFormField(
  focusNode: focusNode,
  ...
);

在 onFieldSubmitted、onSaved 上调用 focusNode.unfocus();。在您显示下拉菜单时点击操作的情况下。

添加这一行:

  FocusScope.of(context).requestFocus(FocusNode());

如果键盘打开它就会关闭,如果关闭它就会打开。