ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

flutter 常见组件的特殊用法 —— AppBar

2022-08-13 04:00:22  阅读:265  来源: 互联网

标签:toolbarHeight bottomHeight preferredSize AppBar 高度 组件 flutter Size


AppBar 的高度与 PreferredSizeWidget

通常可以观察到 Scaffold.appBar 与 AppBar.bottom 属性,要求其值必须是 PreferredSizeWidget(典型的是 AppBar 与 TabBar 组件)。

abstract class PreferredSizeWidget implements Widget {
  Size get preferredSize;
}

① 那么 AppBar 是怎么实现 PreferredSizeWidget 的呢?

  AppBar({
    Key? key,
    this.leading,
    this.automaticallyImplyLeading = true,
    this.title,
    //...
  }) : assert(automaticallyImplyLeading != null),
       //...
       // 这里即是实现
       preferredSize = _PreferredAppBarSize(toolbarHeight, bottom?.preferredSize.height),
       super(key: key);

class _PreferredAppBarSize extends Size {
  // kToolbarHeight 值是 56.0
  _PreferredAppBarSize(this.toolbarHeight, this.bottomHeight)
    : super.fromHeight((toolbarHeight ?? kToolbarHeight) + (bottomHeight ?? 0));

  final double? toolbarHeight;
  final double? bottomHeight;
}

可以看到 AppBar 的默认高度由 toolbarHeight(56.0) + bottomHeight 组成。

② 由于 bottom 组件通常是 TabBar,那么 TabBar 的默认高度是多少呢?

  @override
  Size get preferredSize {
    double maxHeight = _kTabHeight;
    for (final Widget item in tabs) {
      if (item is PreferredSizeWidget) {
        final double itemHeight = item.preferredSize.height;
        maxHeight = math.max(itemHeight, maxHeight);
      }
    }
    // indicatorWeight 默认高度是 2
    return Size.fromHeight(maxHeight + indicatorWeight);
  }

可以看到高度是 2 + tabs 的最大高度。

③ tabs 组件基础组件通常是 Tab,那么 Tab 的默认高度是多少呢?

  @override
  Size get preferredSize {
    if (height != null)
      return Size.fromHeight(height!);
    else if ((text != null || child != null) && icon != null)
      // _kTextAndIconTabHeight 是 72
      return const Size.fromHeight(_kTextAndIconTabHeight);
    else
      // _kTabHeight 是 46
      return const Size.fromHeight(_kTabHeight);
  }

即Tab有文字和图标时是 72,只有文字或图标时为 46。

④ 总结:AppBar 的高度是 104(56 + 48),其中 toolbarHeight 高度是 56(kToolbarHeight),bottom 组件 TabBar 组件高度通常是 48(46+2)(kTextTabBarHeight)。

特殊属性说明

primary: true

由前面的说明可知 AppBar 的高度由 toolbarHeight + bottomHeight 组成。默认情况下,该属性为 true,即总高度由 statusBar + toolbarHeight + bottomHeight 组成。

flexibleSpace 的布局不受此属性影响

标签:toolbarHeight,bottomHeight,preferredSize,AppBar,高度,组件,flutter,Size
来源: https://www.cnblogs.com/lemos/p/16581874.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有