You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Flutter垂直视口错误:已尝试ShrinkWrap等方案仍未解决

问题解决:Vertical viewport 无限宽度错误

错误原因

你的代码存在两个核心问题:

  1. Expanded组件误用:Expanded只能在Row/Column这类Flex布局容器内使用,直接在Container中嵌套Expanded会破坏布局约束逻辑,导致子组件无法正确计算尺寸。
  2. 垂直ListView缺少水平约束:当设置shrinkWrap: true时,垂直滚动的ListView需要明确的水平宽度限制,如果父容器未提供有限宽度(比如放在无约束的容器中),就会触发"unbounded width"错误。

修复后的代码

ListView.builder(
  itemCount: customerList.length,
  shrinkWrap: true,
  scrollDirection: Axis.vertical,
  itemBuilder: (BuildContext context, int index) {
    return ListTile(
      title: Text(customerList[index].customerName),
      subtitle: Column(
        crossAxisAlignment: CrossAxisAlignment.start, // 可选优化:让文本左对齐更贴合ListTile样式
        children: [
          Text(customerList[index].customerID),
          Text(customerList[index].customerAddress),
        ],
      ),
    );
  },
),

额外适配场景

如果你的ListView是放在Row或其他无水平约束的容器中,需要给ListView外层添加宽度约束,比如用Expanded包裹整个ListView(注意是包裹ListView本身,而非item内的组件):

Row(
  children: [
    Expanded( // 为ListView提供水平方向的约束
      child: ListView.builder(
        // 你的ListView配置参数
      ),
    ),
  ],
)

内容的提问来源于stack exchange,提问作者Syed Mujtaba Ali

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.28 17:22:42