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

如何在CustomScrollView中让Widget占满垂直空间并实现垂直居中

要实现CustomScrollView内部组件占满垂直空间并完成垂直居中,按如下方案修改即可:

核心思路

CustomScrollView的Sliver类子组件默认仅适配自身内容高度,要让内容占满全部垂直高度,需使用SliverFillRemaining组件承载需要撑满高度的内容,它会自动填充CustomScrollView视口内剩余的全部垂直空间。

修改后的代码

Expanded(
  // 不需要整个CustomScrollView水平居中的话可以移除外层Center
  child: Center(
    child: CustomScrollView(
      // 不需要滚动效果可添加下面这行配置
      // physics: const NeverScrollableScrollPhysics(),
      slivers: [
        // 顶部固定高度留白用SliverToBoxAdapter承载
        SliverToBoxAdapter(
          child: SizedBox(
            height: 20.h,
          ),
        ),
        // 填充剩余全部垂直空间
        SliverFillRemaining(
          child: Stack(
            children: [
              InkWell(
                onTap: () {
                  print("test");
                  Get.toNamed(home);
                },
                child: RotatedBox(
                  quarterTurns: 3,
                  child: Text(
                    "Projects",
                    style: themeData.textTheme.headline1,
                  ),
                ),
              ),
              // 套Center实现组件垂直+水平居中
              const Center(
                child: CustomAboutWidget()
              ),
            ],
          ),
        )
      ],
    ),
  ),
)

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.09.25 00:24:03