Soft keyboard stopped working in flutter's WebView on Android 12

2,808

UPD: Since webview_flutter:3.0.0 Step 2 from actions described below isn't needed, as developers changed default mode to Hybrid composition, which has no issue with keyboard on Android 12.

Original answer: webview_flutter has an option to set the platform view mode. As described in the README the composition mode can be switched from Virtual displays to Hybrid composition. This solves the problem, allows for full keyboard support and only requires Android SDK support 19 but comes with a drawback. The performance on Android 9 and lower is not as good as with the default virtual display mode.

To enable hybrid composition (copying webview's readme right now):

  1. Set minSdkVersion in android/app/build.gradle
 android {
     defaultConfig {
         minSdkVersion 19
     }
 }
  1. Put if (Platform.isAndroid) WebView.platform = SurfaceAndroidWebView(); somewhere in your application e.g. main.dart or in your StatefulWidget's initState() method.
Share:
2,808
ploth
Author by

ploth

Co-Founder / Software Engineer at cc.systems

Updated on January 02, 2023

Comments

  • ploth
    ploth over 1 year

    I have a few flutter applications in production where the webview stopped working. webview_flutter fails to show the (soft) keyboard on Android 12 (API 31), no matter of what kind of input field is clicked. The error can be easily reproduced with the Android emulator when switching between Android 11 and 12. On Android 12 it fails with a exception like this:

    [        ] V/InputMethodManager(10400): b/117267690: Display ID mismatch found. ViewRootImpl displayId=1 InputMethodManager displayId=0. Use the right InputMethodManager instance to avoid performance overhead.
    [        ] V/InputMethodManager(10400): java.lang.Throwable
    [        ] V/InputMethodManager(10400):     at android.view.inputmethod.InputMethodManager.getFallbackInputMethodManagerIfNecessary(InputMethodManager.java:549)
    [        ] V/InputMethodManager(10400):     at android.view.inputmethod.InputMethodManager.restartInput(InputMethodManager.java:1843)
    [        ] V/InputMethodManager(10400):     at org.chromium.content.browser.input.ImeAdapterImpl.H(chromium-TrichromeWebViewGoogle6432.apk-stable-447211487:4)
    [        ] V/InputMethodManager(10400):     at org.chromium.content.browser.input.ImeAdapterImpl.updateState(chromium-TrichromeWebViewGoogle6432.apk-stable-447211487:30)
    [        ] V/InputMethodManager(10400):     at android.os.MessageQueue.nativePollOnce(Native Method)
    [        ] V/InputMethodManager(10400):     at android.os.MessageQueue.next(MessageQueue.java:335)
    [        ] V/InputMethodManager(10400):     at android.os.Looper.loopOnce(Looper.java:161)
    [        ] V/InputMethodManager(10400):     at android.os.Looper.loop(Looper.java:288)
    [        ] V/InputMethodManager(10400):     at android.app.ActivityThread.main(ActivityThread.java:7842)
    [        ] V/InputMethodManager(10400):     at java.lang.reflect.Method.invoke(Native Method)
    [        ] V/InputMethodManager(10400):     at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:548)
    [        ] V/InputMethodManager(10400):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1003)
    [        ] V/InputMethodManager(10400): b/117267690: Display ID mismatch found. ViewRootImpl displayId=1 InputMethodManager displayId=0. Use the right InputMethodManager instance to avoid performance overhead.
    [        ] V/InputMethodManager(10400): java.lang.Throwable
    [        ] V/InputMethodManager(10400):     at android.view.inputmethod.InputMethodManager.getFallbackInputMethodManagerIfNecessary(InputMethodManager.java:549)
    [        ] V/InputMethodManager(10400):     at android.view.inputmethod.InputMethodManager.showSoftInput(InputMethodManager.java:1625)
    [        ] V/InputMethodManager(10400):     at android.view.inputmethod.InputMethodManager.showSoftInput(InputMethodManager.java:1617)
    [        ] V/InputMethodManager(10400):     at sH.d(chromium-TrichromeWebViewGoogle6432.apk-stable-447211487:3)
    [        ] V/InputMethodManager(10400):     at org.chromium.content.browser.input.ImeAdapterImpl.L(chromium-TrichromeWebViewGoogle6432.apk-stable-447211487:20)
    [        ] V/InputMethodManager(10400):     at org.chromium.content.browser.input.ImeAdapterImpl.updateState(chromium-TrichromeWebViewGoogle6432.apk-stable-447211487:32)
    [        ] V/InputMethodManager(10400):     at android.os.MessageQueue.nativePollOnce(Native Method)
    [        ] V/InputMethodManager(10400):     at android.os.MessageQueue.next(MessageQueue.java:335)
    [        ] V/InputMethodManager(10400):     at android.os.Looper.loopOnce(Looper.java:161)
    [        ] V/InputMethodManager(10400):     at android.os.Looper.loop(Looper.java:288)
    [        ] V/InputMethodManager(10400):     at android.app.ActivityThread.main(ActivityThread.java:7842)
    [        ] V/InputMethodManager(10400):     at java.lang.reflect.Method.invoke(Native Method)
    [        ] V/InputMethodManager(10400):     at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:548)
    [        ] V/InputMethodManager(10400):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1003)
    [        ] W/InputMethodManager(10400): Ignoring showSoftInput() as view=com.pichillilorenzo.flutter_inappwebview.in_app_webview.InAppWebView{c5bb4f2 VFEDHVCL. .F...... 0,0-1080,1911} is not served.
    
  • ZeroCode
    ZeroCode about 2 years
    Not working for me because: The plugin "google_maps_flutter" requires a higher Android SDK version.
  • ploth
    ploth about 2 years
    @ZeroCode it is totally fine to specify a newer sdk version ;)