Repository navigation
Closed
Labels
P1High-priority issues at the top of the work listf: focusFocus traversal, gaining or losing focusframeworkflutter/packages/flutter repository. See also f: labels.has reproducible stepsThe issue has been confirmed reproducible and is ready to work onteam-frameworkOwned by Framework teamtriaged-frameworkTriaged by Framework teamwaiting for PR to land (fixed)A fix is in flight
Description
Steps to reproduce
- Run code snippet on web
- Focus on any element with tab traversal (Tab key)
- Focus on previous element with tab traversal (shift + Tab), it's easiest to reproduce that way, but sometimes even not necessary
- Click on "Toggle can request focus" button to disable "Focus.canRequestFocus" for all the children.
- Observe that focus is moved to another focus node, which should not be possible, because that node has "Focus.canRequestFocus == false"
Expected results
Focus widgets with canRequestFocus==false can not get focus, the focus is lost, all of Focus widgets with canRequestFocus==false do not have primaryFocus.
Actual results
a random Focus with canRequestFocus==false gets primary focus.
Code sample
import 'package:flutter/material.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatefulWidget {
const MyApp({super.key});
@override
State<MyApp> createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
bool _canRequestFocus = true;
@override
Widget build(BuildContext context) => MaterialApp(
home: Scaffold(
body: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
ElevatedButton(
onPressed: () {
setState(() {
_canRequestFocus = !_canRequestFocus;
});
},
child: Text('Toggle can request focus'),
),
Column(
children: List.generate(
10,
(i) => _FocusableChild(canRequestFocus: _canRequestFocus),
),
),
],
),
),
);
}
class _FocusableChild extends StatelessWidget {
const _FocusableChild({required this.canRequestFocus});
final bool canRequestFocus;
@override
Widget build(BuildContext context) => Focus(
canRequestFocus: canRequestFocus,
child: Builder(builder: (context) {
final focus = Focus.of(context);
return Container(
decoration: BoxDecoration(
border:
focus.hasPrimaryFocus ? Border.all(color: Colors.blue) : null,
),
child: Text(' canRequestFocus:${focus.canRequestFocus} '),
);
}),
);
}Screenshots or Video
output.mp4
Logs
No response
Flutter Doctor output
Doctor output
rafalbednarczuk@RBEDNARCZUK-MAC lightning_core_ui % flutter doctor -v
[!] Flutter (Channel [user-branch], 3.35.7, on macOS 15.6.1 24G90 darwin-arm64, locale en-US) [750ms]
! Flutter version 3.35.7 on channel [user-branch] at /Users/rafalbednarczuk/flutter
Currently on an unknown channel. Run `flutter channel` to switch to an official channel.
If that doesn't fix the issue, reinstall Flutter by following instructions at https://flutter.dev/setup.
! Upstream repository unknown source is not a standard remote.
Set environment variable "FLUTTER_GIT_URL" to unknown source to dismiss this error.
• Framework revision adc9010625 (6 months ago), 2025-10-21 14:16:03 -0400
• Engine revision 035316565a
• Dart version 3.9.2
• DevTools version 2.48.0
• Feature flags: enable-web, enable-linux-desktop, enable-macos-desktop, enable-windows-desktop, enable-android, enable-ios, cli-animations, enable-native-assets, enable-lldb-debugging
• If those were intentional, you can disregard the above warnings; however it is recommended to use "git" directly to perform update checks and upgrades.
[✓] Android toolchain - develop for Android devices (Android SDK version 35.0.1) [3.5s]
• Android SDK at /Users/rafalbednarczuk/Library/Android/sdk
• Emulator version 36.3.10.0 (build_id 14472402) (CL:N/A)
• Platform android-36, build-tools 35.0.1
• Java binary at: /Applications/Android Studio.app/Contents/jbr/Contents/Home/bin/java
This is the JDK bundled with the latest Android Studio installation on this machine.
To manually set the JDK path, use: `flutter config --jdk-dir="path/to/jdk"`.
• Java version OpenJDK Runtime Environment (build 21.0.8+-14196175-b1038.72)
• All Android licenses accepted.
[✓] Xcode - develop for iOS and macOS (Xcode 16.2) [1,657ms]
• Xcode at /Applications/Xcode.app/Contents/Developer
• Build 16C5032a
• CocoaPods version 1.16.2
[✓] Chrome - develop for the web [8ms]
• Chrome at /Applications/Google Chrome.app/Contents/MacOS/Google Chrome
[✓] Android Studio (version 2025.2) [8ms]
• Android Studio at /Applications/Android Studio.app/Contents
• Flutter plugin can be installed from:
🔨 https://plugins.jetbrains.com/plugin/9212-flutter
• Dart plugin can be installed from:
🔨 https://plugins.jetbrains.com/plugin/6351-dart
• Java version OpenJDK Runtime Environment (build 21.0.8+-14196175-b1038.72)
[✓] VS Code (version 1.108.2) [7ms]
• VS Code at /Users/rafalbednarczuk/Downloads/Visual Studio Code.app/Contents
• Flutter extension can be installed from:
🔨 https://marketplace.visualstudio.com/items?itemName=Dart-Code.flutter
[✓] Connected device (2 available) [6.2s]
• macOS (desktop) • macos • darwin-arm64 • macOS 15.6.1 24G90 darwin-arm64
• Chrome (web) • chrome • web-javascript • Google Chrome 147.0.7727.56
[✓] Network resources [749ms]
• All expected network resources are available.
! Doctor found issues in 1 category.Metadata
Metadata
Assignees
Labels
P1High-priority issues at the top of the work listf: focusFocus traversal, gaining or losing focusframeworkflutter/packages/flutter repository. See also f: labels.has reproducible stepsThe issue has been confirmed reproducible and is ready to work onteam-frameworkOwned by Framework teamtriaged-frameworkTriaged by Framework teamwaiting for PR to land (fixed)A fix is in flight
Activity
mbcorona commentedon Apr 16, 2026
Verified the issue on Flutter 3.41.6 using Chrome.
Routing to the team framework for further investigation.
Istiak-Ahmed78 commentedon Apr 20, 2026
I managed to reproduce this on Web (Chrome). Here's what was happening - when you'd disable a focus node by setting
canRequestFocusto false, the setter would immediately callunfocus(). The problem was thatunfocus()would try to find another focusable node to give focus to. But if you were disabling multiple nodes at once in the same build cycle,unfocus()would find a node that hadn't been disabled yet (because its setter hadn't run) and give it focus. Then that node's setter would eventually run and disable it too - leaving you with a node that has focus butcanRequestFocus=false. Anyway, I've fixed it! The item is no longer being focused whencanRequestFocusis set to false. Check out the PR for the detailed implementation.justinmc commentedon Apr 21, 2026
@Istiak-Ahmed78 Thanks for the analysis and for jumping on writing a PR too!
victorsanni commentedon Apr 23, 2026
Hi @rafalbednarczuk and @Istiak-Ahmed78, is #183456 happening because of this issue?
Istiak-Ahmed78 commentedon Apr 24, 2026
I'm investigating the issue
Istiak-Ahmed78 commentedon Apr 24, 2026
Looks like you resolved the issue there #185543.
No, that is not happening because of this issue we are talking about
3 remaining items
flutter-triage-bot commentedon Nov 12, 2026
This issue was assigned to @Istiak-Ahmed78 but has had no status updates in a long time. To remove any ambiguity about whether the issue is being worked on, the assignee was removed.
Fix focus violation when canRequestFocus set to false for all sibling…
Renzo-Olivares commentedon Nov 17, 2026
Thank you for the report @rafalbednarczuk! I assigned the issue to @Istiak-Ahmed78 since he has a PR #191536 out to fix it. If you feel like you do not want to work on this feel free to unassign.
Istiak-Ahmed78 commentedon Nov 18, 2026
Ok, no problem. I'm already working on it and doing the last finishings. Hope that very soon it be merged. Thanks
Fix focus violation when canRequestFocus set to false for all sibling…