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

如何修改Flutter中BottomAppBar的高度?已用PreferredSize仍未生效

解决BottomAppBar用PreferredSize设置高度无效的问题

问题原因

BottomAppBar本身默认有固定最小高度(默认56),且内部文本组件的默认行高会撑高布局,导致PreferredSize的高度设置被覆盖,无法生效。

解决方案

方案1:用Container强制约束高度 + 调整内部文本样式

通过外层Container固定高度,同时限制内部文本的大小和布局,避免撑高组件:

bottomNavigationBar: PreferredSize(
  preferredSize: const Size.fromHeight(20),
  child: Container(
    height: 20,
    child: BottomAppBar(
      padding: EdgeInsets.zero,
      elevation: 0,
      color: Colors.blue,
      child: Row(
        children: [
          Expanded(
            child: Container(
              height: 20,
              alignment: Alignment.center,
              child: const Text('Add to cart', style: TextStyle(fontSize: 12)),
            ),
          ),
          Expanded(
            child: Container(
              height: 20,
              alignment: Alignment.center,
              child: const Text('Buy Now', style: TextStyle(fontSize: 12)),
            ),
          ),
        ],
      ),
    ),
  ),
),

方案2:直接自定义PreferredSizeWidget(替代BottomAppBar)

如果不需要BottomAppBar的特殊特性,直接用Container实现布局,更易精准控制高度:

bottomNavigationBar: PreferredSize(
  preferredSize: const Size.fromHeight(20),
  child: Container(
    height: 20,
    color: Colors.blue,
    child: Row(
      children: [
        Expanded(
          child: Center(
            child: const Text('Add to cart', style: TextStyle(fontSize: 12, color: Colors.white)),
          ),
        ),
        Expanded(
          child: Center(
            child: const Text('Buy Now', style: TextStyle(fontSize: 12, color: Colors.white)),
          ),
        ),
      ],
    ),
  ),
),

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.15 16:20:06