【问题标题】:How to nest a ListView inside a PageView如何在 PageView 中嵌套 ListView
【发布时间】:2021-03-30 12:43:38
【问题描述】:

我有一个水平的 PageView,每个页面上有一些嵌套的垂直 LIstView。

问题是我无法滚动那些嵌套的列表视图,因为我只注册了PageView 手势。

注意:当我在customSCrollView 内使用SliverChildBuilderDelegate 时没有限制,滚动实际上是活动的,但其他一切都没有

这就是代码的样子

class ProductDetail extends StatefulWidget {
  
  @override
  State<StatefulWidget> createState() => ProductDetailState();
}

class ProductDetailState extends State {
  

  @override
  Widget build(BuildContext context) {
    return Material(
      child: DefaultTabController(
        length: 2,
        child: Column(
          children: [
            Expanded(
              child: NestedScrollView(
//                 physics: BouncingScrollPhysics(),
                headerSliverBuilder:
                    (BuildContext context, bool innerBoxIsScrolled) {
                  return <Widget>[];
                },
                body: PageView( // this is a pageView container
                  children: <Widget>[
                    CustomScrollView(
                      slivers: [
                       SliverList(
                          delegate: SliverChildBuilderDelegate(
                            (context, index) {
                              return ListTile(
                                title: Text('index $index'),
                              );
                            },
                            childCount: 10
                          ),
                        )
                       ]
                    ),
                    CustomScrollView(
                      slivers: [
                       SliverList(
                          delegate: SliverChildBuilderDelegate(
                            (context, index) {
                              return ListTile(
                                title: Text('index $index'),
                              );
                            },
                            childCount: 10
                          ),
                        )
                       ]
                    ),
                  ],
                ),
              ),
            ),
            Container(
              height: 30,
//               child: 
            )
          ],
        ),
      ),
    );
  }
}

【问题讨论】:

  • 你能发布一个最小的可重现代码吗?
  • 更新了代码,请看@iDecode

标签: flutter flutter-layout


【解决方案1】:

NestedScrollView 在这里搞砸了。由于您没有使用headerSliverBuilder,因此您可以完全删除该小部件。这是工作代码。

@override
Widget build(BuildContext context) {
  return Material(
    child: PageView(
      children: <Widget>[
        CustomScrollView(
          slivers: [
            SliverList(
              delegate: SliverChildBuilderDelegate(
                (context, index) => ListTile(title: Text('index $index')),
                childCount: 20,
              ),
            )
          ],
        ),
        CustomScrollView(
          slivers: [
            SliverList(
              delegate: SliverChildBuilderDelegate(
                (context, index) => ListTile(title: Text('index $index')),
                childCount: 20,
              ),
            )
          ],
        ),
      ],
    ),
  );
}

【讨论】:

  • 这确实有效,尽管我仍然没有得到这些小部件之间的手势注册竞赛 +1
  • @bihireboris 当您在NestedScrollView 中使用ListView 时,它会优先使用后者,这就是ListView 不滚动的原因。可能有解决方法可以做到这一点,但我没有自己尝试任何事情。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2022-01-16
  • 2017-12-29
  • 2019-06-17
  • 2023-03-25
  • 2021-08-24
  • 1970-01-01
  • 2020-02-13
相关资源
最近更新 更多