Flutter 使用RawKeyboardListener在颤振web中获取不带文本字段焦点的按键值

Flutter 使用RawKeyboardListener在颤振web中获取不带文本字段焦点的按键值,flutter,keyboard,Flutter,Keyboard,我正在为FlitterWeb编写一个蛇视频游戏。我想使用箭头键四处移动,但我无法使用RawKeyboardListener捕捉按键。我相信这是因为我没有正确的焦点节点。在这一点上,我只是想打印出我收到的击键 以下是我的测试代码: Scaffold( appBar: AppBar(), body: RawKeyboardListener( focusNode: FocusNode(), //<-- I'm not sure what to p

我正在为FlitterWeb编写一个蛇视频游戏。我想使用箭头键四处移动,但我无法使用RawKeyboardListener捕捉按键。我相信这是因为我没有正确的焦点节点。在这一点上,我只是想打印出我收到的击键

以下是我的测试代码:

Scaffold(
      appBar: AppBar(),
      body: RawKeyboardListener(
        focusNode: FocusNode(),      //<-- I'm not sure what to put here... and it's required.
        onKey: (RawKeyEvent event) {
          print(event.data.logicalKey.keyId);
        },
        child: GridView.builder(
          itemCount: 300,
          gridDelegate:
              SliverGridDelegateWithFixedCrossAxisCount(crossAxisCount: 30),
          itemBuilder: (BuildContext context, int index) {
            return Container(
                padding: EdgeInsets.all(5.0),
                color: Colors.grey,
                child: getPixels(index));
          },
        ),
      ),
    );
尝试添加自动对焦:true

根据文件

如果此小部件将被选择为初始焦点,则为True 其范围内的其他节点当前处于关注状态。。。默认为false

尝试添加自动对焦:true

根据文件

如果此小部件将被选择为初始焦点,则为True 其范围内的其他节点当前处于关注状态。。。默认为false


我有一个解决办法。。。但我还是不完全明白。最后,我开始尽可能多地从RawKeyboardListener的Textfield示例中输入内容,我遇到了以下答案:以及本期中的示例:

使用这些共性作为模板,这就是最终有效的代码,我希望它能帮助其他人。我想知道到底发生了什么:

import 'package:flutter/services.dart';  //<-- needed for the keypress comparisons

FocusNode focusNode = FocusNode();  // <-- still no idea what this is.

  @override
  Widget build(BuildContext context) {
    FocusScope.of(context).requestFocus(focusNode); // <-- yup.  magic. no idea.
    return Scaffold(
        appBar: AppBar(),
        body: RawKeyboardListener(
          autofocus: true,
          focusNode: focusNode,   // <-- more magic
          onKey: (RawKeyEvent event) {
            if (event.data.logicalKey == LogicalKeyboardKey.arrowDown) {
               direction = "down";
               }
            if (event.data.logicalKey == LogicalKeyboardKey.arrowLeft) {
               direction = "left";
               }
            if (event.data.logicalKey == LogicalKeyboardKey.arrowRight) {
               direction = "right";
               }
            if (event.data.logicalKey == LogicalKeyboardKey.arrowUp) {
               direction = "up";
               }
          },
          child: GridView.builder(
              physics: NeverScrollableScrollPhysics(),
              itemCount: 300,
              gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
                  crossAxisCount: 30),
              itemBuilder: (BuildContext context, int index) {
                return Container(
                    padding: EdgeInsets.all(5.0),
                    color: Colors.grey,
                    child: getPixels(index));
              },
            ),
          ),
      );
  }

我有一个解决办法。。。但我还是不完全明白。最后,我开始尽可能多地从RawKeyboardListener的Textfield示例中输入内容,我遇到了以下答案:以及本期中的示例:

使用这些共性作为模板,这就是最终有效的代码,我希望它能帮助其他人。我想知道到底发生了什么:

import 'package:flutter/services.dart';  //<-- needed for the keypress comparisons

FocusNode focusNode = FocusNode();  // <-- still no idea what this is.

  @override
  Widget build(BuildContext context) {
    FocusScope.of(context).requestFocus(focusNode); // <-- yup.  magic. no idea.
    return Scaffold(
        appBar: AppBar(),
        body: RawKeyboardListener(
          autofocus: true,
          focusNode: focusNode,   // <-- more magic
          onKey: (RawKeyEvent event) {
            if (event.data.logicalKey == LogicalKeyboardKey.arrowDown) {
               direction = "down";
               }
            if (event.data.logicalKey == LogicalKeyboardKey.arrowLeft) {
               direction = "left";
               }
            if (event.data.logicalKey == LogicalKeyboardKey.arrowRight) {
               direction = "right";
               }
            if (event.data.logicalKey == LogicalKeyboardKey.arrowUp) {
               direction = "up";
               }
          },
          child: GridView.builder(
              physics: NeverScrollableScrollPhysics(),
              itemCount: 300,
              gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
                  crossAxisCount: 30),
              itemBuilder: (BuildContext context, int index) {
                return Container(
                    padding: EdgeInsets.all(5.0),
                    color: Colors.grey,
                    child: getPixels(index));
              },
            ),
          ),
      );
  }

嘿,洛根,谢谢你的意见!我没有注意到文档中的自动对焦参数。。。谢谢你引起我的注意!它没有解决问题,我很沮丧,我一直得到“primaryContext!”服务库中的“null”错误。我确实提出了一个解决方案,但我仍然不确定它为什么有效。。。但它确实与focusNode参数有关。无论如何,谢谢你的帮助!嘿,洛根,谢谢你的意见!我没有注意到文档中的自动对焦参数。。。谢谢你引起我的注意!它没有解决问题,我很沮丧,我一直得到“primaryContext!”服务库中的“null”错误。我确实提出了一个解决方案,但我仍然不确定它为什么有效。。。但它确实与focusNode参数有关。无论如何,谢谢你的帮助!