首页 > 解决方案 > 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 (

标签: fluttercustomscrollview

解决方案


使用选项卡小部件时,这似乎是一个经常出现的错误。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上打开一个问题,以更好地了解这一点。


推荐阅读