这是一些小部件中的简单代码。 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,
...
),
);
}
),
),
),
附言 flutter 是
1.17.0-3.2pre
, Dart 是 2.8.0-dev.20.10
最佳答案
我认为您需要将此行添加到构建器方法的顶部:
FocusScope.of(context).requestFocus(focusNode);
您的代码应如下所示: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,
...
),
);
}
),
),
),
关于flutter - RawKeyboardListener 未在 Flutter web 中触发任何事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61609806/