flutter / flutter

Flutter makes it easy and fast to build beautiful apps for mobile and beyond
https://flutter.dev
BSD 3-Clause "New" or "Revised" License
162.55k stars 26.73k forks source link

Text field loses focus when changing Gboard language on Android #148117

Open Mr-Pepe opened 3 weeks ago

Mr-Pepe commented 3 weeks ago

Steps to reproduce

On a Pixel 8 with Gboard:

  1. Run the app with the provided sample code
  2. Change the keyboard input language

Expected results

The text field stays focused and the keyboard stays visible, so I can keep typing.

This is the behavior of all other apps on my phone.

Actual results

The text field loses focus and the keyboard disappears.

Code sample

Code sample ```dart import 'package:flutter/material.dart'; void main() { runApp(const MyApp()); } class MyApp extends StatelessWidget { const MyApp({super.key}); @override Widget build(BuildContext context) { return const MaterialApp( home: MyHomePage(), ); } } class MyHomePage extends StatefulWidget { const MyHomePage({super.key}); @override State createState() => _MyHomePageState(); } class _MyHomePageState extends State { @override Widget build(BuildContext context) { return Scaffold( appBar: AppBar( title: const Text("Keyboard focus"), ), body: const Center( child: TextField( autofocus: true, ), ), ); } } ```

Screenshots or Video

Screenshots / Video demonstration [Upload media here]

Logs

Logs ```console Restarted application in 1,419ms. I/ImeTracker( 9336): com.example.bug:9cc5ecb3: onRequestShow at ORIGIN_CLIENT_SHOW_SOFT_INPUT reason SHOW_SOFT_INPUT D/InputMethodManager( 9336): showSoftInput() view=io.flutter.embedding.android.FlutterView{779c5cb VFED..... .F...... 0,0-1080,2337 #1 aid=1073741824} flags=0 reason=SHOW_SOFT_INPUT I/ImeTracker( 9336): com.example.bug:6aff8d06: onRequestShow at ORIGIN_CLIENT_SHOW_SOFT_INPUT reason SHOW_SOFT_INPUT D/InputMethodManager( 9336): showSoftInput() view=io.flutter.embedding.android.FlutterView{779c5cb VFED..... .F...... 0,0-1080,2337 #1 aid=1073741824} flags=0 reason=SHOW_SOFT_INPUT I/AssistStructure( 9336): Flattened final assist data: 388 bytes, containing 1 windows, 3 views D/InputConnectionAdaptor( 9336): The input method toggled cursor monitoring on 2 D/InsetsController( 9336): show(ime(), fromIme=true) I/ImeTracker( 9336): com.example.bug:6aff8d06: onCancelled at PHASE_CLIENT_APPLY_ANIMATION I/ImeTracker( 9336): com.example.bug:9cc5ecb3: onShown W/WindowOnBackDispatcher( 9336): sendCancelIfRunning: isInProgress=falsecallback=ImeCallback=ImeOnBackInvokedCallback@21818024 Callback=android.window.IOnBackInvokedCallback$Stub$Proxy@3ac00d1 D/InputConnectionAdaptor( 9336): The input method toggled cursor monitoring off D/InputConnectionAdaptor( 9336): The input method toggled cursor monitoring on D/InputConnectionAdaptor( 9336): The input method toggled cursor monitoring off W/WindowOnBackDispatcher( 9336): sendCancelIfRunning: isInProgress=falsecallback=ImeCallback=ImeOnBackInvokedCallback@21818024 Callback=android.window.IOnBackInvokedCallback$Stub$Proxy@3ac00d1 I/ImeTracker( 9336): com.example.bug:7eab1e62: onRequestHide at ORIGIN_CLIENT_HIDE_SOFT_INPUT reason HIDE_SOFT_INPUT_BY_INSETS_API I/ImeTracker( 9336): uid(10377):bb927647: onHidden ```

Flutter Doctor output

Doctor output ```console [✓] Flutter (Channel stable, 3.19.6, on Arch Linux 6.8.9-arch1-1, locale en_US.UTF-8) • Flutter version 3.19.6 on channel stable at /home/felipe/snap/flutter/common/flutter • Upstream repository https://github.com/flutter/flutter.git • Framework revision 54e66469a9 (3 weeks ago), 2024-04-17 13:08:03 -0700 • Engine revision c4cd48e186 • Dart version 3.3.4 • DevTools version 2.31.1 [✓] Android toolchain - develop for Android devices (Android SDK version 34.0.0) • Android SDK at /home/felipe/Android/Sdk/ • Platform android-34, build-tools 34.0.0 • Java binary at: /home/felipe/Applications/android-studio/jbr/bin/java • Java version OpenJDK Runtime Environment (build 17.0.6+0-17.0.6b802.4-9586694) • All Android licenses accepted. [✓] Chrome - develop for the web • CHROME_EXECUTABLE = chromium [✓] Linux toolchain - develop for Linux desktop • clang version 10.0.0-4ubuntu1 • cmake version 3.16.3 • ninja version 1.10.0 • pkg-config version 0.29.1 [✓] Android Studio (version 2022.2) • Android Studio at /home/felipe/Applications/android-studio • 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.6b802.4-9586694) [✓] Connected device (3 available) • Pixel 8 (mobile) • 3A101FDJH00722 • android-arm64 • Android 14 (API 34) • Linux (desktop) • linux • linux-x64 • Arch Linux 6.8.9-arch1-1 • Chrome (web) • chrome • web-javascript • Chromium 124.0.6367.118 Arch Linux [✓] Network resources • All expected network resources are available. • No issues found! ```
darshankawar commented 3 weeks ago

Thanks for the report @Mr-Pepe

Although I don't have Pixel device, but I tried on Pixel emulator with which I didn't observe the reported behavior. I also tried on Samsung S10 with same result. Can you provide a short video of the behavior ?

Mr-Pepe commented 3 weeks ago

@darshankawar Here you go :)

https://github.com/flutter/flutter/assets/26527091/4480d94b-dcf7-40ad-b1fc-d87239cc4839

darshankawar commented 3 weeks ago

Thanks for the video. Can you provide Gboard details if possible and just to make sure, this doesn't occur with default virtual keyboard correct ?

Mr-Pepe commented 3 weeks ago

I just tried with Microsoft SwiftKey and that doesn't have the issue.

The Gboard version is 14.1.04.621126403. Let me know if there is more that I can do.

Anderillo commented 3 weeks ago

I've also been experiencing this exact same issue, and would love a fix whenever possible.

danagbemava-nc commented 2 weeks ago

Reproducible using the code sample and steps outlined above.

Tested on my Pixel 7.

https://github.com/flutter/flutter/assets/88313112/ee21e40a-685e-4fbd-bc7a-c5e5856cc418

flutter doctor -v ``` [!] Flutter (Channel stable, 3.22.0, on macOS 14.4.1 23E224 darwin-arm64, locale en-GB) • Flutter version 3.22.0 on channel stable at /Users/nexus/dev/sdks/flutter ! Warning: `flutter` on your path resolves to /Users/nexus/dev/sdks/flutters/bin/flutter, which is not inside your current Flutter SDK checkout at /Users/nexus/dev/sdks/flutter. Consider adding /Users/nexus/dev/sdks/flutter/bin to the front of your path. ! Warning: `dart` on your path resolves to /Users/nexus/dev/sdks/flutters/bin/dart, which is not inside your current Flutter SDK checkout at /Users/nexus/dev/sdks/flutter. Consider adding /Users/nexus/dev/sdks/flutter/bin to the front of your path. • Upstream repository https://github.com/flutter/flutter.git • Framework revision 5dcb86f68f (5 days ago), 2024-05-09 07:39:20 -0500 • Engine revision f6344b75dc • Dart version 3.4.0 • DevTools version 2.34.3 • 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/nexus/Library/Android/sdk • Platform android-34, build-tools 34.0.0 • Java binary at: /Users/nexus/Applications/Android Studio.app/Contents/jbr/Contents/Home/bin/java • Java version OpenJDK Runtime Environment (build 17.0.7+0-17.0.7b1000.6-10550314) • All Android licenses accepted. [✓] Xcode - develop for iOS and macOS (Xcode 15.3) • Xcode at /Applications/Xcode-15.3.0.app/Contents/Developer • Build 15E204a • CocoaPods version 1.15.2 [✓] Chrome - develop for the web • Chrome at /Applications/Google Chrome.app/Contents/MacOS/Google Chrome [✓] Android Studio (version 2023.1) • Android Studio at /Users/nexus/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.7+0-17.0.7b1000.6-10550314) [✓] IntelliJ IDEA Ultimate Edition (version 2023.2.5) • IntelliJ at /Users/nexus/Applications/IntelliJ IDEA Ultimate.app • Flutter plugin version 77.2.2 • Dart plugin version 232.10286 [✓] VS Code (version 1.89.1) • VS Code at /Applications/Visual Studio Code.app/Contents • Flutter extension version 3.88.0 [✓] Connected device (6 available) • Pixel 7 (mobile) • adb-28291FDH2001SA-5Lv71w._adb-tls-connect._tcp. • android-arm64 • Android 14 (API 34) • Nexus (mobile) • 00008020-001875E83A38002E • ios • iOS 17.4.1 21E236 • Dean’s iPad (mobile) • 00008103-000825C811E3401E • ios • iOS 17.4.1 21E236 • macOS (desktop) • macos • darwin-arm64 • macOS 14.4.1 23E224 darwin-arm64 • Mac Designed for iPad (desktop) • mac-designed-for-ipad • darwin • macOS 14.4.1 23E224 darwin-arm64 • Chrome (web) • chrome • web-javascript • Google Chrome 124.0.6367.202 [✓] Network resources • All expected network resources are available. ! Doctor found issues in 1 category. ``` ``` [✓] Flutter (Channel master, 3.22.0-33.0.pre.5, on macOS 14.4.1 23E224 darwin-arm64, locale en-GB) • Flutter version 3.22.0-33.0.pre.5 on channel master at /Users/nexus/dev/sdks/flutters • Upstream repository https://github.com/flutter/flutter.git • Framework revision 21177cced4 (2 hours ago), 2024-05-14 01:06:32 -0400 • Engine revision 84687fe0f1 • Dart version 3.5.0 (build 3.5.0-152.0.dev) • DevTools version 2.36.0-dev.5 [✓] Android toolchain - develop for Android devices (Android SDK version 34.0.0) • Android SDK at /Users/nexus/Library/Android/sdk • Platform android-34, build-tools 34.0.0 • Java binary at: /Users/nexus/Applications/Android Studio.app/Contents/jbr/Contents/Home/bin/java • Java version OpenJDK Runtime Environment (build 17.0.7+0-17.0.7b1000.6-10550314) • All Android licenses accepted. [✓] Xcode - develop for iOS and macOS (Xcode 15.3) • Xcode at /Applications/Xcode-15.3.0.app/Contents/Developer • Build 15E204a • CocoaPods version 1.15.2 [✓] Chrome - develop for the web • Chrome at /Applications/Google Chrome.app/Contents/MacOS/Google Chrome [✓] Android Studio (version 2023.1) • Android Studio at /Users/nexus/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.7+0-17.0.7b1000.6-10550314) [✓] IntelliJ IDEA Ultimate Edition (version 2023.2.5) • IntelliJ at /Users/nexus/Applications/IntelliJ IDEA Ultimate.app • Flutter plugin version 77.2.2 • Dart plugin version 232.10286 [✓] VS Code (version 1.89.1) • VS Code at /Applications/Visual Studio Code.app/Contents • Flutter extension version 3.88.0 [✓] Connected device (5 available) • Pixel 7 (mobile) • adb-28291FDH2001SA-5Lv71w._adb-tls-connect._tcp. • android-arm64 • Android 14 (API 34) • Nexus (mobile) • 00008020-001875E83A38002E • ios • iOS 17.4.1 21E236 • macOS (desktop) • macos • darwin-arm64 • macOS 14.4.1 23E224 darwin-arm64 • Mac Designed for iPad (desktop) • mac-designed-for-ipad • darwin • macOS 14.4.1 23E224 darwin-arm64 • Chrome (web) • chrome • web-javascript • Google Chrome 124.0.6367.202 ! Error: Browsing on the local area network for Dean’s iPad. Ensure the device is unlocked and attached with a cable or associated with the same local area network as this Mac. The device must be opted into Developer Mode to connect wirelessly. (code -27) [✓] Network resources • All expected network resources are available. • No issues found! ```
justinmc commented 1 week ago

I can reproduce this on my physical Pixel 6, though only when I cause the "Change keyboard" dialog to appear and make a selection, not when I just tap the globe 🌐 key to immediately switch languages.

I've confirmed that this is not coming from the framework. The framework does not send TextInput.hide when I reproduce the bug.

I see the engine calls hideSoftInputFromWindow in two places, maybe one of those are causing it to close: https://github.com/flutter/engine/blob/8eba63f29b8b26931496807cb4fc2fed8e4e3258/shell/platform/android/io/flutter/plugin/editing/TextInputPlugin.java#L371, https://github.com/flutter/engine/blob/8eba63f29b8b26931496807cb4fc2fed8e4e3258/shell/platform/android/io/flutter/plugin/editing/TextInputPlugin.java#L401.

And here's an old issue that sounds vaguely similar: https://github.com/flutter/flutter/issues/34169.

Maybe someone with more Android expertise would have a hunch @reidbaker ?

Edit: Oh and I see this in my console when I reproduce it:

I/ImeTracker(15092): com.example.sandbox:eb0c645b: onRequestHide at ORIGIN_CLIENT_HIDE_SOFT_INPUT reason HIDE_SOFT_INPUT_BY_INSETS_API
Mr-Pepe commented 5 days ago

This does not happen if the app is run as a web app. No idea if that's relevant.