Skip to content

Whether focus exists has nothing to do with whether the keyboard is open #140508

Open

Description

@MeandNi

Steps to reproduce

Related issues:

#123523

Related Pull Requests:

flutter-team-archive/engine#41500
#123929

There should be no correlation between whether the keyboard is open and whether focus exists. Should keep focus after keyboard was closed.

related comments:
flutter-team-archive/engine#41500 (comment)
flutter-team-archive/engine#40746 (comment)

Expected results

Keep focus when keyboard was closed.

before.fix.mp4

Actual results

the focus is gone after the keyboard is closed

after.fix.mp4

Code sample

Code sample
import 'package:flutter/material.dart';

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

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: const MyHomePage(title: 'Flutter Demo Home Page'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  const MyHomePage({super.key, required this.title});

  final String title;

  @override
  State<MyHomePage> createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: const <Widget>[TextField()],
        ),
      ),
    );
  }
}

Flutter Doctor output

Doctor
[!] Flutter (Channel [user-branch], 3.18.0-14.0.pre.46, on macOS 14.2.1 23C71
    darwin-arm64, locale zh-Hans-CN)
    ! Flutter version 3.18.0-14.0.pre.46 on channel [user-branch] at
      /Users/yangjiakang/flutter/framework
      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/docs/get-started/install.
    ! Upstream repository unknown source is not a standard remote.
      Set environment variable "FLUTTER_GIT_URL" to unknown source to dismiss
      this error.
    • Framework revision d746007fcd (12 小时前), 2023-12-20 16:19:59 -0800
    • Engine revision c70f0a495a
    • Dart version 3.3.0 (build 3.3.0-244.0.dev)
    • DevTools version 2.31.0-dev.0
    • 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 34.0.0)
    • Android SDK at /Users/yangjiakang/Library/Android/sdk
    • Platform android-34, build-tools 34.0.0
    • ANDROID_HOME = /Users/yangjiakang/Library/Android/sdk
    • Java binary at: /Applications/Android
      Studio.app/Contents/jbr/Contents/Home/bin/java
    • Java version OpenJDK Runtime Environment (build
      17.0.6+0-17.0.6b829.9-10027231)
    • All Android licenses accepted.

[!] Xcode - develop for iOS and macOS (Xcode 15.1)
    • Xcode at /Applications/Xcode.app/Contents/Developer
    • Build 15C65
    ! CocoaPods 1.11.3 out of date (1.13.0 is recommended).
        CocoaPods is used to retrieve the iOS and macOS platform side's plugin
        code that responds to your plugin usage on the Dart side.
        Without CocoaPods, plugins will not work on iOS or macOS.
        For more info, see https://flutter.dev/platform-plugins
      To upgrade see
      https://guides.cocoapods.org/using/getting-started.html#updating-cocoapods
      for instructions.

[✓] Chrome - develop for the web
    • Chrome at /Applications/Google Chrome.app/Contents/MacOS/Google Chrome

[✓] Android Studio (version 2022.3)
    • 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
      17.0.6+0-17.0.6b829.9-10027231)

[!] Android Studio (version unknown)
    • Android Studio at
      /Users/yangjiakang/Code/androidx/studio/android-studio-2023.1.1.15-mac_arm
      /Android Studio Preview.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
    ✗ Unable to determine Android Studio version.
    • Java version OpenJDK Runtime Environment (build
      17.0.7+0-17.0.7b1000.6-10550314)

[!] Android Studio (version unknown)
    • Android Studio at
      /Users/yangjiakang/Code/aosp/frameworks/support/studio/android-studio-2023
      .2.1.4-mac_arm/Android Studio Preview.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
    ✗ Unable to determine Android Studio version.
    • Java version OpenJDK Runtime Environment (build
      17.0.8+0-17.0.8b1000.22-10799086)

[✓] IntelliJ IDEA Ultimate Edition (version 2022.3.2)
    • IntelliJ at /Applications/IntelliJ IDEA.app
    • 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

[✓] VS Code (version 1.85.1)
    • VS Code at /Applications/Visual Studio Code.app/Contents
    • Flutter extension version 3.78.0

[✓] Network resources
    • All expected network resources are available.

! Doctor found issues in 4 categories.

Activity

  1. changed the title [-]Keyboard behavior is inconsistent with system default[/-] [+]Whether focus exists has nothing to do with whether the keyboard is open[/+] on Dec 21, 2023
  2. OPTRONIX-10 commented on Dec 21, 2023

    @OPTRONIX-10

    can you please be more clear that weather you want focus after closing keyboard or not?

  3. CoderBuck commented on Dec 21, 2023

    @CoderBuck

    Indeed, in native Android applications, the focus remains unaffected when the keyboard is dismissed. Flutter should ideally exhibit similar behavior, or at least it used to behave that way in the past.

    To be honest, this seems to be a breaking change. When our app upgraded to Flutter 3.16, this change led to some bugs. In order to revert to the behavior prior to the upgrade, we had to hook into the TextInputChannel.

    class MyTextInputChannel extends TextInputChannel {
    
        public MyTextInputChannel(@NonNull DartExecutor dartExecutor) {
            super(dartExecutor);
        }
    
        /**
         * 修复键盘收起后,Flutter TextField 焦点消失的问题
         * 引入问题的PR: https://github.com/flutter/engine/pull/40746
         */
        @Override
        public void onConnectionClosed(int inputClientId) {
            // super.onConnectionClosed(inputClientId);
        }
    }
  4. MeandNi commented on Dec 21, 2023

    @MeandNi
    ContributorAuthor

    can you please be more clear that weather you want focus after closing keyboard or not?↳

    I want focus when keyboard was closed.

  5. DBOutlink commented on Dec 21, 2023

    @DBOutlink

    Indeed, in native Android applications, the focus remains unaffected when the keyboard is dismissed. Flutter should ideally exhibit similar behavior, or at least it used to behave that way in the past.

    To be honest, this seems to be a breaking change. When our app upgraded to Flutter 3.16, this change led to some bugs. In order to revert to the behavior prior to the upgrade, we had to hook into the TextInputChannel.

    class MyTextInputChannel extends TextInputChannel {
    
        public MyTextInputChannel(@NonNull DartExecutor dartExecutor) {
            super(dartExecutor);
        }
    
        /**
         * 修复键盘收起后,Flutter TextField 焦点消失的问题
         * 引入问题的PR: https://github.com/flutter/engine/pull/40746
         */
        @Override
        public void onConnectionClosed(int inputClientId) {
            // super.onConnectionClosed(inputClientId);
        }
    }

    Could you show me how to use this extension on your android code? I have the same issue...

  6. CoderBuck commented on Dec 22, 2023

    @CoderBuck

    @DBOutlink

    // FlutterMainActivity
        @Override
        public void configureFlutterEngine(@NonNull FlutterEngine flutterEngine) {
            TextInputChannelHook.hook(flutterEngine);
        }
    public class TextInputChannelHook {
        public static void hook(FlutterEngine engine) {
            try {
                Field textInputChannel = engine.getClass().getDeclaredField("textInputChannel");
                textInputChannel.setAccessible(true);
                textInputChannel.set(engine, new MyTextInputChannel(engine.getDartExecutor()));
            } catch (Exception e) {
                //...
            }
        }
    }
  7. dam-ease commented on Dec 22, 2023

    @dam-ease

    Thanks for filing this @MeandNi.
    I can reproduce this on the latest stable and master channel.
    I see this seems to be a known issue and is similar/related to #31985, can you please check this out?

  8. added
    waiting for responseThe Flutter team cannot make further progress on this issue until the original reporter responds
    on Dec 22, 2023
  9. MeandNi commented on Dec 22, 2023

    @MeandNi
    ContributorAuthor

    Thanks for filing this @MeandNi. I can reproduce this on the latest stable and master channel. I see this seems to be a known issue and is similar/related to #31985, can you please check this out?↳

    @dam-ease

    From what I have observed, flutter-team-archive/engine#41500, #123929 These two PRs have met my needs after rolling back. I hope that the focus can be retained after I close the keyboard. #31985 is probably a more ideal situation.
    ​

  10. removed
    waiting for responseThe Flutter team cannot make further progress on this issue until the original reporter responds
    on Dec 22, 2023
  11. added
    a: text inputEntering text in a text field or keyboard related problems
    frameworkflutter/packages/flutter repository. See also f: labels.
    engineflutter/engine related. See also e: labels.
    and removed
    in triagePresently being triaged by the triage team
    on Dec 27, 2023
  12. 4 remaining items

  13. added
    P2Important issues not at the top of the work list
    fyi-androidFor the attention of Android platform team
    on Jan 2, 2024
  14. MeandNi commented on Feb 4, 2024

    @MeandNi
    ContributorAuthor

    flutter-team-archive/engine#49366 has been merged. Shouldn’t iOS also have this default behavior? #123523 @camsim99 @mossmana

  15. auradigitaldev commented on Jul 16, 2024

    @auradigitaldev

    Question - am I missing something or does this make it impossible to make the keyboard reappear? I had a page that relied on the fact that dismissing the keyboard also removed focus, and I would call "requestFocus()" to show the keyboard. Now it doesn't seem possible to programmatically show the keyboard, though I'd love to be proved wrong with a workaround!

    In other words, is it possible to replicate the previous functionality where the keyboard dismissal is linked to focus? It's really important for one of the flows in my app.

  16. bleroux commented on Jul 16, 2024

    @bleroux
    Contributor

    You can make the keyboard reappear using:

    SystemChannels.textInput.invokeMethod("TextInput.show");
  17. auradigitaldev commented on Jul 17, 2024

    @auradigitaldev

    Ohhh perfect, I was missing that - thanks so much!

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 lista: text inputEntering text in a text field or keyboard related problemsengineflutter/engine related. See also e: labels.f: focusFocus traversal, gaining or losing focusfound in release: 3.16Found to occur in 3.16found in release: 3.18Found to occur in 3.18frameworkflutter/packages/flutter repository. See also f: labels.has reproducible stepsThe issue has been confirmed reproducible and is ready to work onplatform-androidAndroid applications specificallyteam-frameworkOwned by Framework teamtriaged-frameworkTriaged by Framework team

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions