Repository navigation
Fix focus violation when canRequestFocus set to false for all siblings in same build - #191536
Conversation
There was a problem hiding this comment.
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.
| 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; | ||
| } | ||
| } |
There was a problem hiding this comment.
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
- 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}) { |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
+1, this is a breaking change, do we need this break to fix the issue?
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Can you revert all files except those two then?
|
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 |
victorsanni
left a comment
There was a problem hiding this comment.
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)) { |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
As we shifted to applyFocusChangesIfNeeded fix from the setter validation. We no more need similar gaud in descendantsAreFocusable.
|
I apologize for being late.
However, you are right. Now we are inspecting the |
victorsanni
left a comment
There was a problem hiding this comment.
Approach LGTM, left some nits.
victorsanni
left a comment
There was a problem hiding this comment.
Hi, can you fix the linux analyze failure by running dart format packages/flutter/test/widgets/focus_manager_test.dart?
|
Hi @victorsanni , what's our next plan? |
|
Hi @Istiak-Ahmed78, This is still waiting for review from Justin, who is working through his review queue gradually. Thanks for the patience! |
justinmc
left a comment
There was a problem hiding this comment.
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
81f4269 to
dae80ea
Compare
@justinmc , I rebased. But this PR needs a CICD label to check the tests again |
|
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 , can you please why were we encountering that test failure? I think, that was not related to our work |
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
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
canRequestFocusis set tofalsefor all sibling widgets in the same build cycle.This is reviewable in draft state.
Pre-launch Checklist
///).