一尘不染

Flutter RawKeyboardListener收听了两次?

flutter

我想要实现的是在查看此小部件时,RawKeyboardListenerTextField未选中/焦点对准时立即开始收听。它运行HandleKey function来处理我要处理的问题keyCode

我遇到的问题是第一次运行该应用程序时,handleKey function似乎运行了两次。因此,在下面的示例中,why does this run twice $_keyCode当我仅输入1键时,它将打印TWICE。我认为它监听keyUp和keyDown。我想要的结果是它只能运行一次…

但是,当我选择TextField并使用模拟器键盘进行常规提交时,代码也可以正常工作。

我正在努力理解为什么它仅在与TextField交互后才有问题。我觉得它需要一个Futureawait某个地方?但我不知道

请帮忙。

    import 'package:flutter/material.dart';
    import 'package:flutter/services.dart';
    import 'dart:async';

    class KeyboardListener extends StatefulWidget {

        KeyboardListener();

        @override
        _RawKeyboardListenerState createState() => new _RawKeyboardListenerState();
    }

    class _RawKeyboardListenerState extends State<KeyboardListener> {

        TextEditingController _controller = new TextEditingController();
        FocusNode _textNode = new FocusNode();


        @override
            initState() {
            super.initState();
        }

        //Handle when submitting
        void _handleSubmitted(String finalinput) {

            setState(() {
                SystemChannels.textInput.invokeMethod('TextInput.hide'); //hide keyboard again
                _controller.clear();
            });
        }

        handleKey(RawKeyEventDataAndroid key) {
            String _keyCode;
            _keyCode = key.keyCode.toString(); //keycode of key event (66 is return)

            print("why does this run twice $_keyCode");
        }

        _buildTextComposer() {
            TextField _textField = new TextField(
                controller: _controller,
                onSubmitted: _handleSubmitted,
            );

            FocusScope.of(context).requestFocus(_textNode);

            return new RawKeyboardListener(
                focusNode: _textNode,
                onKey: (key) => handleKey(key.data),
                child: _textField
            );
        }


      @override
      Widget build(BuildContext context) {
        return new Scaffold(
          appBar: new AppBar(title: new Text("Search Item")),
          body: _buildTextComposer(),
        );
      }
    }

阅读 618

收藏
2020-08-13

共1个答案

一尘不染

使用以下类的实例会同时为keydown和keyup事件调用回调:

  • RawKeyDownEvent
  • RawKeyUpEvent

您可以将整个对象传递给handleKey,并根据对象的运行时类型进行过滤。例如

  handleKey(RawKeyEvent key) {
    print("Event runtimeType is ${key.runtimeType}");
    if(key.runtimeType.toString() == 'RawKeyDownEvent'){
        RawKeyEventDataAndroid data = key.data as RawKeyEventDataAndroid;
        String _keyCode;
        _keyCode = data.keyCode.toString(); //keycode of key event (66 is return)

        print("why does this run twice $_keyCode");
    }
  }

  _buildTextComposer() {
      TextField _textField = new TextField(
          controller: _controller,
          onSubmitted: _handleSubmitted,
      );

      FocusScope.of(context).requestFocus(_textNode);

      return new RawKeyboardListener(
          focusNode: _textNode,
          onKey: handleKey,
          child: _textField
      );
  }

如果仍然不能解决问题,请检查从handleKey方法记录的实际runtimeType,并按其进行过滤。

2020-08-13