Flutter-另一个Listview中的Listview.builder

时间:2018-11-25 07:05:27

标签: android android-listview flutter

我希望屏幕可以滚动,所以我将所有内容都放在一个Listview中。

现在,我想在其中显示另一个Listview来显示列表的详细信息。当我尝试此操作时,将引发错误-“必须在Flex小部件内放置扩展的小部件。”

enter image description here

6 个答案:

答案 0 :(得分:7)

shrinkwrap: true中添加listview.builder并删除最上面的container或将其替换为column

答案 1 :(得分:3)

就我而言,在physics: ScrollPhysics(),中添加ListView.builder使ListView.builder可以滚动。

层次结构为 ListView > StreamBuilder > RefreshIndicator > ListView.builder

答案 2 :(得分:2)

在listview.builder中添加shrinkWrap: true, physics: ScrollPhysics(),,在这种情况下,listview.builder需要扩展的父级。 physics: ScrollPhysics()将允许其保持其状态,而无需滚动回到前几项。另外,如果您不希望用户滚动listview.builder,则可以使用physics: NeverScrollableScrollPhysics()

答案 3 :(得分:2)

我能够通过以下方法解决该问题:将主列包装在SingleChildScrollView中,然后将ListView.builder包装在指定容器高度的Container中,然后再次将该容器包装在SingleChildScrollView中。 我知道这很复杂,但是对我有用! 您可以通过代码清楚地看到图片。

 Scaffold(
    appBar: AppBar(
      centerTitle: true,
      backgroundColor: Colors.black,
      title: Text("Welcome, ${widget.myUser.name}"),
      actions: [
        InkWell(
          child: Container(
            alignment: Alignment.center,
            padding: EdgeInsets.only(right: 20),
            child: Icon(Icons.settings),
          ),
          onTap: () {},
        ),
      ],
    ),
    body: Padding(
      padding: EdgeInsets.symmetric(horizontal: 20),
      child: SingleChildScrollView(
        child: Column(
          mainAxisSize: MainAxisSize.max,
          children: [
            _customerDetailsWidget(),
            SizedBox(
              height: 15,
            ),
            Container(
              child: Divider(
                color: Colors.grey[500],
              ),
            ),
            SizedBox(
              height: 15,
            ),
            _addProductText(),
            SingleChildScrollView(
                child: Container(
                  height: 500,
                  child: ListView.builder(
                    itemCount:1,
                    itemBuilder: (BuildContext context, int index) {
                      return Container(child: Text("HELLO WORLD"));
                    },
                  ),
                ))
          ],
        ),
      ),
    ),
  ),

答案 4 :(得分:1)

当我使用两个 ListViews(一个在另一个内部)时遇到了这个问题。除此变通办法外,对我没有任何帮助。

在嵌套的Listview中,用ConstrainedBox覆盖它,并为其增加一些高度。并在两个ListView中都使用“ shrinkWrap:true”。由于收缩包装会修剪多余的空间,因此多余的高度不会成为问题。

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,
        ),
      ),
    ],
  ),
),

答案 5 :(得分:-1)

  

我希望屏幕可以滚动,所以我将所有内容都放在一个Listview中。

我认为您应该将CustomScrollView和条子一起使用。

如果这是您第一次听说银条,或者看起来有些吓人,我建议您阅读this excellent article wrote by Emily Fortuna

就您而言,我会做类似的事情:

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();
        },
      ),
    ),
  ],
);