简体   繁体   English

Flutter:按下按键时跳出“Focus”Widget

[英]Flutter: Jump out of "Focus" Widget on key pressed

I want to traverse a Focus-Group with arrow keys, so that the next/previous focusNode is selected.我想用箭头键遍历 Focus-Group,以便选择下一个/上一个 focusNode。 On tab/shift+tab I want the focus to jump out of that focus group, to the next/previous focusNode OUTSIDE my Focus-Group:在 tab/shift+tab 上,我希望焦点跳出那个焦点组,到我的焦点组之外的下一个/上一个焦点节点:

Focus(
  onKey: (focusNode, event) {
     if (event.runtimeType == RawKeyDownEvent) {
      if (event.logicalKey == LogicalKeyboardKey.arrowDown) {
          focusNode.nextFocus();
          return KeyEventResult.handled;

      } else if (event.logicalKey == LogicalKeyboardKey.arrowUp) {
          focusNode.previousFocus();
          return KeyEventResult.handled;

      } else if (event.logicalKey == LogicalKeyboardKey.tab) {
          if(RawKeyboard.instance.keysPressed.contains(LogicalKeyboardKey.shiftLeft))){ 

            // TODO: JUMP FOCUS OUT OF THIS FOCUS WIDGET 
            // TO THE FOCUS NODE ABOVE/BEFORE THIS FOCUS WIDGET

          } else {

            // TODO: JUMP FOCUS OUT OF THIS FOCUS WIDGET 
            // TO THE FOCUS NODE BELOW/AFTER THIS FOCUS WIDGET

          }
          return KeyEventResult.handled;
      }
     }
     return KeyEventResult.ignored;
  },
  child: ...

Solution was:解决方案是:

bool someoneInListHasPrimaryFocus(Iterable<FocusNode> descendants){
  for (FocusNode fn in descendants) {
    if(fn.hasPrimaryFocus) return true;
  }
  return false;
}

                        Focus(
                          onKey: (focusNode, event) {
                            if (event.runtimeType == RawKeyDownEvent) {
                              if (event.logicalKey == LogicalKeyboardKey.arrowDown || event.logicalKey == LogicalKeyboardKey.arrowRight) {
                                focusNode.nextFocus();
                                return KeyEventResult.handled;
                              } else if (event.logicalKey == LogicalKeyboardKey.arrowUp || event.logicalKey == LogicalKeyboardKey.arrowLeft) {
                                focusNode.previousFocus();
                                return KeyEventResult.handled;
                              } else if (event.logicalKey == LogicalKeyboardKey.tab) {
                                if(RawKeyboard.instance.keysPressed.contains(LogicalKeyboardKey.shiftLeft) || RawKeyboard.instance.keysPressed.contains(LogicalKeyboardKey.shiftRight)){ //You think shiftLeft + shiftRight = shift? HAHAHA think again
                                  FocusNode firstElement = focusNode.descendants.first;
                                  firstElement.requestFocus();
                                  firstElement.previousFocus();
                                } else {
                                  if(someoneInListHasPrimaryFocus(focusNode.descendants)){
                                    FocusNode lastElement = focusNode.descendants.last;
                                    lastElement.requestFocus();
                                    lastElement.nextFocus();
                                  } else {
                                    FocusNode firstElement = focusNode.descendants.first;
                                    firstElement.requestFocus();
                                  }
                                }
                                return KeyEventResult.handled;
                              }
                            }
                            return KeyEventResult.ignored;
                          },
                          child: ...

Try the following code:试试下面的代码:

Focus.of(context).unfocus;

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

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