build method
The widget to place inside the SliverPersistentHeader.
The context
is the BuildContext of the sliver.
The shrinkOffset
is a distance from maxExtent towards minExtent
representing the current amount by which the sliver has been shrunk. When
the shrinkOffset
is zero, the contents will be rendered with a dimension
of maxExtent in the main axis. When shrinkOffset
equals the difference
between maxExtent and minExtent (a positive number), the contents will
be rendered with a dimension of minExtent in the main axis. The
shrinkOffset
will always be a positive number in that range.
The overlapsContent
argument is true if subsequent slivers (if any) will
be rendered beneath this one, and false if the sliver will not have any
contents below it. Typically this is used to decide whether to draw a
shadow to simulate the sliver being above the contents below it. Typically
this is true when shrinkOffset
is at its greatest value and false
otherwise, but that is not guaranteed. See NestedScrollView for an
example of a case where overlapsContent
's value can be unrelated to
shrinkOffset
.
Implementation
@override
Widget build(
BuildContext context,
double shrinkOffset,
bool overlapsContent,
) {
Widget child = builder(context, shrinkOffset, overlapsContent);
//测试代码:如果在调试模式,且子组件设置了key,则打印日志
assert(() {
if (child.key != null) {
debugPrint(
'${child.key}: shrink: $shrinkOffset,overlaps:$overlapsContent');
}
return true;
}());
// 让 header 尽可能充满限制的空间;宽度为 Viewport 宽度,
// 高度随着用户滑动在[minHeight,maxHeight]之间变化。
return SizedBox.expand(child: child);
}