【问题标题】:NestedScrollView issue, throwing error when switching between two screensNestedScrollView 问题,在两个屏幕之间切换时抛出错误
【发布时间】:2020-02-08 21:24:37
【问题描述】:

我对 NestedScrollView 有一些问题。我已经用 BottomNavigationBar 实现了 PageView,有时,当我在两个屏幕之间切换时,我得到了这个错误:

'package:flutter/src/widgets/nested_scroll_view.dart':断言失败:第 501 行 pos 14:'position.minScrollExtent != null && position.pixels != null':不正确。

child: NestedScrollView(
      controller: _controller,
      headerSliverBuilder: (BuildContext context, bool innerBoxIsScrolled) {
        return <Widget>[
          SliverAppBar(
            automaticallyImplyLeading: false,
            pinned: true,
            expandedHeight: 140.0,
            floating: false,
            centerTitle: true,
            flexibleSpace: FlexibleSpaceBar(
              centerTitle: true,
              background: new Container(
                child: FutureBuilder(
                  future: CoverImagesApi().getImageFile(1),
                  builder: (context, snapshot) {
                    if(snapshot.data != null) {
                      return Container(
                        child: Column(
                          children: <Widget>[
                            Expanded(
                              flex: 5,
                              child: Container(
                                alignment: Alignment.topCenter,
                              ),
                            ),
                            Expanded(
                              flex: 2,
                              child: new Container(
                                child: Center(
                                child: Text('Obecná tabuľa',
                                  style: TextStyle(
                                    fontSize: 26.0,
                                    fontWeight: FontWeight.bold
                                  ),
                                  textAlign: TextAlign.center,
                                ),
                                ),
                                decoration: BoxDecoration(
                                  color: Color.fromRGBO(255, 255, 255, 0.8)
                                ),
                                alignment: Alignment.bottomCenter, //variable above
                              )
                            ),
                          ],
                        ),
                        decoration: new BoxDecoration(
                          image: new DecorationImage(
                            fit: BoxFit.cover
                            image: snapshot.data.existsSync() ? Image.file(snapshot.data).image : AssetImage('assets/tabula.jpg'),
                          ),
                        ),
                      );
                    } else {
                      return Container(height: 0.0, width: 0.0);
                    }
                  }
                ),
              ),
            ),
          ),
        ];

      },
      body: DataFiller(scaffoldKey: _scaffoldKey),
    )

有人有什么想法吗?

【问题讨论】:

  • 能否也粘贴DataFiller的内容?
  • 尝试将extendBody: true,添加到脚手架
  • 我复制了您的代码(减去图像和FutureBuilder,并将DataFiller 替换为TabBarView),我没有看到任何错误。您将不得不展示更多代码或提供更简单的示例。
  • @kamzik​​ 你能解决这个问题吗?

标签: android flutter dart nestedscrollview


【解决方案1】:

只需将key: UniqueKey() 添加到 NestedScrollView。

您的代码将是

child: NestedScrollView(
  key: UniqueKey()
  controller: _controller,
  headerSliverBuilder: (BuildContext context, bool innerBoxIsScrolled) {
    return <Widget>[
      SliverAppBar(
        automaticallyImplyLeading: false,
        pinned: true,
        expandedHeight: 140.0,
        floating: false,
        centerTitle: true,
        flexibleSpace: FlexibleSpaceBar(
          centerTitle: true,
          background: new Container(
            child: FutureBuilder(
              future: CoverImagesApi().getImageFile(1),
              builder: (context, snapshot) {
                if(snapshot.data != null) {
                  return Container(
                    child: Column(
                      children: <Widget>[
                        Expanded(
                          flex: 5,
                          child: Container(
                            alignment: Alignment.topCenter,
                          ),
                        ),
                        Expanded(
                          flex: 2,
                          child: new Container(
                            child: Center(
                            child: Text('Obecná tabuľa',
                              style: TextStyle(
                                fontSize: 26.0,
                                fontWeight: FontWeight.bold
                              ),
                              textAlign: TextAlign.center,
                            ),
                            ),
                            decoration: BoxDecoration(
                              color: Color.fromRGBO(255, 255, 255, 0.8)
                            ),
                            alignment: Alignment.bottomCenter, //variable above
                          )
                        ),
                      ],
                    ),
                    decoration: new BoxDecoration(
                      image: new DecorationImage(
                        fit: BoxFit.cover
                        image: snapshot.data.existsSync() ? Image.file(snapshot.data).image : AssetImage('assets/tabula.jpg'),
                      ),
                    ),
                  );
                } else {
                  return Container(height: 0.0, width: 0.0);
                }
              }
            ),
          ),
        ),
      ),
    ];

  },
  body: DataFiller(scaffoldKey: _scaffoldKey),
)

【讨论】:

  • 每次上下滚动时数据都会重新加载。如何限制它。
【解决方案2】:

我使用的是 extendBody : true,在脚手架内部并且遇到了同样的错误。删除它后,错误得到解决。

【讨论】:

  • extendBody: true 是我的情况所必需的。还有其他解决方案吗?让我知道 。谢谢。
  • 我目前没有任何其他解决方案,但如果我发现任何问题,我会在此处更新。另外,如果您能给我一个可重现的代码示例,我将能够轻松测试
【解决方案3】:

我有 TabView 有 4 个标签。其中两个我有ScrollController 用于ListView,但另外两个没有ScrollController 但有Grid。对我有帮助的是将ScrollController 添加到Grids 中。也许这个建议会帮助某人在他们失踪的地方添加ScrollController

【讨论】:

    【解决方案4】:

    这没有问题,只需热重启即可解决问题。这是因为热重载不会改变应用程序的状态,并且您执行了一个不存在 draws from state 的操作(假设它存在)..

    如果一切都失败了,请尝试清理您的应用,这应该会很好

    【讨论】:

      猜你喜欢
      • 2015-04-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多