Flutter - Listview.builder inside another Listview

元气小坏坏 提交于 2020-02-27 04:55:06

问题


I want my screen to be scrollable so I put everything in a Listview.

Now I want to show another Listview inside to show details of a List. When I try this an error is thrown - " Expanded widgets must be placed inside Flex widgets."


回答1:


Add shrinkwrap: true in listview.builder & Remove the top most container or replace it with column.




回答2:


Adding shrinkWrap: true, physics: ScrollPhysics(), inside the listview.builder, in this case the listview.builder need an expanded parent. The physics: ScrollPhysics() will allow it to maintain its state without scrolling back to the first items. Also, you can use physics: NeverScrollableScrollPhysics(), if you don't want the listview.builder to be scrolled by the user.




回答3:


I had this issue when I used two ListViews, one inside another. Nothing has worked for me except this workaround.

In the nested Listview, cover it with a ConstrainedBox and give it some large height. and use 'shrinkWrap: true' both ListViews. Since the shrinkwrap will trim the extra space, that extra height won't be an issue.

Flexible(
  child: ListView(
    children: <Widget>[
      //other Widgets here ...
      ConstrainedBox(
        constraints: BoxConstraints(maxHeight: 1000), // **THIS is the important part**
        child: ListView.builder(
          shrinkWrap: true,
          itemBuilder: (context, index) => _buildRow(index),
          itemCount: _elements.length,
        ),
      ),
    ],
  ),
),



回答4:


I want my screen to be scrollable so I put everything in a Listview.

I think you should use a CustomScrollView with slivers instead.

If it's the first time your hear about slivers, or if they seem a little bit scary, I suggest you to read this excellent article written by Emily Fortuna.

In your case, I would do something like this:

return CustomScrollView(
  slivers: <Widget>[
    SliverToBoxAdapter(
      // Put here all widgets that are not slivers.
      child: Container(),
    ),
    // Replace your ListView.builder with this:
    SliverList(
      delegate: SliverChildBuilderDelegate(
        (BuildContext context, int index) {
          return ListTile();
        },
      ),
    ),
  ],
);


来源:https://stackoverflow.com/questions/53465394/flutter-listview-builder-inside-another-listview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!