简体   繁体   English

Flutter SliverAppBar 和 SliverList 有不同的滚动

[英]Flutter SliverAppBar and SliverList have different scrolls

[Initial Postion][1]
[Scrolling from SliverList side][2]
[Scrolling from SliverAppBar side][3]


class ProductScreen extends StatelessWidget {
  static const routeName = "/product-screen";

  @override
  Widget build(BuildContext context) {
    final _subCategory = Provider.of<SubCategoryProvider>(
      context,
      listen: false,
    );

    final _mediaQuery = MediaQuery.of(context);

    return Scaffold(
      body: SafeArea(
        bottom: true,
        top: true,
        left: true,
        right: true,
        child: CustomScrollView(
          slivers: <Widget>[
            SliverAppBar(
              expandedHeight: 200.0,
              pinned: true,
              flexibleSpace: FlexibleSpaceBar(
                title: Text("${_subCategory.currentSubCategoryName()}"),
                background: Container(
                  margin: const EdgeInsets.only(
                    top: 4,
                    bottom: 50.0,
                  ),
                  child: Image.asset(
                    'asset/images/grocery.jpeg',
                  ),
                ),
              ),
            ),
            SliverList(
              delegate: SliverChildListDelegate(
                [
                 Column(
                    children: <Widget>[
                      Container(
                        height: _mediaQuery.size.height - kToolbarHeight,
                        color: Color.fromARGB(0xff, 0xff, 0xcd, 0x3c),
                        child: ListView.builder(
                          physics: BouncingScrollPhysics(),
                          itemCount: _subCategory.differentProductCount(),
                          itemBuilder: (ctx, pdIndex) {
                            return Column(
                              children: <Widget>[
                                Container(
                                  height: 30.0,
                                  margin:
                                      const EdgeInsets.symmetric(vertical: 5.0),
                                  decoration: BoxDecoration(
                                    border: Border.all(
                                      width: 1.0,
                                      color: Colors.black,
                                    ),
                                    borderRadius: BorderRadius.circular(20.0),
                                  ),
                                  child: Row(
                                    mainAxisAlignment:
                                        MainAxisAlignment.spaceBetween,
                                    children: <Widget>[
                                      Padding(
                                        padding:
                                            const EdgeInsets.only(left: 8.0),
                                        child: Text(
                                          "${_subCategory.currentProductName(pdIndex)}",
                                          style: TextStyle(
                                            fontSize: 18.0,
                                            fontWeight: FontWeight.bold,
                                          ),
                                        ),
                                      ),
                                      Icon(Icons.arrow_drop_down, size: 30.0),
                                    ],
                                  ),
                                ),
                                Container(
                                    height: 270.0 *
                                        _subCategory
                                            .differentCompanyCount(pdIndex),
                                    child: ListView.builder(
                                      physics: ClampingScrollPhysics(),
                                      itemCount: _subCategory
                                          .differentCompanyCount(pdIndex),
                                      itemBuilder: (ctx, cyIndex) {
                                        return Column(
                                          children: <Widget>[
                                            Container(
                                                height: 250.0,
                                                margin: const EdgeInsets.only(
                                                    bottom: 20.0),
                                                color: cyIndex.isEven
                                                    ? Colors.green
                                                    : Colors.pink),
                                          ],
                                        );
                                      },
                                    )),
                              ],
                            );
                          },
                        ),
                      ),
                    ],
                  ),
                ],
              ),
            ),
          ],
        ),
      ),
    );
  }
}

Sliver AppBar has its different scroll and SliverList has its own scroll. Sliver AppBar 有不同的滚动条,SliverList 有自己的滚动条。

If I try to scroll from sliver appbar then only both scrolls but if I scroll from the sliver list only sliver list scrolls and sliver appbar has its full expanded height.如果我尝试从 sliver appbar 滚动,那么只有两个滚动,但如果我从 sliver 列表滚动,则只有 sliver 列表滚动,并且 sliver appbar 具有其完整的展开高度。

I had tried settings physics to remove the inner scrolling but still, there are two different scrolling我曾尝试设置物理来删除内部滚动,但仍然有两种不同的滚动

The problem is you are creating a Listview.builder inside a SliverList, so it looks like a nested scroll, ignore the column and ListView.builder and just use SliverChildBuilderDelegate to create the items on demand问题是您在 SliverList 中创建 Listview.builder,因此它看起来像嵌套滚动,忽略列和 ListView.builder,只需使用SliverChildBuilderDelegate创建项目

SliverList(
   delegate: SliverChildBuilderDelegate(
    (BuildContext context, int pdIndex) {
      return Column(
        children: [
          //... The children inside the column of ListView.builder
        ]
      );
    }
    childCount: _subCategory.differentProductCount(),
  ),
),

And for the color of the Container and Bouncing physics you could add those to the Scaffold backgroundColor and CustomScrollView physics respectively.对于 Container 和 Bouncing 物理的颜色,您可以分别将它们添加到 Scaffold backgroundColor 和 CustomScrollView 物理。

return Scaffold(
  backgroundColor: Color.fromARGB(0xff, 0xff, 0xcd, 0x3c),
  body: SafeArea( //bottom, top, left and right are true by default so no need to add them
    child: CustomScrollView(
      physics: BouncingScrollPhysics(),
      slivers: [
        ...
      ]
    )
  )
);

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

相关问题 Flutter:结合 SliverAppbar 和 Column 小部件 - Flutter: combining SliverAppbar with Column widget flutter 中 SliverAppBar 和 ListView 之间的奇怪空间 - Weird space between SliverAppBar and ListView in flutter Flutter:使用 ListView.builder 消失 SliverAppBar - Flutter: Disappearing SliverAppBar with ListView.builder 如何使用 SliverAppbar 创建浮动按钮并在 Flutter 中添加 ListView? - How to create floating button with SliverAppbar and add ListView in Flutter? Flutter:嵌套 ListView 有效。 嵌套的 SliverList 没有,但 ListView 包含 Sliver - Flutter: nested ListView works. Nested SliverList don't, yet ListView contains Slivers Flutter listview 在其内容滚动屏幕的末尾 - Flutter listview at the end of its content scrolls screen 我在 Flutter 的多个下拉按钮上保存不同的列表时遇到问题 - I have an issue saving a different list on multiple dropdown button in Flutter 我想单击文档图片并对其进行裁剪(如凸轮扫描仪),这要在用户单击我的 sliverList 时完成。 我是颤振的新手 - I want to click a picture of document and crop it (like cam scanner), this to be done when user clicks on my sliverList. I am new to flutter 当行高不同时,listview会滚动得很乱 - listview scrolls quite junky when therre are different row heights flutter 中的 Overscroll 指示器颜色不同? - Different color of Overscroll indicator in flutter?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM