我有一个特殊的要求,允许标题小部件,通常包含静态内容出现在滚动视图的顶部。滚动视图应与标题小部件重叠,以便可以使用剪辑形状来产生效果。我通过使用堆栈视图来实现此效果,其中标题小部件作为堆栈中的第一项,滚动视图作为顶部元素。滚动视图包含一列,其中第一个子项是所需高度的空容器(标题小部件的高度减去重叠量)。当将已知高度作为硬编码参数传递时,这可以达到预期的效果。 (注意:我尝试使用 Sliver List 来实现此目的,但无法实现所需的重叠来满足产品要求。)
标头小部件包含通过 API 响应加载的图像。图像的高度可能会有所不同,因此我需要在运行时确定这一点并相应地调整 UI。我认为这不是一项艰巨的任务,但到目前为止,我还没有找到一种方法来做到这一点。下面的两张图片显示了所需的效果,右侧的图片显示了向上滚动时的正确行为。请注意,滚动视图与标题图像的重叠量必须与剪辑的半径相同。
这会生成列表。_getComponents
为 SingleChildScrollView 中包含的列提供子部件:
List<Widget> _getComponents(List<Section> sections, BuildContext context, double? height) {
List<Widget> widgetList = [
Container(height: 225) // need to obtain correct height here
];
for (int i = 1; i < sections.length; i++) {
Section section = sections[i];
if (section.model != null) {
switch (section.code) {
case mediaTypeCode:
if (section.model.runtimeType == MediaModel) {
widgetList.add(HeaderComponent(section: section));
}
break;
case articleTypeCode:
if (section.model.runtimeType == ArticleSectionModel) {
widgetList.add(TitleContentHeader(
model: section.model as ArticleSectionModel));
}
break;
}
}
}
return widgetList;
}
然后在我的视图小部件中,堆栈的构建如下:
return Container(
color: Colors.white,
child: Stack(
children: [
_getHeader(sections),
SingleChildScrollView(
child: Column(
children: _getComponents(sections!, context, null),
),
),
],
),
);
我需要能够获取返回的标题的高度_getHeader(sections)
并将其传递给_getComponents
函数,以便我可以确定滚动视图的正确起始位置。
谁能对此提供任何见解?
或者,您能建议一个允许上图中显示行为的插件吗?如上所述,Sliver List 并没有产生预期的效果。
Thanks!