14.8 C
New York
Wednesday, December 11, 2024

android – How do you present and conceal the keyboard with out TextField?


My present code creates a FocusNode that KeyboardListener makes use of to focus and unfocus when the display is tapped. That does not open the keyboard so I added SystemChannels.textInput.invokeMethod("TextInput.present") and SystemChannels.textInput.invokeMethod("TextInput.disguise"), however these will not be engaged on iOS simulation or actual iOS gadget.

class MyClass extends StatefulWidget {
  const MyClass({tremendous.key});

  @override
  State createState() => _MyClassState();
}

class _MyClassState extends State {
late FocusNode _focusNode;

KeyEventResult keyEventHandler(KeyEvent occasion) {
  if (kDebugMode) {
    print("deal with key: ${occasion.logicalKey}");
  }
  return KeyEventResult.dealt with;
}

@override
void initState() {
  _focusNode = new FocusNode(
    debugLabel: "wordlet",
  );
  tremendous.initState();
}

@override
void dispose() {
  _focusNode.dispose();
  tremendous.dispose();
}

@override
Widget construct(BuildContext context) {
  return KeyboardListener(
    focusNode: _focusNode,
    onKeyEvent: keyEventHandler,
    autofocus: true,
    little one: GestureDetector(
      onTap: () {
        if (_focusNode.hasFocus) {
          _focusNode.unfocus();
          SystemChannels.textInput.invokeMethod("TextInput.disguise");
          if (kDebugMode) {
            print("unfocus");
          }
        } else {
          _focusNode.requestFocus();
          SystemChannels.textInput.invokeMethod("TextInput.present");
          if (kDebugMode) {
            print("focus requested");
          }
        }
      }, // onTap
    ), // GestureDetector
  ), // KeyboardListener
} // construct
} // _MyClassState

Flutter physician output:

[✓] Flutter (Channel secure, 3.24.5, on macOS 14.5 23F79 darwin-arm64, locale en-US)
[✓] Android toolchain - develop for Android units (Android SDK model 35.0.0)
[✓] Xcode - develop for iOS and macOS (Xcode 16.1)
[✓] Chrome - develop for the net
[✓] Android Studio (model 2024.2)
[✓] VS Code (model 1.95.3)
[✓] Related gadget (6 accessible)
[✓] Community sources

• No points discovered!

iOS Simulator iPhone 15, iOS 17.5

I attempted to open the keyboard by tapping the display. I obtain the debug print “focus requested” and “unfocus” however no keyboard open.

flutter: unfocus
flutter: focus requested

I additionally confirmed that textual content enter is being learn in when utilizing iOS simulator and typing on my laptop computer keyboard.

flutter: deal with key: LogicalKeyboardKey#12bb1(keyId: "0x00000061", keyLabel: "A", debugName: "Key A")
flutter: deal with key: LogicalKeyboardKey#1e0f5(keyId: "0x00000065", keyLabel: "E", debugName: "Key E")
flutter: deal with key: LogicalKeyboardKey#34b7d(keyId: "0x0000006d", keyLabel: "M", debugName: "Key M")
flutter: deal with key: LogicalKeyboardKey#12bb1(keyId: "0x00000061", keyLabel: "A", debugName: "Key A")

Related Articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Latest Articles