Skip to content

Fix GuessInput focus persistence using StatefulWidget#13402

Open
harshyadavDeveloper wants to merge 3 commits into
flutter:mainfrom
harshyadavDeveloper:fix-guessinput-focus-lifecycle
Open

Fix GuessInput focus persistence using StatefulWidget#13402
harshyadavDeveloper wants to merge 3 commits into
flutter:mainfrom
harshyadavDeveloper:fix-guessinput-focus-lifecycle

Conversation

@harshyadavDeveloper
Copy link
Copy Markdown
Contributor

Description of what this PR is changing or adding, and why:

Converts GuessInput from a StatelessWidget to a
StatefulWidget to preserve the FocusNode and
TextEditingController across rebuilds.

This ensures _focusNode.requestFocus() works as intended after
submitting input and demonstrates proper lifecycle management for
long-lived objects used by text fields.

The change also adds disposal for the FocusNode and
TextEditingController to avoid resource leaks.

Issues fixed by this PR (if any):

Fixes #13392

PRs or commits this PR depends on (if any):

None.

Presubmit checklist

@harshyadavDeveloper harshyadavDeveloper requested review from a team and sfshaza2 as code owners May 19, 2026 05:39
Copy link
Copy Markdown
Contributor

@gemini-code-assist gemini-code-assist Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request refactors the GuessInput widget from a StatelessWidget to a StatefulWidget to properly manage the lifecycle of the TextEditingController and FocusNode, including their disposal. A review comment suggests adding a length validation check in the _onSubmit method to ensure that only five-letter guesses are processed, which prevents potential runtime errors when interacting with the underlying game logic.

Comment on lines 86 to 90
void _onSubmit() {
onSubmitGuess(_textEditingController.text.trim());
widget.onSubmitGuess(_textEditingController.text.trim());
_textEditingController.clear();
_focusNode.requestFocus();
}
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

The _onSubmit method calls onSubmitGuess with the trimmed text without validating its length. Since the Game.guess implementation (via Word.fromString) throws an ArgumentError if the string length is not exactly 5, this will cause the application to crash if a user submits an empty or partially filled input. Adding a length check prevents this crash and allows the user to correct their input before resubmitting.

  void _onSubmit() {
    final text = _textEditingController.text.trim();
    if (text.length == 5) {
      widget.onSubmitGuess(text);
      _textEditingController.clear();
    }
    _focusNode.requestFocus();
  }

@harshyadavDeveloper
Copy link
Copy Markdown
Contributor Author

Thanks for catching this. I added a length check before submitting guesses so invalid input no longer reaches Word.fromString, preventing crashes while preserving the focus behavior.

@flutter-website-bot
Copy link
Copy Markdown
Collaborator

Visit the preview URL for this PR (updated for commit 54a3f36):

https://flutter-docs-prod--pr13402-fix-guessinput-focus-lifecy-9htefr8i.web.app

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.

GuessInput class should be a StatelfulWidget

2 participants