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

如何实现Flutter中Scaffold的滚动效果?嵌套列表后消失求助

问题解决:SingleChildScrollView包裹ListView后列表消失

你的问题出在ListView本身是可滚动组件,和SingleChildScrollView嵌套时,ListView无法确定自身高度,直接收缩成0导致看不见。给你两种解决思路:

思路一:直接用ListView(推荐)

ListView本身支持滚动,不需要额外套SingleChildScrollView,直接把ListView作为body即可:

return Scaffold(
  body: ListView.builder(
    itemCount: subjects.length,
    itemBuilder: (context, i) {
      return singleSubject(
        subjects[i],
        () => deleteItem(i),
      );
    },
  ),
  floatingActionButton: FloatingActionButton(
    backgroundColor: const Color(0xFF383838),
    onPressed: newSubject,
    child: const Icon(Icons.add),
  ),
  backgroundColor: const Color(0xFFB94844),
);

思路二:必须嵌套时的处理

如果页面有其他需要滚动的内容(比如顶部固定栏+列表),非要嵌套的话,给ListView加两个属性:

  • shrinkWrap: true:让ListView根据内容自适应高度
  • physics: NeverScrollableScrollPhysics():禁用ListView自身的滚动,把滚动逻辑交给外层的SingleChildScrollView

修改后的代码:

return Scaffold(
  body: SingleChildScrollView(
    child: ListView.builder(
      shrinkWrap: true,
      physics: const NeverScrollableScrollPhysics(),
      itemCount: subjects.length,
      itemBuilder: (context, i) {
        return singleSubject(
          subjects[i],
          () => deleteItem(i),
        );
      },
    ),
  ),
  floatingActionButton: FloatingActionButton(
    backgroundColor: const Color(0xFF383838),
    onPressed: newSubject,
    child: const Icon(Icons.add),
  ),
  backgroundColor: const Color(0xFFB94844),
);

内容的提问来源于stack exchange,提问作者Vince

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.12 10:25:36