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

Flutter/Dart:如何在现有ListView下方添加另一个ListView

在Flutter页面中添加第二个ListView的实现方案

直接在现有ListView中嵌套新的ListView会引发滚动冲突,你需要给内部的ListView设置两个关键属性来解决这个问题:

  • shrinkWrap: true:让ListView根据子项内容自动计算高度,而非占用整个视口
  • physics: NeverScrollableScrollPhysics():禁用内部ListView的独立滚动,让页面的主ListView统一处理滚动逻辑

以下是修改后的完整代码示例,我在原页面的末尾添加了一个展示账单明细的ListView:

return Scaffold(
  appBar: AppBar(title: Text('detailsPage')),
  body: ListView(
    children: [
      Card(
        child: ListTile(
          title: Center(child: Text(utf8.decode(appController.userName.runes.toList()) + " " + utf8.decode(appController.userSurname.runes.toList()))),
          subtitle: Center(child: Text('UserID: '+appController.userid.toString())),
        ),
      ),
      Card(
        child: ListTile(
          title: Center(child: Text(months[index])),
          subtitle: Center(child: Text("This month you have done "+appController.Totaleachlength[index].toString()+' charges')),
        ),
      ),
      Card(
        child: ListTile(
          title: Center(child: Text(appController.Totaleachlist[index].toStringAsFixed(3)+"€")),
          subtitle: Center(child: Text("Total amount")),
        ),
      ),
      ElevatedButton(
        child: Text('Download Bill pdf'),
        onPressed: () => '',
      ),
      ListTile(
        title: Center(child: Text('Details of your charges')),
      ),
      // 添加的第二个ListView
      ListView.builder(
        shrinkWrap: true,
        physics: NeverScrollableScrollPhysics(),
        itemCount: appController.chargesList.length, // 替换为你的账单明细数据源长度
        itemBuilder: (context, idx) {
          return Card(
            child: ListTile(
              title: Text(appController.chargesList[idx].description),
              subtitle: Text(appController.chargesList[idx].amount.toStringAsFixed(3)+"€"),
              trailing: Text(appController.chargesList[idx].date),
            ),
          );
        },
      ),
    ],
    shrinkWrap: true,
  ),
);

补充说明:

  • 如果你使用的是普通ListView而非ListView.builder,同样需要添加shrinkWrap: true和physics: NeverScrollableScrollPhysics()这两个属性
  • 示例中的chargesList需要替换为你实际业务中的账单明细数据源

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.15 15:45:43