Fix GuessInput focus persistence using StatefulWidget#13402
Fix GuessInput focus persistence using StatefulWidget#13402harshyadavDeveloper wants to merge 3 commits into
Conversation
There was a problem hiding this comment.
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.
| void _onSubmit() { | ||
| onSubmitGuess(_textEditingController.text.trim()); | ||
| widget.onSubmitGuess(_textEditingController.text.trim()); | ||
| _textEditingController.clear(); | ||
| _focusNode.requestFocus(); | ||
| } |
There was a problem hiding this comment.
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();
}|
Thanks for catching this. I added a length check before submitting guesses so invalid input no longer reaches |
|
Visit the preview URL for this PR (updated for commit 54a3f36): https://flutter-docs-prod--pr13402-fix-guessinput-focus-lifecy-9htefr8i.web.app |
Description of what this PR is changing or adding, and why:
Converts
GuessInputfrom aStatelessWidgetto aStatefulWidgetto preserve theFocusNodeandTextEditingControlleracross rebuilds.This ensures
_focusNode.requestFocus()works as intended aftersubmitting input and demonstrates proper lifecycle management for
long-lived objects used by text fields.
The change also adds disposal for the
FocusNodeandTextEditingControllerto avoid resource leaks.Issues fixed by this PR (if any):
Fixes #13392
PRs or commits this PR depends on (if any):
None.
Presubmit checklist
of 80 characters or fewer.