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

Flutter中顶部Container内文本不显示问题求助

Flutter容器文本不显示问题解决办法

问题出在你给Container设置的参数冲突:

  • 容器高度仅设为140,但顶部内边距却设了150,这会把文本直接推到容器外部,导致看不到内容。

解决办法很简单,二选一即可:

  1. 移除或调小顶部内边距,比如改成全局内边距:
    padding: const EdgeInsets.all(16),
    
  2. 如果需要保留内边距布局,把容器高度调大,确保高度大于内边距+文本高度的总和,比如设为200。

修改后的完整示例代码:

body: ListView(
    children: [
      //Container 1
      GestureDetector(
        onTap: () {
          Navigator.of(context).push(
              MaterialPageRoute(builder: (context) => RememberingPage()));
        },
        child: Container(
          height: 140,
          width: 500,
          padding: const EdgeInsets.all(16), // 替换过大的顶部内边距
          margin: const EdgeInsets.only(top: 100, left: 25, right: 25),
          decoration: BoxDecoration(
            color: Theme.of(context).colorScheme.primary,
            borderRadius: BorderRadius.circular(15),
            boxShadow: const [
              BoxShadow(
                color: Color.fromARGB(63, 0, 0, 0),
                blurRadius: 4.0,
                spreadRadius: -5.0,
                offset: Offset(
                  0.0,
                  8.0,
                ),
              ),
            ],
          ),
          child: Align(
            alignment: Alignment.center,
            child: Text(
              "Remembering",
              style: TextStyle(
                color: Theme.of(context).colorScheme.onPrimary,
                fontSize: 20,
              ),
              
            ),
          ),
        ),
      ),

这样修改后,文本就能在蓝色容器的居中位置正常显示了。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.02 05:33:24