Flutter中开发TV时候焦点错乱问题

1、参考文章
flutter 基础 —— Focus 组件的使用: https://www.cnblogs.com/lemos/p/16710210.html
Flutter 的键盘组件系列 — FocusWidget:https://juejin.cn/post/7128007713501478920
Flutter入门(28):Flutter 组件之 FocusNode 详解:https://www.jianshu.com/p/f02ebc9ab004
Flutter 强制获取焦点的问题:https://blog.csdn.net/kuanxu/article/details/122062514
Flutter 按键事件监听 RawKeyboardListener:https://blog.csdn.net/xyh256/article/details/120731971
Flutter TV Android端开发技巧详细教程:http://www.45fan.com/article.php?aid=1DjvdvtIfP6ajkHd#_lab2_3_0
遇到的问题,在手机上使用触屏时候,焦点问题还不那么明显,如果在TV上面通过按键时候,焦点常常跑到不知道什么地方去,本人在实际开发一个需求时候,页面除了一些固有的可点击控件还有三个ListView,ListView的item可以被焦点选中,当进行切换时候,焦点常常移动到未知的地方,可以使用以下方式进行解决, 下面给出简略解决代码:

FocusNode focusNode1 = FocusNode();
Widget _loadCityList1Widget() {
    
    
    return Expanded(
        flex: nodeListFocusIndex == 0 ? 2 : 1,
        child: Focus(
          focusNode: focusNode1,
          onFocusChange: (focus) {
    
    
            if (focus) {
    
    
              _switchListFocus(0);
              focusNode1.children.first.requestFocus();//强制第一个节点获取焦点
            }
          },
          child: ListView.builder(
              itemCount: nodeList.length,
              itemBuilder: (context, index) {
    
    
                var name = nodeList.elementAt(index).cityName;
                return Container(
                  margin: const EdgeInsets.only(top: 27.5),
                  decoration: BoxDecoration(
                      color: node1ListSelectIndex == index
                          ? ColorStore.colorWhite_10
                          : Colors.transparent),
                  child: LineItemWidget(
                    itemIndex: index,
                    child: _loadText(name),
                    onPressed: () {
    
    
                      setState(() {
    
    
                        node1ListSelectIndex = index;
                      });
                    },
                  ),
                );
              }),
        ));
  }

猜你喜欢

转载自blog.csdn.net/Mr_Tony/article/details/128978816