Flutter 2. 迁移后,滚动视图 animateTo(...) 出现问题,在 addPostFrameCallback 内部调用

问题描述

基本上,我有一个可滚动的部分列。每个部分都是一个扩展图块,具有 [ ListView ] 作为子属性。 (ListView 使用 NeverScrollPhysics,因此 ListView 的所有子项都是一次性构建的)。当我打开一个部分(展开磁贴)时,我希望 Scrollview 滚动,例如打开的磁贴(一次只能打开一个磁贴)位于最顶部。如您所见,如果底部没有足够的空间使列可滚动,我会添加一个额外的空间。我在 animateto()调用 addPostFrameCallback。在我迁移到 Flutter 2 之前,它在发布和调试模式下都能正常工作。但现在它只能在调试模式下正常工作,在发布时,滚动视图不会滚动(我已经检查过 scrollOffset 是否正确计算)。在释放模式下,它仅在我从打开-关闭-打开快速更改折叠状态时才起作用。而且它似乎适用于最底部的部分。应为其添加额外空间。在其他情况下,它只是展开而不执行滚动。

 return LayoutBuilder(builder: (context,constraints) {
              var selectedSection = sections.firstWhere(
                  (element) => element.isExpanded,orElse: () => null);
              var selectedSectionIndex = sections.indexOf(selectedSection);
              scrollOffset = calculateScrollOffset(selectedSectionIndex);
              double bottomExtraSpaceHeight = calculateBottomExtraSpaceHeight(
                constraints.maxHeight,scrollOffset,selectedSection,sections,);
              bottomExtraSpace = SizedBox(height: bottomExtraSpaceHeight);
              WidgetsBinding.instance.addPostFrameCallback((timeStamp) {
                if (_scrollController.hasClients) {
                  _scrollController.animateto(scrollOffset,duration: Duration(milliseconds: 350),curve: Curves.easeIn);
                }
              });
              
              return Container(
                alignment: Alignment.topCenter,child: SingleChildScrollView(
                  controller: _scrollController,child: Column(
                    mainAxisSize: MainAxisSize.min,crossAxisAlignment: CrossAxisAlignment.start,children: [
                      buildFiltersRow(),buildSections(sections),bottomExtraSpace
                    ],),);
            })

如果我改变了

WidgetsBinding.instance.addPostFrameCallback((timeStamp) {
  if (_scrollController.hasClients) {
    _scrollController.animateto(scrollOffset,curve: Curves.easeIn);
  }
});

Future.delayed(Duration(milliseconds: 200)).then((_) {
  if (_scrollController.hasClients) {
    _scrollController.animateto(scrollOffset,curve: Curves.easeIn);
  }
});

它开始工作正常,但似乎不是一个好的解决方案。关于我做错了什么以及如何以适当的方式修复它的任何想法都将不胜感激。谢谢!

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...