FocusNode: Programmatically Managing Widget Focus
A FocusNode is your handle to a widget's focus state, letting you programmatically request focus or listen for changes. It's essential for forms, like moving focus from one TextField to the next.
Why it exists
Flutter needs a way to manage which widget is currently receiving keyboard input. While simple apps can rely on default behavior, complex UIs like forms or keyboard-navigable interfaces require a way to explicitly control, move, and listen to focus changes. FocusNode provides this granular control.
The mental model
Think of a FocusNode as a remote control for a widget's focus. It's a persistent object that lives outside the ephemeral build cycle. You create this "remote," pair it with a widget (like a TextField), and then use the remote to tell the widget to grab focus, release focus, or to listen for when its focus state changes. These nodes form a tree structure that mirrors the widget hierarchy, determining how focus moves and how keyboard events bubble up.
How it works
A FocusNode is a ChangeNotifier, meaning you can add listeners to react to focus changes. They must be created in a StatefulWidget's initState and cleaned up in dispose to prevent memory leaks and unexpected behavior. You then attach the node to a focusable widget, like a TextField, via its focusNode property. To programmatically give focus, you call focusNode.requestFocus(). To release it, focusNode.unfocus(). Key events are sent to the node with primary focus. If that node's onKeyEvent callback ignores the event, it propagates up the focus tree to its parent until handled or discarded.
When to use it
Use a FocusNode when you need direct control over focus. The most common case is in forms, to move focus to the next TextField when the user submits the current one. It's also used for building custom keyboard navigation, accessibility features, or any scenario where you need to imperatively change which widget is active.
When not to use it
Avoid managing FocusNodes directly for simple cases. The Focus and FocusScope widgets handle the lifecycle automatically for many standard layouts. If you only need to know if a widget has focus during a build, calling Focus.of(context).hasFocus is often simpler and less error-prone than managing your own node. Direct management is powerful but adds complexity.
One canonical example
A login form has two TextFields: one for email and one for password. You create two FocusNodes in initState, emailFocusNode and passwordFocusNode. The email TextField has focusNode: emailFocusNode and an onEditingComplete callback that calls passwordFocusNode.requestFocus(). When the user hits "done" on the keyboard in the email field, focus automatically jumps to the password field. Remember to dispose() both nodes in the State's dispose method.
Interview question
For which scenario is direct management of a FocusNode most appropriate?
- a.To check if a specific TextField currently has keyboard focus during a build.
- b.To ensure a widget receives all keyboard events regardless of its focus state.
- c.To enable automatic focus traversal between multiple TextFields in a simple form.
- d.To programmatically shift keyboard focus from an email TextField to a password TextField after email submission.Correct
Why? this is the answer
Direct management of a FocusNode is ideal for programmatic control, such as moving focus between fields in a form, as described in the canonical example. For simply checking if a widget has focus during a build, Focus.of(context).hasFocus is generally simpler, and automatic traversal is handled by Focus and FocusScope widgets without direct FocusNode management.
Just read this? Test yourself on what you have been reading.
Read the original → api.flutter.dev
Put your scrolling time to good use
Learn one idea, try a quiz and save useful cards for revision. Tezvyn makes it easy to learn and stay current in your tech field, a few minutes at a time.
The iPhone app is on the way
We are building it. Until it lands, nothing here is held back from you: every interview card, your saved cards, streaks and the job board all work in Safari, plus hundreds of free practice quizzes of thirty questions each. Sign in and it all carries over to the app the day it arrives.
Want it as an icon? Tap Share at the bottom of Safari, then Add to Home Screen. It opens full screen and the cards you have read stay available offline.
We are hiring for this. Open roles that interview on flutter — each one lists the topics its interview covers.
See open roles