2

I really love using Slivers, but really have a Problem with SliverAppBar: usually, I use the SliverAppBar Widget for creating a AppBar that has a Background Image with the Parallax effect. So all I use is the flexible space, which unfortunately gets blocked by the AppBar Portion whenever you scroll down. I tried making the AppBar portion transparent by setting the color value to transparent, but all it does is make the whole thing, including the flexiblespace, transparent..

Is there a way to use only the flexiblespace of a SliverAppBar without fading in the AppBar portion when scrolling down?

here is the code I use:

SliverAppBar(
          expandedHeight: 220,
          pinned: false,
          forceElevated: true,
          backgroundColor: Colors.transparent,
          stretch: true,
          leading: Container(),
          flexibleSpace: FlexibleSpaceBar(
            collapseMode: CollapseMode.pin,
            background:Stack(
              children: <Widget>[
                Image(
                  fit: BoxFit.cover,
                  width: MediaQuery.of(context).size.width,
                  image: AssetImage('assets/images/image2.png'),
                ),
                Positioned(
                  bottom: 0,
                  child: Container(
                    height: 110,
                    width: MediaQuery.of(context).size.width,
                    color: Colors.grey,
                  ),
                ),
                Positioned(
                  bottom: 10,
                  left: 10,
                  child: CircleAvatar(
                    radius: 45,
                    backgroundImage: AssetImage('assets/images/image.png'),
                  ),
                ),
                Positioned(
                  bottom: 77,
                  left: 110,
                  child: Container(
                    width: MediaQuery.of(context).size.width -110 -60,

                    child: Text(
                      'Name...',
                      overflow: TextOverflow.ellipsis,
                      maxLines: 1,
                      style: TextStyle(
                          fontSize: 20,
                          color: Colors.white
                      ),
                    ),
                  ),
                ),
                Positioned(
                  bottom: 63,
                  left: 110,
                  child: Container(
                    width: MediaQuery.of(context).size.width -110 -60,

                    child: Text(
                      'description...',
                      overflow: TextOverflow.ellipsis,
                      maxLines: 1,
                      style: TextStyle(
                          fontSize: 14,
                          color: Colors.white70
                      ),
                    ),
                  ),
                ),

                Positioned(
                  bottom: 5,
                  left: 110,
                  child: Container(
                    width: MediaQuery.of(context).size.width-110,
                    child: Row(
                      mainAxisAlignment: MainAxisAlignment.start,

                      children: <Widget>[

                      ],
                    ),
                  ),
                ),

              ],
            ),
          ),

        ),

Hope these Images help explain my problem: The flexiblespace looks as follows:Image 1 but once you scroll down, the flexible space fades away as seen in this picture:Image 2 I don't want to flexiblespace to fade away.

4

1 回答 1

3

您可能已经找到了答案,但我想如果像我这样的人遇到您的问题,这可能是您正在寻找的答案。

FlexibleSpaceBar 包括淡出动画,排除它的唯一方法是编写自己的 LayoutBuilder 来处理折叠。

这是一个代码示例,您可以如何使用 LayoutBuilder 创建自定义的 flexibleSpace 小部件。

NestedScrollView(
      headerSliverBuilder: (context, innerBoxIsScrolled) {
        return [
          SliverAppBar(
            titleSpacing: 0,
            pinned: false,
            backgroundColor: Colors.transparent,
            automaticallyImplyLeading: false,
            elevation: 0.0,
            actions: [
              IconButton(
                splashRadius: 20,
                icon: Icon(
                  FontAwesomeIcons.cog,
                  color: Colors.grey[200],
                ),
                onPressed: () {
                 
                },
              ),
            ],
            toolbarHeight: kToolbarHeight,
            expandedHeight: maxExtent,
            flexibleSpace: LayoutBuilder(
              builder: (context, constraints) {
                double currentExtent = constraints.maxHeight;
                final double deltaExtent = maxExtent - minExtent;
                // 0.0 -> Expanded
                // 1.0 -> Collapsed to toolbar
                final double t =
                    (1.0 - (currentExtent - minExtent) / deltaExtent)
                        .clamp(0.0, 1.0) as double;

                return Stack(
                  fit: StackFit.loose,
                  overflow: Overflow.clip,
                  children: [
                    Positioned(
                      top: _getCollapsePadding(t),
                      left: 0.0,
                      right: 0.0,
                      height: maxExtent,
                      child: Wrap(
                        crossAxisAlignment: WrapCrossAlignment.start,
                        children: [
                          //Insert Widgets add to your flexible space
                        ],
                      ),
                    ),
                  ],
                );
              },
            ),
          ),
        ];
      },
      body: Container() 
    ),

  double get maxExtent => 300;
  double get minExtent => kToolbarHeight;

  double _getCollapsePadding(double t) {
    final double deltaExtent = maxExtent - minExtent;
    return -Tween<double>(begin: 0.0, end: deltaExtent / 4).transform(t);
  }
于 2020-12-17T12:20:11.903 回答