简体   繁体   English

在颤动上检测删除软键盘

[英]detect delete soft keyboard on flutter

I created custom TextField widget :我创建了自定义 TextField 小部件:

class ActionTextField extends StatefulWidget {
  final String label;
  final String errorText;
  final FocusNode focusNode;...

const ActionTextField(
  {Key key,
  this.label,
  this.focusNode,...

 child: Column(
        children: [
          Material(
            elevation: 1.5,
            shadowColor: Colors.grey,
            child: Stack(
              children: [
                TextField(
                  textAlignVertical: TextAlignVertical.center,
                  obscureText: widget.isPassword,
                  focusNode: widget.focusNode,
                  controller: widget.controller ?? widget.controller,

Now I am using my widget into my project.Now i need to detect delete soft keyboard so in order to i use RawKeyboardListener according it's official page.现在我在我的项目中使用我的小部件。现在我需要检测删除软键盘,以便我根据它的官方页面使用RawKeyboardListener

  final FocusNode _focusNode = FocusNode();

  void _handleKeyEvent(RawKeyEvent event) {
    print("0000000000");
    if (event.logicalKey == LogicalKeyboardKey.delete) {
      //do whatever you have to do
      print("0000000000");
    }
  }

Widget build(BuildContext context) {...
  RawKeyboardListener(
    focusNode: _focusNode,
    onKey: _handleKeyEvent,
    child: ActionTextField(
      label: "Test",
      focusNode: _focusNode,

But when i passed _focusNode into my custom view i got error:但是当我将_focusNode传递到我的自定义视图中时,我得到了错误:

════════ Exception caught by widgets library ═══════════════════════════════════
Tried to make a child into a parent of itself.
'package:flutter/src/widgets/focus_manager.dart':
Failed assertion: line 959 pos 12: 'child != this'
The relevant error-causing widget was
    TextField 

And when i comment _focusNode from my custom view, I can not detect my key from softkeyboard!!!当我从自定义视图中评论 _focusNode 时,我无法从软键盘中检测到我的键!!! How can i detect delete soft keyboard ?如何检测删除软键盘

The problem is that you use the same FocusNode object in parent and child widgets.问题是您在父小部件和子小部件中使用相同的 FocusNode 对象。 You need to exclude TextField from RawKeyboardListener widget, for example:您需要从 RawKeyboardListener 小部件中排除 TextField,例如:

@override
Widget build(BuildContext context) {
  final focusNode = FocusNode();

  return Scaffold(
    body: SafeArea(
      child: Row(
        children: [
          RawKeyboardListener(
            focusNode: focusNode,
            child: const SizedBox.shrink(),
            onKey: (e) => print(e),
          ),
          Expanded(
            child: TextField(
              focusNode: focusNode,
            ),
          )
        ],
      ),
    ),
  );
}

In this case you will able to handle keyboard events in RawKeyboardListener and TextField will handle same events by self.在这种情况下,您将能够在 RawKeyboardListener 中处理键盘事件,而 TextField 将自行处理相同的事件。

In some cases after handling event in RawKeyboardListener TextField loses focus.在某些情况下,在 RawKeyboardListener TextField 中处理事件后会失去焦点。 I use the following workaround to return focus to TextField:我使用以下解决方法将焦点返回到 TextField:

onKey: (e) {
  // Some code for event handling

  // W/A for TextField focus loss
  textFieldFocusNode.previousFocus();
  SchedulerBinding.instance.scheduleFrameCallback((_) {
    textFieldFocusNode.nextFocus();
  });
},

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM