【问题标题】:How to show the listview in a listview如何在列表视图中显示列表视图
【发布时间】:2019-10-17 20:15:03
【问题描述】:

当我尝试在列表视图中显示列表视图时遇到问题。现在它向我显示错误“垂直视口被赋予了无限的高度。”。我尝试添加一个容器并将高度设置为第二个列表视图,它可以工作。但我不想限制第二个列表视图的高度。

return new Scaffold(
    body: new Column(
        children: <Widget> [
          new Expanded(
            child: ListView(
              children: <Widget> [
                new Container(), // something else
                new ListView.builder(
                    physics: NeverScrollableScrollPhysics(),
                    itemBuilder: (context, index) => new MenuCard(menuStore[index]),
                    itemCount: menuStore.length,
                    padding: new EdgeInsets.symmetric(vertical: 16.0)
                )
              ]
            )

          )
        ]
    )
);

【问题讨论】:

    标签: flutter flutter-listview


    【解决方案1】:

    您可能希望将ListView 属性shrinkWrap 设置为true

      /// Whether the extent of the scroll view in the [scrollDirection] should be
      /// determined by the contents being viewed.
      ///
      /// If the scroll view does not shrink wrap, then the scroll view will expand
      /// to the maximum allowed size in the [scrollDirection]. If the scroll view
      /// has unbounded constraints in the [scrollDirection], then [shrinkWrap] must
      /// be true.
    
    ...
    
      /// Defaults to false.
      final bool shrinkWrap;
    

    【讨论】:

      【解决方案2】:

      为了修复代码中的给定错误,您需要在 ListView.builderListView 中使用 shrinkWrap: true。

       return new Scaffold(
              body: new Column(
                  children: <Widget> [
                    new Expanded(
                      child: ListView(
                        shrinkWrap:ture,
                        children: <Widget> [
                          new Container(), // something else
                          new ListView.builder(
                              shrinkWrap:ture,
                              physics: NeverScrollableScrollPhysics(),
                              itemBuilder: (context, index) => new MenuCard(menuStore[index]),
                              itemCount: menuStore.length,
                              padding: new EdgeInsets.symmetric(vertical: 16.0)
                          )
                        ]
                      )
      
                    )
                  ]
              )
          );
      

      【讨论】:

        【解决方案3】:

        在此处尝试此答案,您可以使用 ListView 代替 GridView,还可以从 GridView 中删除 crossAxisCount 属性。 p>

        https://stackoverflow.com/a/53852317/3811162

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2020-12-30
          • 2023-01-04
          • 1970-01-01
          • 1970-01-01
          • 2013-05-18
          • 1970-01-01
          相关资源
          最近更新 更多