如何在颤动的列表视图中嵌套列表视图?

Posted

技术标签:

【中文标题】如何在颤动的列表视图中嵌套列表视图?【英文标题】:How to nest listview in listview in flutter? 【发布时间】:2020-11-20 17:19:31 【问题描述】:

我正在尝试按照以下代码将 ListView 嵌套在 Listview 中,顶部的 Listview 水平滚动,嵌套的 ListView 垂直滚动。对于嵌套的 ListView 我面临的错误

视口在横轴上展开以填充其容器并 约束他们的孩子以匹配他们在横轴上的范围。在 在这种情况下,垂直视口被赋予了无限量的 要扩展的水平空间。

我应该如何解决它,以便嵌套视图也显示在下面的代码中?

new Container(
        child: new ListView(
          
          scrollDirection: Axis.horizontal,
           children: <Widget>[
            ListView(
              

              scrollDirection:Axis.vertical,
              children: <Widget>[new Container(
                padding: EdgeInsets.fromLTRB(10, 20,10, 0),
                
                width: screenSize.width,
                child: new Column(
                  children: <Widget>[
                    Column(children: <Widget>[
                      Row(children: <Widget>[
                        Text('Text1'),
                        Text('Text1'),
                      ],),
                      Row(children: <Widget>[
                        Text('Text1'),
                        Text('Text1'),
                      ],),

                    ],),
                    
                    new Container(
                     //ERROR POINT
                      child: new ListView(
                        scrollDirection: Axis.vertical,
                        shrinkWrap: true,
                        children: <Widget>[
                          new Container(
                            color: Colors.red,
                            width: screenSize.width,
                             child: new Center(
                              child: new Text(
                                'RED',
                                style: new TextStyle(
                                    fontSize: 40.0,
                                    color: Colors.white
                                ),
                              ),
                            ),
                          ),
                          new Container(
                            color: Colors.blue,
                            width: screenSize.width,
                            child: new Center(
                              child: new Text(
                                'BLUE',
                                style: new TextStyle(
                                    fontSize: 40.0,
                                    color: Colors.white
                                ),
                              ),
                            ),
                          ),
                          new Container(
                            color: Colors.orange,
                            width: screenSize.width,
                            child: new Center(
                              child: new Text(
                                'ORANGE',
                                style: new TextStyle(
                                    fontSize: 40.0,
                                    color: Colors.white
                                ),
                              ),
                            ),
                          )
                        ],
                      ),
                    )
                  ],
                ),
              ),],
            ),
            new Container(
              color: Colors.blue,
              width: screenSize.width,
              child: new Center(
                child: new Text(
                  'BLUE',
                  style: new TextStyle(
                      fontSize: 40.0,
                      color: Colors.white
                  ),
                ),
              ),
            ),
            new Container(
              color: Colors.orange,
              width: screenSize.width,
              child: new Center(
                child: new Text(
                  'ORANGE',
                  style: new TextStyle(
                      fontSize: 40.0,
                      color: Colors.white
                  ),
                ),
              ),
            )
          ],
        ),
      )

【问题讨论】:

【参考方案1】:

不要用空的Container 包装小部件

对于嵌套的ListView,您必须通过SizedBoxContainer 限制其宽度,如下所示:

ListView(
  scrollDirection: Axis.horizontal,
  children: [
    SizedBox(
      child: ListView(
        children: [
          Text('data'),
          Text('data'),
          Text('data'),
        ],
      ),
      width: 300,
    ),
    SizedBox(
      child: ListView(
        children: [
          Text('data'),
          Text('data'),
          Text('data'),
        ],
      ),
      width: 300,
    ),
    SizedBox(
      child: ListView(
        children: [
          Text('data'),
          Text('data'),
          Text('data'),
        ],
      ),
      width: 300,
    ),
  ],
)

如果您想保留所有嵌套滚动视图的位置,您可以将它们包装在 SingleChildScrollViewRow 中,但您的决定似乎不太正确

SingleChildScrollView(
    scrollDirection: Axis.horizontal,
    child: Row(
      children: [
        SizedBox(
          width: 200,
          child: ListView.builder(
            itemBuilder: (context, index) => Text('data $index'),
            itemCount: 200,
          ),
        ),
        SizedBox(
          width: 200,
          child: ListView.builder(
            itemBuilder: (context, index) => Text('data $index'),
            itemCount: 200,
          ),
        ),
        SizedBox(
          width: 300,
          child: ListView.builder(
            itemBuilder: (context, index) => Text('data $index'),
            itemCount: 200,
          ),
        ),
        SizedBox(
          width: 300,
          child: ListView.builder(
            itemBuilder: (context, index) => Text('data $index'),
            itemCount: 200,
          ),
        ),
        SizedBox(
          width: 300,
          child: ListView.builder(itemBuilder: (context, index) => Text('data $index'), itemCount: 200),
        ),
        SizedBox(
          width: 300,
          child: ListView.builder(itemBuilder: (context, index) => Text('data $index'), itemCount: 200),
        ),
      ],
    ),
  )

【讨论】:

感谢 Andrey 的指导,我面临的另一个问题是,当我滚动列表时,它没有保持该位置并回滚到列表顶部,我该如何解决? 你确定要使用这种奇怪的结构吗?

以上是关于如何在颤动的列表视图中嵌套列表视图?的主要内容,如果未能解决你的问题,请参考以下文章

以编程方式防止滚动列表视图颤动

如何遍历地图列表并在颤动列表视图中显示文本小部件?

如何在颤动中将格式从列表视图更改为网格视图?

如何在颤动中创建加载更多列表视图

自定义滚动谷歌颤动中的水平列表视图

在 ListView 中,向下滚动时,在颤动中丢失数据