I want to control the TextField
widget when f user taps on it. How can I implement the focusNode
property? There's no detailed explanation in the description.
Asked
Active
Viewed 3.0k times
23

E. Moffat
- 3,165
- 1
- 21
- 34

Ahmad Jamil Al Rasyid
- 498
- 2
- 7
- 19
-
2What exactly is the question. " how can i implement this focusNode property" is quite vague. – Günter Zöchbauer Apr 19 '18 at 04:09
3 Answers
32
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, ...);
}

Cody Gray - on strike
- 239,200
- 50
- 490
- 574

Günter Zöchbauer
- 623,577
- 216
- 2,003
- 1,567
13
Don't forget to dispose of it afterwards to avoid memory leaks:
@override
void dispose() {
focusNode.dispose();
super.dispose();
}

BerlinUkn0w
- 458
- 4
- 8
0
How to Focus Control from outside tapped or pressed;
FocusNode emailFocus = FocusNode();
FocusNode passFocus = FocusNode();
//Add GestureDetector for getting outside touch event;
GestureDetector(
onTap: () {
try {
emailFocus.unfocus();
passFocus.unfocus();
} catch (e) {
print(e);
}
print("Tapped");
},
//add focus in textfield
TextFormField(
autofocus: true,
focusNode: passFocus,

Poritosh David
- 101
- 1
- 5