首页 > 其他分享 >flutter 常见组件的特殊用法 —— SliverAppBar

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

时间:2022-08-14 14:06:16浏览次数:42  
标签:widget expandedHeight collapsedHeight 高度 primary SliverAppBar 组件 topPadding flut

SliverAppBar 的组成

特殊属性说明

primary: true

不同于 AppBar 通常有 Scaffold 包裹,其最大高度由父类约束。SliverAppBar 完全由自身决定。
当 primary 等于 true 时,其 topPadding 等于状态栏高度;若为 false,则 topPadding 等于 0,并且整体高度也会缩小(减去状态栏高度)。

collapsedHeight

最小高度由3部分组成:状态栏高度 + 传入的 collapsedHeight 高度 + 底栏高度。

当 collapsedHeight 为空时,默认等于工具栏高度,除非当前配置为 固定、悬浮并且底栏不为空,则默认等于 0。

相关源码:

final double collapsedHeight = (widget.pinned && widget.floating && widget.bottom != null)
    ? (widget.collapsedHeight ?? 0.0) + bottomHeight + topPadding
    : (widget.collapsedHeight ?? widget.toolbarHeight) + bottomHeight + topPadding;

//...

@override
double get minExtent => collapsedHeight;

expandedHeight

最大高度,其值最小为 collapsedHeight,否则等于 状态栏高度 + 传入的 expandedHeight 高度 + 底栏高度。

当 expandedHeight 为空时,默认等于工具栏高度。

相关源码:

  @override
  double get maxExtent => math.max(topPadding + (expandedHeight ?? (toolbarHeight ?? kToolbarHeight) + _bottomHeight), minExtent);

源码

整体简单看下源码:

@override
Widget build(BuildContext context) {
  assert(!widget.primary || debugCheckHasMediaQuery(context));
  final double bottomHeight = widget.bottom?.preferredSize.height ?? 0.0;
  // 若 primary 属性为 false,则 topPadding 等于 0,且整体高度缩小。
  // 注:跟 AppBar 不同,AppBar 外层由 Scaffold 包裹,其最大高度没有改变,只是整体向上移了。
  final double topPadding = widget.primary ? MediaQuery.of(context).padding.top : 0.0;
  // 最小高度 minExtent = 状态栏高度 + 传入的 collapsedHeight 高度 + 底栏高度
  final double collapsedHeight = (widget.pinned && widget.floating && widget.bottom != null)
      ? (widget.collapsedHeight ?? 0.0) + bottomHeight + topPadding
      : (widget.collapsedHeight ?? widget.toolbarHeight) + bottomHeight + topPadding;

  return MediaQuery.removePadding(
    context: context,
    removeBottom: true,
    // SliverPersistentHeader 用于实现固定、悬浮等效果
    child: SliverPersistentHeader(
      floating: widget.floating,
      pinned: widget.pinned,
      delegate: _SliverAppBarDelegate(
        vsync: this,
        leading: widget.leading,
        automaticallyImplyLeading: widget.automaticallyImplyLeading,
        title: widget.title,
        actions: widget.actions,
        flexibleSpace: widget.flexibleSpace,
        bottom: widget.bottom,
        elevation: widget.elevation,
        scrolledUnderElevation: widget.scrolledUnderElevation,
        shadowColor: widget.shadowColor,
        surfaceTintColor: widget.surfaceTintColor,
        forceElevated: widget.forceElevated,
        backgroundColor: widget.backgroundColor,
        foregroundColor: widget.foregroundColor,
        brightness: widget.brightness,
        iconTheme: widget.iconTheme,
        actionsIconTheme: widget.actionsIconTheme,
        textTheme: widget.textTheme,
        primary: widget.primary,
        centerTitle: widget.centerTitle,
        excludeHeaderSemantics: widget.excludeHeaderSemantics,
        titleSpacing: widget.titleSpacing,
        expandedHeight: widget.expandedHeight,
        collapsedHeight: collapsedHeight,
        topPadding: topPadding,
        floating: widget.floating,
        pinned: widget.pinned,
        shape: widget.shape,
        snapConfiguration: _snapConfiguration,
        stretchConfiguration: _stretchConfiguration,
        showOnScreenConfiguration: _showOnScreenConfiguration,
        toolbarHeight: widget.toolbarHeight,
        leadingWidth: widget.leadingWidth,
        backwardsCompatibility: widget.backwardsCompatibility,
        toolbarTextStyle: widget.toolbarTextStyle,
        titleTextStyle: widget.titleTextStyle,
        systemOverlayStyle: widget.systemOverlayStyle,
      ),
    ),
  );
}

标签:widget,expandedHeight,collapsedHeight,高度,primary,SliverAppBar,组件,topPadding,flut
From: https://www.cnblogs.com/lemos/p/16585038.html

相关文章

  • vue 父子组件传值方法总结
    1、父组件向子组件传参:(1)父组件直接绑定在子组件的标签上,子组件通过props接收传递过来的参数。(2)父组件主动获取所有的参数和方法this.$refs.childrenName.(参数或方法名)......
  • Spring MVC组件之HandlerMapping
    SpringMVC组件之HandlerMappingHandlerMapping概述HandlerMapping组件的作用解析一个个Request请求,并找到相应处理这个Request的Handler。Handler一般可以理解为Control......
  • 自定义组件⑦插槽-微信小程序开发(二十四)
    1.什么是插槽在自定义组件的wxml结构中,可以提供一个节点(插槽),用于承载组件使用者提供的wxml结构。2.单个插槽在小程序中,默认每个自定义组件中只允许使用一个......
  • 自定义组件⑤纯数据字段-微信小程序开发(二十二)
    1.什么是纯数据字段概念:纯数据字段指的是那些不用于界面渲染的data字段。应用场景:例如有些情况下,某些data中的字段既不会展示在界面上,也不会传递给其他组件,仅仅在当......
  • 自定义组件②样式-微信小程序开发(十九)
    1.组件样式隔离默认情况下,自定义组件的样式只对当前组件生效,不会影响到组件之外的UI结构,如图所示:⚫组件A的样式不会影响组件C的样式⚫组件A的样式不会影响小......
  • flutter 常见组件的特殊用法 —— AppBar
    AppBar的高度与PreferredSizeWidget通常可以观察到Scaffold.appBar与AppBar.bottom属性,要求其值必须是PreferredSizeWidget(典型的是AppBar与TabBar组件)。abst......
  • flutter 效果实现 —— 无AppBar下列表滚动时状态栏透明度变化
    效果此效果参考自twitter与gmail实现原理通过Stack组件,在最上层页面的状态栏位置用一个白色的容器占位,在列表滚动时,根据监听到的滚动位置动态调整其透明度。代码......