Skip to content

fix: Use the current focusNode in GameWidget when it changes - #4160

Merged
ufrshubham merged 3 commits into
mainfrom
devkage/game-widget-focus-node-update
Oct 29, 2026
Merged

ufrshubham merged 3 commits into
mainfrom
devkage/game-widget-focus-node-update

Conversation

@ufrshubham

Copy link
Copy Markdown
Member

Description

GameWidget picked its focus node once, in initState, and never looked at focusNode again. When
the same GameWidget was rebuilt with a different focusNode, the new node was never attached, so
requestFocus() on it did nothing and the game stopped receiving key events. dispose also decided
whether it owned the node by checking widget.focusNode == null at that moment, so after switching
from 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:

  • Keeps the node it creates in _internalFocusNode and uses widget.focusNode ?? _internalFocusNode,
    so a new focusNode is used as soon as it is passed.
  • In didUpdateWidget, when focusNode changes and the old node has the primary focus, requests the
    focus 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.
  • In dispose, only disposes _internalFocusNode.

Tests in game_widget_keyboard_test.dart cover 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 focusNode dartdoc and the keyboard input docs
describe the new behavior.

Checklist

  • I have followed the Contributor Guide when preparing my PR.
  • I have updated/added tests for ALL new/updated/fixed functionality.
  • I have updated/added relevant documentation in docs and added dartdoc comments with ///.
  • [-] I have updated/added relevant examples in examples or docs.

Breaking Change?

  • Yes, this PR is a breaking change.
  • No, this PR is not a breaking change.

Related Issues

Closes #4159

@ufrshubham
ufrshubham requested a balanced review from Copilot October 28, 2026 17:57

This comment was marked as outdated.

@ufrshubham
ufrshubham enabled auto-merge (squash) October 29, 2026 16:34
@ufrshubham
ufrshubham merged commit c227f98 into main Oct 29, 2026
8 checks passed
@ufrshubham
ufrshubham deleted the devkage/game-widget-focus-node-update branch October 29, 2026 16:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

GameWidget ignores a focusNode that changes after it is mounted

3 participants