gpt4 book ai didi

flutter - 如何取消对具有自定义 FocusNode 的 TextField 的聚焦?

转载 作者:行者123 更新时间:2023-12-02 10:49:31 28 4
gpt4 key购买 nike

我知道失去焦点的一般答案是使用这段代码:FocusScope.of(context).requestFocus(new FocusNode());

但是当TextField有自定义focusNode时,这段代码似乎不起作用。

SystemChannels.textInput.invokeMethod('TextInput.hide'); 仍然有效,但它仅删除键盘 - 字段本身仍处于选中状态。

代码(删除不相关部分):

class RegisterScreen extends StatelessWidget {
final phoneNumberTEC = TextEditingController();
final passwordTEC = TextEditingController();
final passwordFocusNode = FocusNode();

@override
Widget build(BuildContext context) {
return this.keyboardDismisser(
context: context,
child: Scaffold(
appBar: new AppBar(
title: new Text("Register"),
),
body: this.page(context),
resizeToAvoidBottomPadding: false,
),
);
}

Widget keyboardDismisser({BuildContext context, Widget child}) {
final gesture = GestureDetector(
onTap: () {
this.passwordFocusNode.unfocus();
FocusScope.of(context).requestFocus(new FocusNode());
SystemChannels.textInput.invokeMethod('TextInput.hide');
},
child: child,
);
return gesture;
}

Widget page(BuildContext context) {
return Column(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: <Widget>[
Container(
padding: new EdgeInsets.all(16.0),
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.center,
children: <Widget>[
this.phoneNumberTextField(context),
this.passwordTextField(context),
]
),
),
// cutting irrelevant widgets out
)
]
);
}

Widget phoneNumberTextField(BuildContext context) {
return TextField(
controller: this.phoneNumberTEC,
decoration: InputDecoration(hintText: "Phone number"),
onSubmitted: (string) {
FocusScope.of(context).requestFocus(this.passwordFocusNode);
},
);
}

Widget passwordTextField(BuildContext context) {
return TextField(
controller: this.passwordTEC,
decoration: InputDecoration(hintText: "Password"),
obscureText: true,
focusNode: this.passwordFocusNode,
onSubmitted: (string) {
this.performRegister(context);
},
);
}

}

最佳答案

这是与 @kasiara 的答案类似的答案,但方式不同。

FocusScope.of(context).unfocus();
_textEditingController.clear();

关于flutter - 如何取消对具有自定义 FocusNode 的 TextField 的聚焦?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53481261/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com