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

Flutter页面无内容显示问题:代码错误定位求助

问题分析与解决

错误原因

你的代码里Expanded的位置不正确:Expanded组件必须直接作为Column/Row/Flex的子节点,它的作用是占据父组件的剩余可用空间。但你现在把它嵌套在Center→Container的层级里,导致Expanded无法从外层Column获取空间信息,最终ListView没有足够的高度渲染内容,页面就显示空白了。

修正后的代码

把Expanded移到Center外面,作为Column的直接子组件:

body: Column(
  children: [
    Expanded( // 移至此处,作为Column的直接子节点
      child: Center(
        child: Container(
          color: Color.fromARGB(255, 234, 237, 245),
          child: ListView.separated(
            separatorBuilder: (context, index) => SizedBox(height: 2),
            controller: scrollController,
            itemCount: 2,
            itemBuilder: (context, index) {
              return ListTile(
                onTap: () {
                  Navigator.pushNamed(context, "/seconnecter");
                },
                title: Text('hello'),
              );
            },
          ),
        ),
      ),
    ),
  ],
),

包含Column和Row的页面实现示例

如果要在页面中同时使用Column和Row布局(比如顶部横向栏+列表内容),可以参考以下代码:

body: Column(
  children: [
    // 顶部横向Row组件
    Row(
      mainAxisAlignment: MainAxisAlignment.spaceBetween,
      children: [
        Padding(
          padding: EdgeInsets.all(16),
          child: Text('页面标题'),
        ),
        IconButton(icon: Icon(Icons.more_vert), onPressed: () {}),
      ],
    ),
    // 占据剩余空间的列表区域
    Expanded(
      child: Container(
        color: Color.fromARGB(255, 234, 237, 245),
        child: ListView.separated(
          separatorBuilder: (context, index) => SizedBox(height: 2),
          controller: scrollController,
          itemCount: 5,
          itemBuilder: (context, index) {
            // ListTile内部嵌套Row实现横向内容布局
            return ListTile(
              onTap: () {
                Navigator.pushNamed(context, "/seconnecter");
              },
              title: Row(
                children: [
                  Text('列表项 $index'),
                  Spacer(),
                  Text('右侧内容'),
                ],
              ),
            );
          },
        ),
      ),
    ),
  ],
),

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.13 14:30:39