Skip to content

Fix focus violation when canRequestFocus set to false for all siblings in same build - #191536

Merged
auto-submit[bot] merged 2 commits into
flutter:masterfrom
Istiak-Ahmed78:fix-focus-framework
Nov 19, 2026
Merged

auto-submit[bot] merged 2 commits into
flutter:masterfrom
Istiak-Ahmed78:fix-focus-framework

Conversation

@Istiak-Ahmed78

@Istiak-Ahmed78 Istiak-Ahmed78 commented Aug 22, 2026 •

Copy link
Copy Markdown
Contributor

This PR is a split of #185280. Holds only framework related changes. Fixes #185076, fixes an issue where a widget unexpectedly receives primary focus when canRequestFocus is set to false for all sibling widgets in the same build cycle.
This is reviewable in draft state.

Pre-launch Checklist

  • I read the Contributor Guide and followed the process outlined there for submitting PRs.
  • I read the AI contribution guidelines and understand my responsibilities, or I am not using AI tools.
  • I read the Tree Hygiene wiki page, which explains my responsibilities.
  • I read and followed the Flutter Style Guide, including [Features we expect every widget to implement].
  • I signed the CLA.
  • I listed at least one issue that this PR fixes in the description above.
  • I updated/added relevant documentation (doc comments with ///).
  • I added new tests to check the change I am making, or this PR is [test-exempt].
  • I followed the breaking change policy and added Data Driven Fixes where supported.
  • All existing and new tests are passing.

@github-actions github-actions Bot added framework flutter/packages/flutter repository. See also f: labels. f: focus Focus traversal, gaining or losing focus labels Aug 22, 2026
@dkwingsmt dkwingsmt added the CICD Run CI/CD label Aug 25, 2026
@Istiak-Ahmed78
Istiak-Ahmed78 marked this pull request as ready for review August 26, 2026 09:02

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

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.

Code Review

This pull request updates the Flutter focus system to ensure that only focusable nodes can hold primary focus, introducing fallback logic when a requested node becomes unfocusable, and changing findLastFocus to return a nullable FocusNode?. The review feedback suggests removing a redundant and potentially expensive scope.descendants.isNotEmpty check in _findValidFocusOrFallback, and notes that changing the return type of findLastFocus is a breaking change that requires compliance with the Flutter breaking change policy.

Comment on lines +2069 to +2076
if (scope.descendants.isNotEmpty) {
// Get all traversable descendants, and give the focus to the first one if found any
// [traversalDescendants] filters [canRequestFocus]
final Iterable<FocusNode> traversalDescendants = scope.traversalDescendants;
if (traversalDescendants.isNotEmpty) {
fallbackFocus = traversalDescendants.first;
}
}

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.

medium

The check scope.descendants.isNotEmpty is redundant and potentially expensive because descendants is a recursive getter that traverses the entire subtree. Since scope.traversalDescendants already performs its own traversal and filters for focusable nodes, we can directly check traversalDescendants.isNotEmpty and avoid the extra overhead.

    final Iterable<FocusNode> traversalDescendants = scope.traversalDescendants;
    if (traversalDescendants.isNotEmpty) {
      fallbackFocus = traversalDescendants.first;
    }
References
  1. Suggest simplification and refactoring: Assess whether the code can be made simpler or refactored to enhance readability and maintainability. (link)

/// * [DirectionalFocusTraversalPolicyMixin.findFirstFocusInDirection], a
/// function that finds the first focusable widget in a particular direction.
FocusNode findLastFocus(FocusNode currentNode, {bool ignoreCurrentFocus = false}) {
FocusNode? findLastFocus(FocusNode currentNode, {bool ignoreCurrentFocus = false}) {

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.

medium

Changing the return type of findLastFocus from FocusNode to FocusNode? is a breaking change. Any external callers or custom subclasses of FocusTraversalPolicy that expect a non-nullable FocusNode or override this method might fail to compile. While this change is highly desirable for symmetry with findFirstFocus (which already returns FocusNode?), please ensure that this change follows the Flutter breaking change policy and that any necessary design docs or announcements are prepared.

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.

+1, this is a breaking change, do we need this break to fix the issue?

@Istiak-Ahmed78 Istiak-Ahmed78 Aug 31, 2026 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

No, we don't need. The changes at packages/flutter/lib/src/widgets/focus_manager.dart fixes our issue. But changes at packages/flutter/lib/src/widgets/focus_traversal.dart are basically separate improvement.
Let me tell you. Imagine the example in the example at the issue
At the very first. When the focus has not moved to any node yet (fresh condition, No transversal history). When user presses TAB, we now need to look for the next node. the _findInitialFocusfrom focus_transversal.dar returns a valid focus candidate via first/last (current code you are checked in) . In that case I just tried to improve the

 candidate ??= currentNode;

to

   if (candidate == null && _canRequestTraversalFocus(currentNode)) {
      return currentNode;
    }

such that if it finds no other options to give to. Then as the last option we are checking whether we can give the focus to our current node or not. But current code can be no longer focusable. But this was not checked earlier. Now I added a filter here. (This is not happening in our issue)

However, long story short, this is just an improvement, not happening in our issue we are solving. We are encountering the problem when we have a non-empty traversal history of node navigation. Overall, we can safely go without the breaking changes completely. In that case packages/flutter/lib/src/widgets/focus_manager.dart & packages/flutter/test/widgets/focus_manager_test.dart are enough to address the issue. I ran the tests and also checked physically running the example code.

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.

Can you revert all files except those two then?

@flutter-dashboard flutter-dashboard Bot removed the CICD Run CI/CD label Aug 26, 2026

@victorsanni victorsanni left a comment

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.

@Istiak-Ahmed78

Istiak-Ahmed78 commented Sep 1, 2026 •

Copy link
Copy Markdown
Contributor Author

#191536 (comment)

Yes. I did. Dropped the changes which we really don't need to fix the issue. And updated the test which reproduces the exact issue. The tests pass with the fix and fail if the changes at packages/flutter/lib/src/widgets/focus_manager.dart withdrawn.
Now, we dont need to open a new PR to material and cupertino as well

@victorsanni
victorsanni self-requested a review September 1, 2026 17:28
@victorsanni victorsanni added the CICD Run CI/CD label Sep 1, 2026

@victorsanni victorsanni left a comment •

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.

Left some comments.

@flutter-dashboard flutter-dashboard Bot removed the CICD Run CI/CD label Sep 3, 2026
@dkwingsmt dkwingsmt added the CICD Run CI/CD label Sep 9, 2026

@victorsanni victorsanni left a comment

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.

Thanks for the changes, this is looking better. With more brainstorming there may even be a better solution. applyFocusChangesIfNeeded promotes _markedForFocus to primary focus without revalidating that it's still focusable. So if we can revalidate in applyFocusChangesIfNeeded we won't need to add this guard in these setters.

What do you think?

// unfocusable, previously-focused children.
_canRequestFocus = value;
if (hasFocus && !value) {
if (!value && (hasFocus || _manager?._markedForFocus == this)) {

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.

Are there other places with a similar guard that we can update? One I see is descendantsAreFocusable. Maybe we can wrap the guard with a getter or similar?

@Istiak-Ahmed78 Istiak-Ahmed78 Sep 24, 2026 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

As we shifted to applyFocusChangesIfNeeded fix from the setter validation. We no more need similar gaud in descendantsAreFocusable.

@flutter-dashboard flutter-dashboard Bot removed the CICD Run CI/CD label Sep 23, 2026
@Istiak-Ahmed78

Istiak-Ahmed78 commented Sep 24, 2026 •

Copy link
Copy Markdown
Contributor Author

I apologize for being late.

Thanks for the changes, this is looking better. With more brainstorming there may even be a better solution. applyFocusChangesIfNeeded promotes _markedForFocus to primary focus without revalidating that it's still focusable. So if we can revalidate in applyFocusChangesIfNeeded we won't need to add this guard in these setters.

What do you think?

However, you are right. Now we are inspecting the _markedForFocus, whether that is really any more focusable or not. In case, we observe that the _markedForFocus is unfocuseable, we are providing our next focus to _primaryFocus or rootScope based on _primaryFocus focusability.

@victorsanni victorsanni added the CICD Run CI/CD label Sep 25, 2026
victorsanni
victorsanni previously approved these changes Sep 25, 2026

@victorsanni victorsanni left a comment

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.

Approach LGTM, left some nits.

victorsanni
victorsanni previously approved these changes Sep 28, 2026

@victorsanni victorsanni left a comment

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.

Hi, can you fix the linux analyze failure by running dart format packages/flutter/test/widgets/focus_manager_test.dart?

@flutter-dashboard flutter-dashboard Bot removed the CICD Run CI/CD label Oct 20, 2026
@victorsanni victorsanni added the CICD Run CI/CD label Oct 20, 2026
@flutter-dashboard flutter-dashboard Bot removed the CICD Run CI/CD label Oct 29, 2026
@victorsanni victorsanni added the CICD Run CI/CD label Oct 29, 2026
@Istiak-Ahmed78

Copy link
Copy Markdown
Contributor Author

Hi @victorsanni , what's our next plan?

@flutter-dashboard flutter-dashboard Bot removed the CICD Run CI/CD label Nov 10, 2026
@victorsanni

Copy link
Copy Markdown
Contributor

Hi @Istiak-Ahmed78, This is still waiting for review from Justin, who is working through his review queue gradually. Thanks for the patience!

@justinmc justinmc added the CICD Run CI/CD label Nov 13, 2026

@justinmc justinmc left a comment

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.

LGTM as long as CI passes. Sorry for the delay here @Istiak-Ahmed78.

It looks like Google tests have a single failure (A Timer is still pending even after the widget tree was disposed.). Hopefully it's unrelated and not a slight timing change with this PR. Can you rebase?

…s in same build

Revalidate the node marked for focus before applying focus changes so a node that becomes unfocusable in the same build does not retain primary focus.

Fixes flutter#185076
@flutter-dashboard flutter-dashboard Bot removed the CICD Run CI/CD label Nov 17, 2026
@Istiak-Ahmed78

Copy link
Copy Markdown
Contributor Author

LGTM as long as CI passes. Sorry for the delay here @Istiak-Ahmed78.

It looks like Google tests have a single failure (A Timer is still pending even after the widget tree was disposed.). Hopefully it's unrelated and not a slight timing change with this PR. Can you rebase?

@justinmc , I rebased. But this PR needs a CICD label to check the tests again

@victorsanni

Copy link
Copy Markdown
Contributor

The Google testing failure is legit, it will need a Google fix to land first and then rerun. Working on this, thanks for the patience.

@victorsanni victorsanni added the CICD Run CI/CD label Nov 18, 2026
@Istiak-Ahmed78

Istiak-Ahmed78 commented Nov 18, 2026 •

Copy link
Copy Markdown
Contributor Author

@victorsanni , can you please why were we encountering that test failure? I think, that was not related to our work

@victorsanni victorsanni added the autosubmit Merge PR when tree becomes green via auto submit App label Nov 18, 2026
@auto-submit
auto-submit Bot added this pull request to the merge queue Nov 19, 2026
Merged via the queue into flutter:master with commit fc019cb Nov 19, 2026
24 checks passed
@flutter-dashboard flutter-dashboard Bot removed the autosubmit Merge PR when tree becomes green via auto submit App label Nov 19, 2026
auto-submit Bot pushed a commit to flutter/packages that referenced this pull request Nov 19, 2026
flutter/flutter@06f32f8...e8cc6f3

2026-11-19 [email protected] Roll Skia from 280e0965687d to 3dfb69d709c7 (1 revision) (flutter/flutter#196970)
2026-11-19 [email protected] Update outdated `_formattedValues` documentation (flutter/flutter#195765)
2026-11-19 [email protected] [Impeller] Fix Vulkan GPU frame time metrics in benchmarks (flutter/flutter#196339)
2026-11-19 [email protected] Windows: Enable D3D11 multithread protection on ANGLE's device (flutter/flutter#196897)
2026-11-19 [email protected] Roll Skia from 7356c7627e87 to 280e0965687d (1 revision) (flutter/flutter#196957)
2026-11-19 [email protected] Roll Dart SDK from 091f346ad95c to d8d40cf9c21b (2 revisions) (flutter/flutter#196949)
2026-11-19 [email protected] Roll Skia from 73b9219fd1fa to 7356c7627e87 (4 revisions) (flutter/flutter#196951)
2026-11-19 [email protected] Roll Skia from 7bca23228537 to 73b9219fd1fa (1 revision) (flutter/flutter#196948)
2026-11-19 [email protected] [libimpeller] Fix swapped x and y glyph coordinates (flutter/flutter#196552)
2026-11-19 [email protected] [ios][spm]When using cocoapods plugin, give instruction that plugins may just need update (flutter/flutter#195853)
2026-11-19 [email protected] [web] Let the browser handle Control+wheel zoom outside of macOS (flutter/flutter#193362)
2026-11-19 [email protected] Fix focus violation when canRequestFocus set to false for all siblings in same build (flutter/flutter#191536)
2026-11-19 [email protected] Roll Skia from 9fc4f9e23d56 to 7bca23228537 (10 revisions) (flutter/flutter#196941)
2026-11-19 [email protected] Roll Dart SDK from 753488ede427 to 091f346ad95c (2 revisions) (flutter/flutter#196940)
2026-11-19 [email protected] iOS: Convert platform views to points with the frame's pixel ratio (flutter/flutter#196858)
2026-11-19 [email protected] [engine] Defer default font manager resolution to first use for platforms that have a prefetching API only (flutter/flutter#196377)
2026-11-18 [email protected] [flutter_tools] Add example app to package template and decouple platform configurations (flutter/flutter#192441)
2026-11-18 [email protected] [flutter_tools] Rebuild asset bundle when generated manifest content changes (flutter/flutter#191638)
2026-11-18 [email protected] Roll Skia from b11d69796f71 to 9fc4f9e23d56 (2 revisions) (flutter/flutter#196909)
2026-11-18 [email protected] [flutter_tools] Bump cupertino_icons to ^2.0.0 in app and module templates (flutter/flutter#196828)
2026-11-18 [email protected] Update iOS plugin language instructions (flutter/flutter#196614)
2026-11-18 [email protected] iOS: Add tests for rounded rect clips with per-corner/elliptical radii (flutter/flutter#196854)
2026-11-18 [email protected] Revert: [flutter_tools] Migrate lib/runner.dart off runInContext and overrides (flutter/flutter#196926)
2026-11-18 [email protected] Revert: Move macOS desktop builds to ThinLTO (flutter/flutter#196917)
2026-11-18 [email protected] [FGP] Offset split-APK versionCodes from a manifest-only versionCode (flutter/flutter#196561)
2026-11-18 [email protected] [flutter_tools] Migrate lib/runner.dart off runInContext and overrides (flutter/flutter#196387)
2026-11-18 [email protected] docs: remove broken image references from dev/snippets/README.md (flutter/flutter#195587)
2026-11-18 [email protected] Remove the FGP AGP-public-API migration coordination docs (flutter/flutter#196733)
2026-11-18 [email protected] [web] Skip stale safeBlur focus transfer when the element was refocused (flutter/flutter#196550)
2026-11-18 [email protected] Roll Skia from 81ee7327e81c to b11d69796f71 (4 revisions) (flutter/flutter#196900)

If this roll has caused a breakage, revert this CL and set the roller
to dry run mode using the controls here:
https://autoroll.skia.org/r/flutter-packages
Please CC [email protected],[email protected] on the revert to ensure that a human
is aware of the problem.

To file a bug in Packages: https://github.com/flutter/flutter/issues/new/choose

To report a problem with the AutoRoller itself, please file a bug:
https://issues.skia.org/issues/new?component=1389291&template=1850622

Documentation for the AutoRoller is here:
https://skia.googlesource.com/buildbot/+doc/main/autoroll/README.md
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

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

Projects

Development

Successfully merging this pull request may close these issues.

Focus.canRequestFocus=false doesn't forbid getting primary focus when it's set in the same build for all the siblings

4 participants