Flutter : how to use focusNode property on TextField
FocusNode focusNode;
void initState() {
focusNode = new FocusNode();
// listen to focus changes
focusNode.addListener(() => print('focusNode updated: hasFocus: ${focusNode.hasFocus}'));
}
void setFocus() {
FocusScope.of(context).requestFocus(focusNode);
}
Widget build() {
return
...
new TextField(focusNode: focusNode, ...);
}
Don't forget to dispose of it afterwards to avoid memory leaks:
@override
void dispose() {
focusNode.dispose();
super.dispose();
}