Skip to content

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

Closed

Description

@rafalbednarczuk

Steps to reproduce

  1. Run code snippet on web
  2. Focus on any element with tab traversal (Tab key)
  3. Focus on previous element with tab traversal (shift + Tab), it's easiest to reproduce that way, but sometimes even not necessary
  4. Click on "Toggle can request focus" button to disable "Focus.canRequestFocus" for all the children.
  5. 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.

Activity

  1. added
    in triage
    framework
    has reproducible steps
    team-framework
    on Apr 15, 2026
  2. mbcorona commented on Apr 16, 2026

    @mbcorona
    Member

    Verified the issue on Flutter 3.41.6 using Chrome.
    Routing to the team framework for further investigation.

  3. removed
    in triage
    on Apr 16, 2026
  4. Istiak-Ahmed78 commented on Apr 20, 2026

    @Istiak-Ahmed78
    Contributor

    I managed to reproduce this on Web (Chrome). Here's what was happening - when you'd disable a focus node by setting canRequestFocus to false, the setter would immediately call unfocus(). The problem was that unfocus() 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 but canRequestFocus=false. Anyway, I've fixed it! The item is no longer being focused when canRequestFocus is set to false. Check out the PR for the detailed implementation.

  5. added
    P2
    triaged-framework
    on Apr 21, 2026
  6. justinmc commented on Apr 21, 2026

    @justinmc
    Contributor

    @Istiak-Ahmed78 Thanks for the analysis and for jumping on writing a PR too!

  7. victorsanni commented on Apr 23, 2026

    @victorsanni
    Contributor

    Hi @rafalbednarczuk and @Istiak-Ahmed78, is #183456 happening because of this issue?

  8. Istiak-Ahmed78 commented on Apr 24, 2026

    @Istiak-Ahmed78
    Contributor

    Hi @rafalbednarczuk and @Istiak-Ahmed78, is #183456 happening because of this issue?

    I'm investigating the issue

  9. Istiak-Ahmed78 commented on Apr 24, 2026

    @Istiak-Ahmed78
    Contributor

    Hi @rafalbednarczuk and @Istiak-Ahmed78, is #183456 happening because of this issue?

    Looks like you resolved the issue there #185543.
    No, that is not happening because of this issue we are talking about

  10. 3 remaining items

  11. removed
    triaged-framework
    on Nov 12, 2026
  12. flutter-triage-bot commented on Nov 12, 2026

    @flutter-triage-bot

    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.

  13. removed
    ⏳
    on Nov 12, 2026
  14. added a commit that references this issue on Nov 17, 2026
    dae80ea
  15. added
    P1
    triaged-framework
    f: focus
    and removed
    P2
    on Nov 17, 2026
  16. Renzo-Olivares commented on Nov 17, 2026

    @Renzo-Olivares
    Contributor

    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.

  17. Istiak-Ahmed78 commented on Nov 18, 2026

    @Istiak-Ahmed78
    Contributor

    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.

    Ok, no problem. I'm already working on it and doing the last finishings. Hope that very soon it be merged. Thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

P1f: focusframeworkhas reproducible stepsteam-frameworktriaged-frameworkwaiting for PR to land (fixed)

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions