Skip to content

RawKeyboardListener triggers soft keyboard on Android #70546

@pascalw

Description

@pascalw

This was spun off from #67915.

On Android, RawKeyboardListener triggers the soft keyboard when it receives input. This only happens after a TextField has received focus once. Until a TextField has received focus, the soft keyboard remains hidden.

Steps to Reproduce

  1. Run flutter create bug.
  2. Replace main.dart with this:
import 'package:flutter/material.dart';

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

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: MyHomePage(),
    );
  }
}

class MyHomePage extends StatefulWidget {
  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  final FocusNode rawKbFocusNode = FocusNode();
  final FocusNode textFocusNode = FocusNode();

  bool renderTextField = true;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            RawKeyboardListener(
              onKey: (event) {
                print(event);
              },
              focusNode: rawKbFocusNode,
              autofocus: true,
              child: renderTextField
                  ? TextField(
                      focusNode: textFocusNode,
                    )
                  : Text('No textfield'),
            ),
            RaisedButton(
              child: Text('Focus RawKeyboardListener'),
              onPressed: () => rawKbFocusNode.requestFocus(),
            ),
            RaisedButton(
              child: Text('Toggle textfield'),
              onPressed: () => setState(() {
                renderTextField = !renderTextField;
              }),
            )
          ],
        ),
      ),
    );
  }
}
  1. Use a hardware keyboard to enter text. Key events are printed, no soft keyboard is visible.
  2. Focus the text field. Move focus back to RawKeyboardListener.
  3. Use the hardware keyboard again to enter text. Now the soft keyboard will pop up.
  4. Dismiss the soft keyboard and enter some more text. Soft keyboard will show again.
  5. Even if the TextField is not rendered anymore, the same behavior keeps happening.

Expected results:

I expected the soft keyboard to remain hidden. As long as noTextField receives focus, things behave as expected.
Only after a TextField has received focus once, the soft keyboard will keep popping up when the RawKeyboardListener receives input.

Actual results:

The soft keyboard shows up.

Here's a gif showing the behavior.
All input is typed via a hardware keyboard.

rawkeyboardlistener

flutter doctor -v
[✓] Flutter (Channel master, 1.24.0-8.0.pre.230, on Mac OS X 10.15.7 19H15 darwin-x64, locale en-GB)
    • Flutter version 1.24.0-8.0.pre.230 at /Users/Pascal/.local/flutter-master
    • Framework revision af5eb3b987 (32 hours ago), 2020-11-13 19:57:15 +0800
    • Engine revision dddb532b5c
    • Dart version 2.12.0 (build 2.12.0-37.0.dev)

[✓] Android toolchain - develop for Android devices (Android SDK version 30.0.1)
    • Android SDK at /Users/Pascal/Library/Android/sdk
    • Platform android-30, build-tools 30.0.1
    • ANDROID_HOME = /Users/Pascal/Library/Android/sdk
    • Java binary at: /Applications/Android Studio.app/Contents/jre/jdk/Contents/Home/bin/java
    • Java version OpenJDK Runtime Environment (build 1.8.0_242-release-1644-b3-6915495)
    • All Android licenses accepted.

[✓] Xcode - develop for iOS and macOS (Xcode 12.1)
    • Xcode at /Applications/Xcode.app/Contents/Developer
    • Xcode 12.1, Build version 12A7403
    • CocoaPods version 1.9.3

[✓] Android Studio (version 4.1)
    • 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 1.8.0_242-release-1644-b3-6915495)

[✓] IntelliJ IDEA Community Edition (version 2019.3.2)
    • IntelliJ at /Applications/IntelliJ IDEA CE.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.51.1)
    • VS Code at /Applications/Visual Studio Code.app/Contents
    • Flutter extension version 3.16.0

[✓] Connected device (2 available)
    • sdk gphone x86 64 (mobile) • emulator-5554 • android-x64 • Android 11 (API 30) (emulator)
    • macOS (desktop)            • macos         • darwin-x64  • Mac OS X 10.15.7 19H15 darwin-x64

• No issues found!

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.19Found to occur in 3.19frameworkflutter/packages/flutter repository. See also f: labels.has reproducible stepsThe issue has been confirmed reproducible and is ready to work onplatform-androidAndroid applications specificallyteam-androidOwned by Android platform teamtriaged-androidTriaged by Android platform team

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions