gpt4 book ai didi

flutter : TextField with FocusNode does not get called when clicked outside of TextField

转载 作者:IT老高 更新时间:2023-10-28 12:44:54 32 4
gpt4 key购买 nike

我有一个包含文本字段的 Statefull Widget。我想知道用户何时在文本字段外单击。为了做到这一点,我正在使用 FocusNode。现在的问题是当用户在 texfield 之外单击时,我的回调不会被调用。这是我的代码

class TestText extends State<SingleFieldEdit> 
{
TextEditingController _qTextEditController = new TextEditingController();
FocusNode _focus;

TestText();

@override
void initState() {
super.initState();
_focus = new FocusNode();
_focus.addListener(_onFocusChange);
}

@override
void dispose() {
super.dispose();
_focus.dispose();
}

void _onFocusChange(){
print("Focus changed - Does not get called when clicked outside of textbox");
if(_focus.hasFocus==false){
setState(() {
_enableEdit=false;
_qTextEditController.text = _existingText;
});

}
}


Widget getEditableField()
{
Widget containerBorder = new Container(
margin: const EdgeInsets.all(0.0),
padding: const EdgeInsets.only(top:0.0,bottom:0.0),
decoration: new BoxDecoration(
border: new Border(top:BorderSide(width: 0.4),bottom: BorderSide(width: 0.4) ),
),
child: new Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
crossAxisAlignment: CrossAxisAlignment.center,
children: <Widget>[
new Flexible(child:TextField(
focusNode: _focus,
controller: _qTextEditController,
decoration: InputDecoration.collapsed(hintText: 'Your hint goes here',enabled: _enableEdit),//InputDecoration(hintText: 'Your hint goes here', /*labelText: "Your Label goes here",*/ enabled: _enableEdit),
)),
getIcon(),
],)
);

return getEditableField
}

@override
Widget build(BuildContext context)
{
return getEditableField();
}

}

最佳答案

用 GestureDetector 包裹你的 Scaffold 并将 onTap 设置为专注于另一个节点

关于 flutter : TextField with FocusNode does not get called when clicked outside of TextField,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54346302/

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