Skip to content

Focus traversal is unintuitive after requestFocus #85941

Closed

Description

@MarcusWichelmann

When focusing a widget using requestFocus, the focus traversal with the arrow keys is unintuitive.

Steps to Reproduce

import 'package:flutter/material.dart';
import 'package:flutter/services.dart';

void main() {
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Focus Traversal Bug',
      home: Shortcuts(
        shortcuts: {
          // Focus traversal using keyboard is not enabled by default on the web
          LogicalKeySet(LogicalKeyboardKey.arrowLeft):
              const DirectionalFocusIntent(TraversalDirection.left),
          LogicalKeySet(LogicalKeyboardKey.arrowRight):
              const DirectionalFocusIntent(TraversalDirection.right),
          LogicalKeySet(LogicalKeyboardKey.arrowDown):
              const DirectionalFocusIntent(TraversalDirection.down),
          LogicalKeySet(LogicalKeyboardKey.arrowUp):
              const DirectionalFocusIntent(TraversalDirection.up),
        },
        child: DemoPage(),
      ),
    );
  }
}

class DemoPage extends StatelessWidget {
  final _fifthFocusNode = FocusNode();

  DemoPage({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('Focus Traversal Bug Demo'),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            for (var i = 0; i < 10; i++)
              Padding(
                padding: const EdgeInsets.all(5.0),
                child: ElevatedButton(
                  focusNode: i == 4 ? _fifthFocusNode : null,
                  autofocus: i == 4,
                  onPressed: () => _fifthFocusNode.requestFocus(),
                  child: Text('Button ${i+1}'),
                ),
              ),
          ],
        ),
      ),
    );
  }
}
  1. Run above code using Flutter Web (Also reproducible on mobile emulator or with keyboard connected)
  2. See that Button 5 is focused
  3. Press 3x the arrow down key --> Now Button 8 is focused
  4. Press enter --> The focus jumps to Button 5 again
  5. Press the arrow up key

Expected results: When going up from Button 5, Button 4 should get focused

Actual results: Button 7 gets focused

screencast-2021-07-06_08.36.51.mp4
Flutter Doctor
[✓] Flutter (Channel master, 2.3.0-17.0.pre.414, on Fedora 33 (Workstation Edition) 5.12.11-200.fc33.x86_64, locale de_DE.UTF-8)
    • Flutter version 2.3.0-17.0.pre.414 at /home/marcus/flutter
    • Upstream repository https://github.com/flutter/flutter.git
    • Framework revision 757c3add14 (vor 2 Wochen), 2021-06-20 22:29:02 -0400
    • Engine revision 9520bb15b3
    • Dart version 2.14.0 (build 2.14.0-228.0.dev)

[✓] Android toolchain - develop for Android devices (Android SDK version 30.0.3)
    • Android SDK at /home/marcus/Android/Sdk
    • Platform android-30, build-tools 30.0.3
    • Java binary at: /home/marcus/android-studio/jre/bin/java
    • Java version OpenJDK Runtime Environment (build 1.8.0_242-release-1644-b3-6222593)
    • All Android licenses accepted.

[✓] Chrome - develop for the web
    • Chrome at google-chrome

[✓] Android Studio (version 4.1)
    • Android Studio at /home/marcus/android-studio
    • Flutter plugin version 54.1.1
    • Dart plugin version 201.9335
    • Java version OpenJDK Runtime Environment (build 1.8.0_242-release-1644-b3-6222593)

[✓] VS Code (version 1.57.1)
    • VS Code at /usr/share/code
    • Flutter extension can be installed from:
      🔨 https://marketplace.visualstudio.com/items?itemName=Dart-Code.flutter

[✓] Connected device (1 available)
    • Chrome (web) • chrome • web-javascript • Google Chrome 91.0.4472.114

• No issues found!

