Repository navigation
fix: Use the current focusNode in GameWidget when it changes - #4160
Merged
Merged
Conversation
spydon
approved these changes
Oct 29, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
GameWidgetpicked its focus node once, ininitState, and never looked atfocusNodeagain. Whenthe same
GameWidgetwas rebuilt with a differentfocusNode, the new node was never attached, sorequestFocus()on it did nothing and the game stopped receiving key events.disposealso decidedwhether it owned the node by checking
widget.focusNode == nullat that moment, so after switchingfrom a user node to no node it disposed the user's node, and after switching the other way it never
disposed its internal node.
Now
GameWidgetState:_internalFocusNodeand useswidget.focusNode ?? _internalFocusNode,so a new
focusNodeis used as soon as it is passed.didUpdateWidget, whenfocusNodechanges and the old node has the primary focus, requests thefocus on the new one. If a descendant, like an overlay with a text field, has the focus, it is left
alone, since it is moved under the new node in the next build and keeps the focus.
dispose, only disposes_internalFocusNode.Tests in
game_widget_keyboard_test.dartcover a new node taking over the focus and key events,switching between a user node and the internal one, a user node not being disposed, and an overlay
keeping the focus when the node changes. The
focusNodedartdoc and the keyboard input docsdescribe the new behavior.
Checklist
docsand added dartdoc comments with///.examplesordocs.Breaking Change?
Related Issues
Closes #4159