在Flutter中实现折叠工具栏

时间:2019-06-29 19:22:05

标签: flutter dart android-toolbar flutter-layout flutter-sliver

我正在尝试使用SliverApp栏在我的应用程序中实现折叠工具栏,但是问题是,当SliverAppBar折叠时,SliverAppBar会重叠我的内容,我附上gif以进一步了解,

绿色背景中的内容在工具栏下,我想避免这种情况,任何潜在客户均会受到赞赏。

  @override
  Widget build(BuildContext context) {
    // TODO: implement buildr
    var _tabs = {"1", "2", "3"};
    return Scaffold(
      backgroundColor: Colors.white,
      body: NestedScrollView(
          headerSliverBuilder: (BuildContext context, bool innerBoxIsScrolled) {
            return <Widget>[
              SliverOverlapAbsorber(
                  handle:
                      NestedScrollView.sliverOverlapAbsorberHandleFor(context),
                  child: SliverAppBar(
                    expandedHeight: 250.0,
                    floating: false,
                    pinned: true,
                    flexibleSpace: FlexibleSpaceBar(
                        centerTitle: true,
                        background: Container(
                          decoration: BoxDecoration(
                            shape: BoxShape.rectangle,
                            image: DecorationImage(
                              fit: BoxFit.fill,
                              image: CachedNetworkImageProvider(
                                  newsPost.photos[0].url),
                            ),
                          ),
                        )),
                    forceElevated: innerBoxIsScrolled,
                  ))
            ];
          },
          body: SafeArea(
            top: false,
            bottom: false,
            child: Builder(
              builder: (BuildContext context) {
                return CustomScrollView(
                  slivers: <Widget>[
                    SliverOverlapInjector(
                      handle: NestedScrollView.sliverOverlapAbsorberHandleFor(
                          context),
                    ),
                    SliverPadding(
                      padding: const EdgeInsets.all(8.0),
                      sliver: SliverFillRemaining(child: _getBody()),
                    ),
                  ],
                );
              },
            ),
          )),
    );
  }

enter image description here

2 个答案:

答案 0 :(得分:2)

我认为您应该使用SliverList而不是SliverFillRemaining

body: SafeArea(
        top: false,
        bottom: false,
        child: Builder(
          builder: (BuildContext context) {
            return CustomScrollView(
              shrinkWrap: true,
              slivers: <Widget>[
                SliverOverlapInjector(
                  handle: NestedScrollView.sliverOverlapAbsorberHandleFor(
                      context),
                ),
                SliverPadding(
                  padding: const EdgeInsets.all(0.0),
                  sliver: SliverList(
                    delegate: SliverChildBuilderDelegate((context, index) {
                      return Text('Lorem ipsum dolor sit');
                    }, childCount: 1),
                  ),
                ),
              ],
            );
          },
        ),
      )

答案 1 :(得分:1)

SliverAppBar 创建一个可以放置在 NestedScrollView 中的 Material Design 应用栏。两者结合起来帮助我们实现视差滚动。

SafeArea(
      child: Scaffold(
        body: NestedScrollView(
          headerSliverBuilder: (BuildContext context, bool innerBoxIsScrolled) {
            return <Widget>[
              SliverAppBar(
                expandedHeight: 240.0,
                floating: false,
                pinned: true,
                flexibleSpace: FlexibleSpaceBar(
                    centerTitle: true,
                    title: Text(
                      "App Bar",
                    ),
                    background: Image.network(
                      "https://images.pexels.com/photos/4148020/pexels-photo-4148020.jpeg",
                      fit: BoxFit.cover,
                    )),
              ),
            ];
          },
          body: Center(
            child: Text("Hello World!!!"),
          ),
        ),
      ),
    );

enter image description here