RawKeyboardListener 未在 Flutter Web 中触发任何事件

Bam*_*oUA 6 keyboard widget listener flutter

这是一些小部件内的简单代码。的RawKeyboardListener没有onKey被触发!那么问题是为什么呢?这是一个错误吗?

Container(
  child: StreamProvider<Item>.value(
    value: stream
    child: Consumer<Item>(
      builder: (_, item, __) {
        return RawKeyboardListener(
          focusNode: focusNode,
          onKey: (event) {
            print(event); // NOT PRINTED!!
          }
          child: TextField(
            controller: controller,
            ...
          ),
        );
      }
    ),
  ),
),
Run Code Online (Sandbox Code Playgroud)

PS Flutter是1.17.0-3.2pre,Dart是2.8.0-dev.20.10

Ban*_*eil 5

我认为您需要将此行添加到构建器方法的顶部:

FocusScope.of(context).requestFocus(focusNode);
Run Code Online (Sandbox Code Playgroud)

您的代码应如下所示:

Container(
  child: StreamProvider<Item>.value(
    value: stream
    child: Consumer<Item>(
      builder: (_, item, __) {
        FocusScope.of(context).requestFocus(focusNode); // Add this line

        return RawKeyboardListener(
          focusNode: focusNode,
          onKey: (event) {
            print(event); // NOT PRINTED!!
          }
          child: TextField(
            controller: controller,
            ...
          ),
        );
      }
    ),
  ),
),
Run Code Online (Sandbox Code Playgroud)

  • 抱歉,但这对我不起作用。它在调试应用程序中工作正常,然后在发布应用程序中失败。 (4认同)