解决方法一:

添加以下两行:

ListView.builder(
    scrollDirection: Axis.vertical,
    shrinkWrap: true,
...

In this code snippet, have a ListView.builder with the following properties:

scrollDirection: Axis.vertical: This property specifies the scrolling direction of the list. In this case, it is set to Axis.vertical, which means the list will scroll vertically. You can also set it to Axis.horizontal if you want the list to scroll horizontally.

shrinkWrap: true:
使用自己需要的空间
By default, a ListView takes up as much space as it can along its main axis. However, when you set shrinkWrap to true, the ListView will try to wrap its content and only take up as much space as needed. This can be helpful when you want to include a ListView inside another scrollable view or when the size of the list is not predetermined

解决方法二:
307

This generally happens when you try to use a ListView/GridView inside a Column, there are many ways of solving it, I am listing few here.

Wrap ListView in Expanded
使用expanded

Column(
  children: <Widget>[
    Expanded( // wrap in Expanded
      child: ListView(...),
    ),
  ],
)

Wrap ListView in SizedBox and give a bounded height
指定高度

Column(
  children: <Widget>[
    SizedBox(
      height: 400, // fixed height
      child: ListView(...),
    ),
  ],
)

Use shrinkWrap: true in ListView.

Column(
  children: <Widget>[
    ListView(
      shrinkWrap: true, // use this
    ),
  ],
)

参考:

https://stackoverflow.com/questions/50252569/vertical-viewport-was-given-unbounded-height

Logo

开源鸿蒙跨平台开发社区汇聚开发者与厂商,共建“一次开发,多端部署”的开源生态,致力于降低跨端开发门槛,推动万物智联创新。

更多推荐