Activity

  1. darshankawar commented on Jul 6, 2021

    @darshankawar
    Member

    I see the same behavior on latest stable.

    flutter doctor -v
    [✓] Flutter (Channel stable, 2.2.3, on Mac OS X 10.15.4 19E2269 darwin-x64,
        locale en-GB)
        • Flutter version 2.2.3 at /Users/dhs/documents/fluttersdk/flutter
        • Framework revision f4abaa0735 (4 days ago), 2021-07-01 12:46:11 -0700
        • Engine revision 241c87ad80
        • Dart version 2.13.4
    
    [✓] Xcode - develop for iOS and macOS
        • Xcode at /Applications/Xcode.app/Contents/Developer
        • Xcode 12.3, Build version 12C33
        • CocoaPods version 1.10.1
    
    [✓] Chrome - develop for the web
        • Chrome at /Applications/Google Chrome.app/Contents/MacOS/Google Chrome
    
    [✓] VS Code (version 1.57.1)
        • VS Code at /Applications/Visual Studio Code.app/Contents
        • Flutter extension version 3.21.0
    
    [✓] Connected device (3 available)
        • iPhone 12 Pro Max (mobile) • A5473606-0213-4FD8-BA16-553433949729 • ios
          • com.apple.CoreSimulator.SimRuntime.iOS-14-3 (simulator)
        • macOS (desktop)            • macos                                •
          darwin-x64     • Mac OS X 10.15.4 19E2269 darwin-x64
        • Chrome (web)               • chrome                               •
          web-javascript • Google Chrome 91.0.4472.114
    
    • No issues found!
    
    
    
    
    
    
  2. added
    p: material_uimaterial_ui package in flutter/packages
    frameworkflutter/packages/flutter repository. See also f: labels.
    has reproducible stepsThe issue has been confirmed reproducible and is ready to work on
    platform-webWeb applications specifically
    and removed
    in triagePresently being triaged by the triage team
    on Jul 6, 2021
  3. yjbanov commented on Jul 8, 2021

    @yjbanov
    Contributor
  4. yjbanov commented on Jul 8, 2021

    @yjbanov
    Contributor

    Does this only happen on the web?

  5. added
    waiting for responseThe Flutter team cannot make further progress on this issue until the original reporter responds
    on Jul 8, 2021
  6. gspencergoog commented on Jul 8, 2021

    @gspencergoog
    Contributor

    It appears to only happen on web, and (for me) only in Dartpad. I tried it on Linux and in Chrome (master branch). I could only get it to happen in Dartpad: I couldn't reproduce it in Chrome.

    In Dartpad at least, it seems to get worse each time the app is re-run: the first run it skips every other one each time the arrow or tab is pressed, and it skips two the next time the app is re-run, and so on.

  7. MarcusWichelmann commented on Jul 9, 2021

    @MarcusWichelmann
    Author

    @yjbanov No, this is reproducible on an Android Emulator, too, as well as on real devices, like an Android TV where navigation happens using the d-pad of the remote.

    In Dartpad at least, it seems to get worse each time the app is re-run: the first run it skips every other one each time the arrow or tab is pressed, and it skips two the next time the app is re-run, and so on.

    lol 😄 This looks like another issue. I have never seen this behaviour on any of my testing devices or emulators.

  8. removed
    waiting for responseThe Flutter team cannot make further progress on this issue until the original reporter responds
    on Jul 9, 2021
  9. gspencergoog commented on Jul 9, 2021

    @gspencergoog
    Contributor

    I can't reproduce this on the Android Emulator.

    flutter doctor -v
    [✓] Flutter (Channel master, 2.4.0-1.0.pre.164, on Debian GNU/Linux rodete 5.10.28-1rodete2-amd64, locale en_US.UTF-8)
        • Flutter version 2.4.0-1.0.pre.164 at /home/gspencer/code/flutter
        • Upstream repository [email protected]:gspencergoog/flutter.git
        • Framework revision cb17425df0 (86 minutes ago), 2021-07-09 17:06:08 -0400
        • Engine revision 0156ecd2e5
        • Dart version 2.14.0 (build 2.14.0-295.0.dev)
    

    [✓] Android toolchain - develop for Android devices (Android SDK version 30.0.1)
    • Android SDK at /home/gspencer/Android/Sdk
    • Platform android-30, build-tools 30.0.1
    • ANDROID_HOME = /home/gspencer/Android/Sdk
    • Java binary at: /opt/android-studio/jre/bin/java
    • Java version OpenJDK Runtime Environment (build 1.8.0_242-release-1644-b3-6222593)
    • All Android licenses accepted.

    [✓] Chrome - develop for the web
    • Chrome at google-chrome

    [✓] Linux toolchain - develop for Linux desktop
    • Debian clang version 11.0.1-2
    • cmake version 3.18.4
    • ninja version 1.8.2
    • pkg-config version 0.29.2

    [✓] VS Code (version 1.58.0)
    • VS Code at /usr/share/code
    • Flutter extension version 3.24.0

    [✓] Connected device (3 available)
    • Android SDK built for x86 64 (mobile) • emulator-5554 • android-x64 • Android 9 (API 28) (emulator)
    • Linux (desktop) • linux • linux-x64 • Debian GNU/Linux rodete 5.10.28-1rodete2-amd64
    • Chrome (web) • chrome • web-javascript • Google Chrome 91.0.4472.114

    • No issues found!

  10. 9 remaining items

  11. removed
    assigned for triageissue is assigned to a domain expert for further triage
    on Aug 5, 2021
  12. MarcusWichelmann commented on Oct 13, 2021

    @MarcusWichelmann
    Author

    Still reproducible with latest Flutter 2.5.2.

  13. flutter-triage-bot commented on Aug 7, 2023

    @flutter-triage-bot

    This issue is missing a priority label. Please set a priority label when adding the triaged-design label.

  14. added
    P2Important issues not at the top of the work list
    triaged-designTriaged by Design Languages team
    on Aug 9, 2023
  15. bpjhe commented on Apr 11, 2025

    @bpjhe

    Stumbled upon this today on version 3.27.4. Makes dpad navigation with a TV remote rather frustrating. Implementing scroll to top and focusing first element in a GridView, when clicking back on the remote, causes the same behavior as video in original post. Scrolling far enough down so that the widget you are coming from is disposed when jumping to the start does not trigger this problem.

  16. github-actions commented on Aug 5, 2026

    @github-actions

    This thread has been automatically locked since there has not been any recent activity after it was closed. If you are still experiencing a similar issue, please open a new bug, including the output of flutter doctor -v and a minimal reproduction of the issue.

  17. locked as resolved and limited conversation to collaborators on Aug 5, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    P2Important issues not at the top of the work listfound in release: 2.2Found to occur in 2.2frameworkflutter/packages/flutter repository. See also f: labels.has reproducible stepsThe issue has been confirmed reproducible and is ready to work onp: material_uimaterial_ui package in flutter/packagesteam-designOwned by Design Languages teamtriaged-designTriaged by Design Languages team

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions