首页 > 解决方案 > 如何将“SliverPersistentHeader”固定在顶部,直到向下滚动后到达“SliverList”的第一项?

问题描述

我在 CustomScrollView 中使用 SliverPersistentHeader 和 SliverList 。在到达 SliverList 的第一项(索引:0)之前,我不希望我的 SliverPersistentHeader 下降。即使我在 sliverlist 的索引 50,当我向下滚动时,标题仍然会下降。有没有捷径可以做到这一点?

这是我得到的 ==>> https://i.imgur.com/yl48Kx2.gif

class Home extends StatelessWidget {
  @override
  Widget build(BuildContext context) {

    return SafeArea(
      child: Material(
        child: CustomScrollView(
          slivers: [
            SliverPersistentHeader(
              delegate: MySliverAppBar(expandedHeight: 250),
              pinned: true,
              floating: true,
            ),
            SliverSafeArea(
              sliver: SliverAppBar(
                pinned: true,
                backgroundColor: Color(0xffA2B8A1),
              ),
            ),
            SliverList(
              delegate: SliverChildBuilderDelegate(
                (_, index) => ListTile(
                  title: Text("Index: $index"),
                ),
              ),
            )
          ],
        ),
      ),
    );
  }
}

标签: androidflutterflutter-layoutflutter-animationflutter-sliver

解决方案


尝试设置pinnedtrue保持floatingfalse. 这是每个人的工作。

Floating:如果用户反转滚动方向,标题是否应立即再次增长。

Pinned:当标题达到其最小尺寸时,是否将其粘贴到视口的开头。


推荐阅读