简体   繁体   English

Sliver 应用栏背景颜色不起作用

[英]Sliver app bar background color not working

I have a sliver app bar with the background of the same color as the color or the contents that are scrolled under it, therefore making the appbar blend with the contents.我有一个银色应用栏,其背景颜色与颜色或在其下滚动的内容相同,因此使应用栏与内容混合。 Is there any way I can completely hide the contents that are beneath the SliverAppBar?有什么办法可以完全隐藏 SliverAppBar 下方的内容吗?

So as you can see below, the background color of the main app bar is the same as the background color of what follows(the contents).正如您在下面看到的,主应用栏的背景颜色与后面的(内容)的背景颜色相同。

CustomScrollView(
      controller: controller,
      slivers: [
// This should be the main app bar where the contents have to be behind it
        SliverAppBar(
            toolbarHeight: 36,
            centerTitle: true,
            title: showChatTitle
                ? const Text(
                    'Chats',
                    style: TextStyle(fontSize: 13, color: Colors.black),
                  )
                : null,
            pinned: true,
            floating: true,
            elevation: 0,
            backgroundColor: Colors.grey.withOpacity(0.002),
            leading: Container(
              margin: const EdgeInsets.only(left: 14, top: 10),
              child: const Text(
                'Edit',
                style: TextStyle(color: Colors.blue, fontSize: 12.5),
              ),
            ),
            actions: [
              IconButton(
                onPressed: () {},
                icon: const Icon(
                  Icons.edit_calendar,
                  color: Colors.blueAccent,
                ),
              ),
            ]),
//--------------------------------------------------------------------

// Contents follows now
        SliverAppBar(
          toolbarHeight: 45,
          elevation: 0,
          pinned: pinChatText,
          foregroundColor: Colors.black,
          backgroundColor: Colors.grey.withOpacity(0.002),
          title: const Text(
            'Chats',
            style: TextStyle(
              fontSize: 25,
              fontWeight: FontWeight.bold,
              color: Colors.black,
            ),
          ),
        ),
        SliverPadding(
          padding: const EdgeInsets.symmetric(horizontal: 10),
          sliver: SliverAppBar(
              toolbarHeight: searchBarHeight,
              floating: false,
              shape: RoundedRectangleBorder(
                borderRadius: BorderRadius.circular(5),
              ),
              foregroundColor: Colors.black,
              backgroundColor: Colors.grey.withOpacity(0.2),
              title: Text.rich(
                TextSpan(
                  style: TextStyle(
                    fontSize: 15,
                    color: Colors.grey.withOpacity(textColorOpacity),
                  ),
                  children: [
                    WidgetSpan(
                      child: Icon(
                        Icons.search,
                        color: Colors.grey.withOpacity(textColorOpacity),
                        size: 17,
                      ),
                    ),
                    const TextSpan(
                      text: ' Search',
                    )
                  ],
                ),
              )),
        ),
        SliverList(
            delegate: SliverChildListDelegate(
          [
            Padding(
              padding: const EdgeInsets.only(left: 8.0, right: 8, top: 15),
              child: Row(children: const [
                Text(
                  'Broadcast Lists',
                  style: TextStyle(
                    color: Colors.blueAccent,
                  ),
                ),
                Spacer(),
                Text(
                  'New Group',
                  style: TextStyle(
                    color: Colors.blueAccent,
                  ),
                )
              ]),
            ),
            ListView.builder(
              shrinkWrap: true,
              itemCount: 20,
              itemBuilder: (context, index) => Column(
                children: const [
                  ChatItem(),
                  Divider(
                    indent: 50,
                    thickness: 0.4,
                  )
                ],
              ),
            ),
          ],
        ))
      ],
    );

  • You can try it with code:你可以用代码试试:
 NestedScrollView( controller: _scrollController, headerSliverBuilder: (BuildContext context, bool innerBoxIsScrolled) { return <Widget>[ SliverAppBar( expandedHeight: 200.0, floating: false, pinned: true, backgroundColor: Colors.red, flexibleSpace: FlexibleSpaceBar( centerTitle: true, title: Text("text sample", style: TextStyle( color: isShrink ? Colors.black : Colors.white, fontSize: 16.0, )), background: CachedNetworkImage( imageUrl: 'http://preprod.tibib-live.com/medias/cached-media/medium/5bea5964109aa-c827b05facc3781485e584dac2f4dddc.png', fit: BoxFit.cover, )), ), ]; }, body: Center( child: Text("hello world"), ), );

