我有一个包含文本字段的Statefull小部件。我想知道用户在文本字段外单击的时间。为了做到这一点,我使用了一个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();
}
}