flutter - CustomScrollView 和 PageView 问题
问题描述
我有 CustomScrollView,里面有 SliverFixedExtentList 和 SliverList。我在 SliverFixedExtentList 的委托中放置了一个 PageView。事情是在滚动到底部并打开详细信息页面并返回之后它给了我这个我不太明白的异常:
══╡ EXCEPTION CAUGHT BY WIDGETS LIBRARY ╞═══════════════════════════════════════════════════════════
I/flutter (25339): The following assertion was thrown while finalizing the widget tree:
I/flutter (25339): 'package:flutter/src/widgets/scroll_position.dart': Failed assertion: line 661 pos 12: 'pixels !=
I/flutter (25339): null': is not true.
你知道发生了什么吗?这是产生问题的要点:https ://gist.github.com/figengungor/e03704744d0ef786a15ecb783060f730 (
解决方案
使用选项卡小部件时,这似乎是一个经常出现的错误。github上有一个未解决的问题,它似乎解决了与您的问题密切相关的其他案例。
在您的情况下,这似乎发生在您的PageView
小部件失去屏幕焦点时。虽然这不是一个好的解决方案(但确实解决了错误),但它应该可以帮助您:
声明一个ScrollController
:
class HomePageState extends State<HomePage> {
ScrollController _scrollController = new ScrollController();
...
将其分配给您的CustomScrollView
:
CustomScrollView(
controller: _scrollController,
...
在转到另一个页面之前,向上滚动列表以使 PageView 小部件可见:
_openSecondPage(BuildContext context) {
_scrollController.animateTo(0.0, duration: Duration(milliseconds: 1), curve: Curves.bounceInOut);
Navigator.of(context).push(MaterialPageRoute(builder: (_) => SecondPage()));
...
您应该保存滚动位置以滚动到单击项目时的位置。
PS:在颤振 github上打开一个问题,以更好地了解这一点。
推荐阅读
- c# - C# Dynamically determine class properties based on configuration for comparison
- c# - C# OData use query parameter as input but return a custom response
- google-apps-script - Copy not contiguous selected rows from a spreadsheet to another
- c# - URP 中的自定义着色器不可升级
- python-sphinx - Tox 在 Docker 容器中通过 Poetry 运行 sphinx-build 时抛出错误
- java - 无法使用环境变量实例化 yaml 变量列表:无法解析值中的占位符
- postgresql - Lob Coulmn 用于标准生成器的 JPA 规范
- node.js - 此 set-cookie 被阻止,因为它的域属性对于当前主机 url 无效
- c# - 用户输入无法保存到数据库
- mysql - 使用 date(createdAt) 和不使用 createdAt 的区别