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

Flutter自定义AppBar中TabBar作为bottom时与Toolbar重叠/缩小问题

问题原因

你的CustomAppBar实现PreferredSizeWidget时,preferredSize固定返回了kToolbarHeight,没有将传入的bottom组件的高度纳入计算。当添加TabBar作为bottom时,AppBar本身会尝试容纳TabBar,但CustomAppBar告诉父组件它的总高度只有工具栏高度,导致父容器分配的空间不足,进而出现TabBar与Toolbar重叠、Toolbar被压缩的问题。

解决方案

修改preferredSize的计算逻辑,将bottom组件的高度(如果存在的话)加到基础工具栏高度上:

class CustomAppBar extends StatefulWidget implements PreferredSizeWidget {
  final String title;
  final Widget? leading;
  final List<Widget>? actions;
  final PreferredSizeWidget? bottom;

  const CustomAppBar(this.title,
      {this.leading, this.actions, this.bottom, super.key});

  @override
  Size get preferredSize => Size.fromHeight(
        kToolbarHeight + (widget.bottom?.preferredSize.height ?? 0.0),
      );

  @override
  State<CustomAppBar> createState() => _CustomAppBarState();
}

class _CustomAppBarState extends State<CustomAppBar> {
  @override
  Widget build(BuildContext context) {
    return AppBar(
        title: Text(widget.title),
        centerTitle: true,
        elevation: 0,
        leading: widget.leading,
        actions: widget.actions,
        bottom: widget.bottom);
  }
}
关键说明
  • 当bottom不为null时,preferredSize会将kToolbarHeight和bottom.preferredSize.height相加,确保CustomAppBar的总高度足够容纳工具栏和底部组件。
  • 你之前修改toolbarHeight无效,是因为问题根源在CustomAppBar对外暴露的整体尺寸错误,而非AppBar内部的工具栏高度设置。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.03 16:32:17