具有无限高度的 Flutter ListView 异常

Car*_*val 8 android dart flutter

我正在 Android Studio 上开发一个新的 Flutter 应用程序。我的应用程序的一页从Future. 有了未来的结果,我正在创建一个ListView.builder()

这是代码:

Widget encabezados(BuildContext context, AsyncSnapshot snapshot)
  {
    ListView(
      children: <Widget>[
        new Card(
          elevation: 3,
          child: Row(
            mainAxisAlignment: MainAxisAlignment.center,
            children: <Widget>[
              SizedBox(width: 0.0,),
              Category(
                backgroundColor: Colors.deepOrange,
                val: clientesacobrar.toString(),
                title: "Clientes a cobrar",
              ),
              SizedBox(width: 10.0,),
              Category(
                backgroundColor: Colors.blue,
                title: "Clientes cobrados",
                val: clientescobrados.toString(),
              ),
              SizedBox(width: 10.0,),
              Category(
                val: formatter.format(montoacobrar),
                //val: records.length.toString(),
                backgroundColor: Colors.green,
                title: "Monto a cobrar",
              )
            ],
          ),
        ),
        new ListView(
          shrinkWrap: true,
          scrollDirection: Axis.vertical,

          //elevation: 3,
            children: <Widget>[
              ListView.builder(

                  scrollDirection: Axis.vertical,
                  shrinkWrap: true,
                  //ShrinkWrappingViewport: true,
                  itemCount: records.length,
                  itemBuilder: (BuildContext context, int index) {
                    return records.isNotEmpty
                        ? Card(
                        child: Padding (
                          padding: const EdgeInsets.all(16.00),
                          child: Text(records[index].nombre),
                        )
                    )
                        : CircularProgressIndicator();
                  }
              )
            ],
        )
      ],
    );


  }
Run Code Online (Sandbox Code Playgroud)

正如您在 listviews 小部件中看到的,我使用的是shrinkWrapset to true,因为如果我不设置它,我会得到:


I/flutter (22634): The following assertion was thrown during performResize():
I/flutter (22634): Vertical viewport was given unbounded height.
I/flutter (22634): Viewports expand in the scrolling direction to fill their container.In this case, a vertical
I/flutter (22634): viewport was given an unlimited amount of vertical space in which to expand. This situation
I/flutter (22634): typically happens when a scrollable widget is nested inside another scrollable widget.
I/flutter (22634): If this widget is always nested in a scrollable widget there is no need to use a viewport because
I/flutter (22634): there will always be enough vertical space for the children. In this case, consider using a Column
I/flutter (22634): instead. Otherwise, consider using the "shrinkWrap" property (or a ShrinkWrappingViewport) to size
I/flutter (22634): the height of the viewport to the sum of the heights of its children.

Run Code Online (Sandbox Code Playgroud)

但是,如果我设置了该shrinkwrap属性,我将无法在我的应用程序上滚动页面

我曾尝试将其ListView.builder()放入:容器小部件、列小部件、ListView小部件,但ListView.builder()始终要求我设置该shrinkwrap属性。

Mig*_*ivo 18

您需要约束您的父小部件,以便它知道边界在哪里。

对于这种情况,Container单独包装在 a 中不会产生任何效果,除非您明确限制它(例如,通过设置限制或只给它一个明确的高度)。

Container(
    height: 200.0, // or whatever
    child: ListView(
  ....
  )
)
Run Code Online (Sandbox Code Playgroud)

作为替代方案,您还可以将内部ListView物理设置为NeverScrollableScrollPhysics()防止在内部发生任何滚动,并且只有顶部滚动视图将控制滚动。

如果所有这些都没有帮助,您可能需要使用 aCustomScrollView来嵌套多个滚动视图,基本上是Sliver.


Lev*_*GLU 18

尝试将物理:NeverScrollableScrollPhysics()添加到内部ListView。