Flutter SliverAppBar和SliverList具有不同的滚动

时间:2020-06-17 23:23:13

标签: listview flutter nested-lists flutter-sliver sliverappbar

[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具有不同的滚动,而SliverList具有其自己的滚动。

如果我尝试从条状appbar滚动,那么只有两个滚动条,但是,如果从条状列表中滚动,只有条状列表滚动,并且条状appbar具有完整的展开高度。

我曾经尝试过设置物理学来消除内部滚动,但是仍然有两种不同的滚动方式

1 个答案:

答案 0 :(得分:0)

问题是您正在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(),
  ),
),

对于“容器”和“弹跳”物理场的颜色,可以将它们分别添加到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: [
        ...
      ]
    )
  )
);