【问题标题】:SliverAppBar - SliverList is Scrolling all the way to the topSliverAppBar - SliverList 一直滚动到顶部
【发布时间】:2020-03-16 04:21:25
【问题描述】:

我目前正在使用 SliverAppBar,并且在滚动 sliverList 时遇到问题。

在上图中,我的 TabBar 一直到通知栏。当 sliverAppBar 崩溃时,我希望我的 Tabbar 贴在 AppBar 的底部。

这是我正在尝试的代码...

    return Scaffold(
  body: CustomScrollView(

    slivers: <Widget>[
      SliverAppBar(
        //backgroundColor: Colors.transparent,
        actions: <Widget>[
          IconButton(icon: Icon(Icons.favorite), onPressed: () {}),
          IconButton(icon: Icon(Icons.share), onPressed: () {})
        ],
        floating: false,
        pinned: true,
        expandedHeight: getHeight(context),
        flexibleSpace: FlexibleSpaceBar(
          title: Text("text"),
          background: Container(
            height: double.infinity,
            width: double.infinity,
            decoration: BoxDecoration(
                image: DecorationImage(
                    image: AssetImage(currentImage),
                    fit: BoxFit.cover)),
          ),
        ),
      ),
      SliverList(
        delegate: SliverChildListDelegate(bottomListView),
      ),
    ],
  )
  ,
);

【问题讨论】:

  • 您希望 TabBar 始终位于应用栏下方吗?
  • @IgorKharakhordin 是的...

标签: flutter dart flutter-layout flutter-sliver flutter-appbar


【解决方案1】:

可能是个bug:

https://github.com/flutter/flutter/issues/22393

所以你可以使用这个package,它解决了这个问题。

【讨论】:

    【解决方案2】:

    只需使用 SliverAppBarbottom 参数并将 TabBar 传递给它。在 FlexibleSpaceBar 中添加 titlePadding 以从 TabBar 添加填充。如果需要更改 TabBar 颜色,可以查看this question

    注意 FlexibleSpaceBar 中的标题和 AppBar 中的操作图标,因为长标题会导致最小化的 appbar 重叠。

        child: CustomScrollView(
          slivers: <Widget>[
            SliverAppBar(
              //backgroundColor: Colors.transparent,
              actions: <Widget>[
                IconButton(icon: Icon(Icons.favorite), onPressed: () {}),
                IconButton(icon: Icon(Icons.share), onPressed: () {})
              ],
              floating: false,
              pinned: true,
              expandedHeight: getHeight(context),
              flexibleSpace: FlexibleSpaceBar(
                title: Text("text"),
                // Adding padding from TabBar
                titlePadding: EdgeInsets.only(bottom: 64, left: 60),
                background: Container(
                  height: double.infinity,
                  width: double.infinity,
                  decoration: BoxDecoration(
                      image: DecorationImage(
                          image: AssetImage(currentImage),
                          fit: BoxFit.cover)),
                ),
              ),
              // Adding TabBar to the bottom of SliverAppBar
              bottom: TabBar(
                tabs: [
                  for (var i = 0; i < 3; i++)
                    Tab(
                      text: 'test $i',
                    ),
                ]
              ),
            ),
            SliverList(
              delegate: SliverChildListDelegate(bottomListView),
            ),
          ],
        ),
    

    【讨论】:

    • 我用 Container 包裹了 TabBarVIew 并赋予它白色。所以,我不必更改整个应用程序的原色。这是另一种方式...但是,它的工作原理谢谢...
    猜你喜欢
    • 2020-10-07
    • 2019-05-20
    • 2020-05-24
    • 1970-01-01
    • 2020-11-15
    • 1970-01-01
    • 1970-01-01
    • 2021-01-22
    • 2016-08-26
    相关资源
    最近更新 更多