Skip to content

Make sure that a Focus doesn't crash in 0x0 environment#180674

Merged
auto-submit[bot] merged 2 commits into
flutter:masterfrom
ahmedsameha1:handle#6537-Focus
Jan 21, 2026
Merged

Make sure that a Focus doesn't crash in 0x0 environment#180674
auto-submit[bot] merged 2 commits into
flutter:masterfrom
ahmedsameha1:handle#6537-Focus

Conversation

@ahmedsameha1
Copy link
Copy Markdown
Contributor

This is my attempt to handle #6537 for the Focus widget.

@github-actions github-actions Bot added framework flutter/packages/flutter repository. See also f: labels. f: focus Focus traversal, gaining or losing focus labels Jan 8, 2026
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 adds a test case to verify that the Focus widget does not crash in a zero-sized environment. The test is well-written and correctly reproduces the scenario from issue #6537. However, the pull request is missing the actual implementation of the fix. Without the fix, this new test will fail. Please include the necessary changes to packages/flutter/lib/src/widgets/focus_scope.dart to resolve the issue.

semantics.dispose();
});

testWidgets('Focus does not crash at zero area', (WidgetTester tester) async {
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.

critical

This test is a good regression test for the crash in a zero-sized environment. However, the pull request is incomplete as it's missing the corresponding fix in packages/flutter/lib/src/widgets/focus_scope.dart.

To fix the issue, you'll need to prevent the onFocus callback from being set when the widget has a zero size. A possible implementation in _FocusState.build could look like this:

// in packages/flutter/lib/src/widgets/focus_scope.dart, _FocusState.build method
final RenderBox? renderBox = context.findRenderObject() as RenderBox?;
final bool hasNonZeroSize = renderBox?.hasSize == true && renderBox!.size != Size.zero;
if (widget.includeSemantics) {
  child = Semantics(
    onFocus: defaultTargetPlatform != TargetPlatform.iOS && _couldRequestFocus && hasNonZeroSize
        ? focusNode.requestFocus
        : null,
    // ... other properties
  );
}

Please add the fix to this pull request so the new test can pass and the issue can be closed.

Copy link
Copy Markdown
Contributor

@victorsanni victorsanni left a comment

Choose a reason for hiding this comment

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

Does Focus have its own renderbox? I took a look at the source code for _FocusState, in its build method it just wraps the child in an InheritedNotifier + Semantics.

@ahmedsameha1
Copy link
Copy Markdown
Contributor Author

Does Focus have its own renderbox? I took a look at the source code for _FocusState, in its build method it just wraps the child in an InheritedNotifier + Semantics.

Should I close this PR?

Copy link
Copy Markdown
Contributor

@dkwingsmt dkwingsmt left a comment

Choose a reason for hiding this comment

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

LGTM. I think it doesn't hurt to add one.

@justinmc justinmc requested a review from victorsanni January 13, 2026 23:09
@justinmc justinmc added the autosubmit Merge PR when tree becomes green via auto submit App label Jan 20, 2026
@auto-submit auto-submit Bot removed the autosubmit Merge PR when tree becomes green via auto submit App label Jan 20, 2026
@auto-submit
Copy link
Copy Markdown
Contributor

auto-submit Bot commented Jan 20, 2026

autosubmit label was removed for flutter/flutter/180674, because The base commit of the PR is older than 7 days and can not be merged. Please merge the latest changes from the main into this branch and resubmit the PR.

@victorsanni victorsanni added the autosubmit Merge PR when tree becomes green via auto submit App label Jan 21, 2026
@auto-submit auto-submit Bot added this pull request to the merge queue Jan 21, 2026
Merged via the queue into flutter:master with commit be2f3bd Jan 21, 2026
71 checks passed
@flutter-dashboard flutter-dashboard Bot removed the autosubmit Merge PR when tree becomes green via auto submit App label Jan 21, 2026
flutter-zl pushed a commit to flutter-zl/flutter that referenced this pull request Feb 10, 2026
This is my attempt to handle
flutter#6537 for the Focus widget.

Co-authored-by: Victor Sanni <victorsanniay@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

f: focus Focus traversal, gaining or losing focus framework flutter/packages/flutter repository. See also f: labels.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants