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

Flutter中标题为何显示在状态栏?如何将标题置于ListView头部

解决方法

将标题组件直接作为ListView的首个子元素,让标题成为列表头部并随列表项一同滚动,同时避免布局溢出问题。

修改后的完整代码:

Widget build(BuildContext context) {
  return Scaffold(
    appBar: null,
    body: ListView(
      children: [
        // 标题作为ListView的头部元素
        ListTile(
          title: Text("chose your credentials",
              style: Theme.of(context).textTheme.bodySmall),
        ),
        // 展开API加载的列表项
        ...List.generate(_credentials.length, (index) {
          return Card(
            child: ListTile(
              title: Text(_credentials[index].title),
              trailing: Icon(Icons.star),
            ),
          );
        }),
      ],
    ),
    floatingActionButton: FloatingActionButton(
      onPressed: () {},
      child: const Text('ok'),
    ),
  );
}

说明

原代码用Column分离标题与ListView,会导致标题固定在顶部无法随列表滚动,且列表内容过多时易出现布局溢出。将标题整合到ListView的子元素列表中,既满足“标题位于ListView头部”的需求,又能让标题随列表滚动,同时借助ListView的滚动特性规避布局溢出问题。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.22 18:15:07