Okay so I had to use Colors.grey.shade50 instead of Colors.grey.withOpacity(0.003)好的,所以我不得不使用 Colors.grey.shade50 而不是 Colors.grey.withOpacity(0.003)

Check code below检查下面的代码

CustomScrollView(
      controller: controller,
      slivers: [
// This should be the main app bar where the contents have to be behind it
        SliverAppBar(
            toolbarHeight: 36,
            centerTitle: true,
            title: showChatTitle
                ? const Text(
                    'Chats',
                    style: TextStyle(fontSize: 13, color: Colors.black),
                  )
                : null,
            pinned: true,
            floating: true,
            elevation: 0,
            backgroundColor: Colors.grey.shade50, // Used shade here
            leading: Container(
              margin: const EdgeInsets.only(left: 14, top: 10),
              child: const Text(
                'Edit',
                style: TextStyle(color: Colors.blue, fontSize: 12.5),
              ),
            ),
            actions: [
              IconButton(
                onPressed: () {},
                icon: const Icon(
                  Icons.edit_calendar,
                  color: Colors.blueAccent,
                ),
              ),
            ]),
//--------------------------------------------------------------------

// Contents follows now
        SliverAppBar(
          toolbarHeight: 45,
          elevation: 0,
          pinned: pinChatText,
          foregroundColor: Colors.black,
          backgroundColor: Colors.grey.withOpacity(0.002),
          title: const Text(
            'Chats',
            style: TextStyle(
              fontSize: 25,
              fontWeight: FontWeight.bold,
              color: Colors.black,
            ),
          ),
        ),
        SliverPadding(
          padding: const EdgeInsets.symmetric(horizontal: 10),
          sliver: SliverAppBar(
              toolbarHeight: searchBarHeight,
              floating: false,
              shape: RoundedRectangleBorder(
                borderRadius: BorderRadius.circular(5),
              ),
              foregroundColor: Colors.black,
              backgroundColor: Colors.grey.withOpacity(0.2),
              title: Text.rich(
                TextSpan(
                  style: TextStyle(
                    fontSize: 15,
                    color: Colors.grey.withOpacity(textColorOpacity),
                  ),
                  children: [
                    WidgetSpan(
                      child: Icon(
                        Icons.search,
                        color: Colors.grey.withOpacity(textColorOpacity),
                        size: 17,
                      ),
                    ),
                    const TextSpan(
                      text: ' Search',
                    )
                  ],
                ),
              )),
        ),
        SliverList(
            delegate: SliverChildListDelegate(
          [
            Padding(
              padding: const EdgeInsets.only(left: 8.0, right: 8, top: 15),
              child: Row(children: const [
                Text(
                  'Broadcast Lists',
                  style: TextStyle(
                    color: Colors.blueAccent,
                  ),
                ),
                Spacer(),
                Text(
                  'New Group',
                  style: TextStyle(
                    color: Colors.blueAccent,
                  ),
                )
              ]),
            ),
            ListView.builder(
              shrinkWrap: true,
              itemCount: 20,
              itemBuilder: (context, index) => Column(
                children: const [
                  ChatItem(),
                  Divider(
                    indent: 50,
                    thickness: 0.4,
                  )
                ],
              ),
            ),
          ],
        ))
      ],
    );

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM