sliver 组件

sliver 组件

在开发过程中一定会遇到一个多种类型item的长滚动列表此时需要一个滚动的组件

1.CustomScrollView

CustomScrollView参数类型说明
physicsScrollPhysics滑动类型: BouncingScrollPhysics() 拉到最底部有回弹效 ClampingScrollPhysics() 包裹内容不会回弹 NeverScrollableScrollPhysics() 滑动禁止
primarybool当条目不足时 true可以滚动
cacheExtentint缓存条目(预加载条目)
scrollDirectionAxis滚动方向: Axis.vertical Axis. horizontal
sliversList子Widget
shrinkWrapbooltrue反向滑动AppBar

用法

  • slivers CustomScrollView构造函数给我们提供的Sliver是一个Widget[],但如果我们提供一个普通的Widget,是会崩溃的。普通组件需要由 SliverToBoxAdapter 去包装。 Widget[] 数组里面都是sliver 组件
   class CustomScrollView extends ScrollView {
  /// Creates a [ScrollView] that creates custom scroll effects using slivers.
  ///
  /// See the [ScrollView] constructor for more details on these arguments.
  const CustomScrollView({
    super.key,
    super.scrollDirection,
    super.reverse,
    super.controller,
    super.primary,
    super.physics,
    super.scrollBehavior,
    super.shrinkWrap,
    super.center,
    super.anchor,
    super.cacheExtent,
    this.slivers = const <Widget>[],
    super.semanticChildCount,
    super.dragStartBehavior,
    super.keyboardDismissBehavior,
    super.restorationId,
    super.clipBehavior,
  });

  /// The slivers to place inside the viewport.
  final List<Widget> slivers;

  @override
  List<Widget> buildSlivers(BuildContext context) => slivers;
}

2. sliver 组件

  • 列表类的sliver
Sliver组件名称功能对应的可滚动组件
SliverList列表ListView
SliverFixedExtentList高度固定的列表ListView,指定itemExtent时
SliverAnimatedList添加/删除列表项可以执行动画AnimatedList
SliverGrid网格GridView
SliverPrototypeExtentList根据原型生成高度固定的列表ListView,指定prototypeItem 时
SliverFillViewport包含多个子组件,每个都可以填满屏幕PageView
SliverFillRemaining填满剩余空间Scrollview
  • 布局和装饰用的sliver 组件
Sliver名称对应 RenderBox
SliverPaddingPadding
SliverVisibility、SliverOpacityVisibility、Opacity
SliverFadeTransitionFadeTransition
SliverLayoutBuilderLayoutBuilder
  • 其他常用的sliver 组件
Sliver名称说明
SliverAppBar对应 AppBar,主要是为了在 CustomScrollView 中使用。
SliverToBoxAdapter一个适配器,可以将 RenderBox 适配为 Sliver,后面介绍。
SliverPersistentHeader滑动到顶部时可以固定住,后面介绍。
  • 如果 CustomScrollView 有孩子也是一个完整的可滚动组件且它们的滑动方向一致,则 CustomScrollView 不能正常工作,要解决这个问题,可以使用 NestedScrollView
  • SliverPrototypeExtentList 用法

跟SliverList用法基本一致,但是子控件的高度是由prototypeItem的控件高度决定的。构造函数如下:

  const SliverPrototypeExtentList({
    super.key,
    required super.delegate,
    required this.prototypeItem,
  }) : assert(prototypeItem != null);
  • SliverAppBar 用法
const SliverAppBar({
    Key key,
    this.leading,//左侧的图标或文字,多为返回箭头
    this.automaticallyImplyLeading = true,//没有leading为true的时候,默认返回箭头,没有leading且为false,则显示title
    this.title,//标题
    this.actions,//标题右侧的操作
    this.flexibleSpace,//可以理解为SliverAppBar的背景内容区
    this.bottom,//SliverAppBar的底部区
    this.elevation,//阴影
    this.forceElevated = false,//是否显示阴影
    this.backgroundColor,//背景颜色
    this.brightness,//状态栏主题,默认Brightness.dark,可选参数light
    this.iconTheme,//SliverAppBar图标主题
    this.actionsIconTheme,//action图标主题
    this.textTheme,//文字主题
    this.primary = true,//是否显示在状态栏的下面,false就会占领状态栏的高度
    this.centerTitle,//标题是否居中显示
    this.titleSpacing = NavigationToolbar.kMiddleSpacing,//标题横向间距
    this.expandedHeight,//合并的高度,默认是状态栏的高度加AppBar的高度
    this.floating = false,//滑动时是否悬浮
    this.pinned = false,//标题栏是否固定
    this.snap = false,//配合floating使用
  })
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值