颤振-麻烦保持路线之间的焦点 [英] Flutter - Trouble preserving focus between routes

查看:52
本文介绍了颤振-麻烦保持路线之间的焦点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用 FocusNode s的表单来直观地指示表单的哪个部分处于活动状态.一个字段将PopupRoute扩展为一种弹出的键盘".我的问题是,当我按该字段时,会弹出键盘,但是不会出现焦点的视觉效果.

I have a form that uses FocusNodes to visually indicate which part of the form is active. One field extends a PopupRoute as a kind of pop up 'keyboard'. My problem is, when I press that field, the keyboard pops up but the visual effect of the focus doesn't occur.

FocusNode 的侦听器进行的一些调试显示,它获得了焦点,但立即失去了焦点.我认为是因为新的 PopupRoute 具有新的 FocusScopeNode ,所以我的 FocusNode 不再关注了.

Some debugging from the FocusNode's listeners shows it gets focus but immediately loses it. I think it is because the new PopupRoute has a new FocusScopeNode, so my FocusNode doesn't have focus any more.

在另一条路线上时,如何使该领域集中精力?我尝试过:

How can I keep the field focused while in the other route? I've tried:

  • 在所有构建方法中都使用 FocusScope.of(context).reparentIfNeeded(focusNode),它什么也没做(我不太了解这个功能tbh)
  • 将当前 FocusScope.of(context)传递到我的自定义 PopupRoute 中以使用.这确实有效,但是在弹出之后,我无法再聚焦任何东西了(我想它会被丢弃吗?)
  • Using FocusScope.of(context).reparentIfNeeded(focusNode) in all the build methods, which didn't do anything (I don't really understand this function tbh)
  • Passing the current FocusScope.of(context) into my custom PopupRoute to use. This actually worked, but after it's popped, I can't focus anything anymore (I guess it gets disposed?)

从代码角度讲,我在字段分接头上调用 requestFocus ,并将此侦听器添加到initState中:

Code-wise, I'm calling requestFocus on the field tap, and adding this listener in initState:

    widget.focusNode.addListener(() {
      print(widget.focusNode);
      if (widget.focusNode.hasFocus) {
        Navigator.of(context).push(
          CustomKeyboardPopupRoute(
            state: widget.state,
            position: //position stuff,
            focusScopeNode: FocusScope.of(context), //the second of my ideas which didn't quite work above
          )
        ).then((_) {
           widget.focusNode.unfocus();
        });
    }); 

推荐答案

您在正确的轨道上,实际上是由于 FocusScopeNode 而发生的.

You are on the right track, indeed this happens because of the FocusScopeNode.

使您的键盘路径扩展 TransitionRoute :

class CustomKeyboardPopupRoute extends TransitionRoute {
  @override
  bool get opaque => false;

  @override
  Duration get transitionDuration => Duration(milliseconds: 300);

  @override
  Iterable<OverlayEntry> createOverlayEntries() sync* {
    yield OverlayEntry(
      opaque: false,
      maintainState: true,
      builder: _buildKeyboard,
    );
  }

  Widget _buildKeyboard(BuildContext context) {
    final positionAnimation = Tween(begin: Offset(0.0, 1.0), end: Offset.zero).animate(animation);
    return SlideTransition(position: positionAnimation, child: Align(
      alignment: Alignment.bottomCenter,
      child: ...
    ),);
  }
}

这篇关于颤振-麻烦保持路线之间的焦点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