SliverChildListDelegate

一个通过显式列表为slivers提供子组件的`delegate`

SliverChildListDelegate是Flutter中用于Sliver系列组件(如CustomScrollViewSliverListSliverGrid等)的一个委托类,用于提供子组件列表。它的作用类似于ListViewchildren参数,但专门为Sliver设计。

核心介绍

  • 接受一个固定的子组件列表(List<Widget>)
  • 不支持懒加载(所有子组件会一次性构建)
  • 适合子组件数量较少或需要精确控制布局的场景

示例

  1. 配合SliverList使用
CustomScrollView(
  slivers: [
    SliverList(
      delegate: SliverChildListDelegate(
        [
          Container(height: 50, color: Colors.red),
          Container(height: 50, color: Colors.green),
          Container(height: 50, color: Colors.blue)
        ]
      )
    )
  ]
)
  1. 动态生成子组件
SliverList(
  delegate: SliverChildListDelegate(
    List.generate(5, (index) {
      return ListTile(
        title: Text('Item $index')
      );
    })
  )
)

对比SliverChildBuilderDelegate

特性SliverChildListDelegateSliverChildBuilderDelegate
子组件来源固定列表动态构建(懒加载)
性能一次性构建所有子组件按需构建(适合长列表)
适用场景少量固定子组件大量或动态数据

注意事项:

  • 避免在长列表中使用: SliverChildListDelegate会一次性构建所有子组件,可能导致性能问题
  • 子组件数量固定: 无法像SliverChildBuilderDelegate那样动态调整数量

构造函数

SliverChildListDelegate.new(
  List<Widget> children, {
  bool addAutomaticKeepAlives = true, 
  bool addRepaintBoundaries = true, 
  bool addSemanticIndexes = true, 
  SemanticIndexCallback semanticIndexCallback = _kDefaultSemanticIndexCallback, 
  int semanticIndexOffset = 0
})


SliverChildListDelegate.fixed(
  List<Widget> children, {
  bool addAutomaticKeepAlives = true, 
  bool addRepaintBoundaries = true, 
  bool addSemanticIndexes = true, 
  SemanticIndexCallback semanticIndexCallback = _kDefaultSemanticIndexCallback, 
  int semanticIndexOffset = 0
})

属性

属性 / 参数名类型默认值作用说明
children 必填List<Widget>要显示的子组件列表。
addAutomaticKeepAlivesbooltrue是否给每个子节点自动包裹 AutomaticKeepAliveClientMixin,防止滚动出可视区域时被销毁。
addRepaintBoundariesbooltrue是否给每个子节点自动包裹 RepaintBoundary,减少重绘范围,提高性能。
estimatedChildCountint?给框架一个"预估"子项数量,它不会限制builder的调用次数,也不会影响实际渲染,只是告诉框架一个大概值,当子项数量未知或很大,但又想滚动条不至于"忽大忽小"