我想使用 Layout Builder 测量 SliverAppBar 的高度,然后在获得 SliverAppBar 高度的高度后,使用ScrollController。
我有一些类似这样的代码:
CustomScrollView(
controller: _scrollController,
slivers: <Widget>[
LayoutBuilder(
builder: (context, constraint) {
message = constraint.biggest.height.toString();
return SliverAppBar(
floating: false,
pinned: true,
expandedHeight: ScreenUtil.getInstance().setHeight(250),
leading: Container(
color: Colors.blue.withOpacity(.5),
child: IconButton(
icon: Icon(Icons.arrow_back),
onPressed: () => Navigator.pop(context),
),
),
flexibleSpace: FlexibleSpaceBar(
// titlePadding: EdgeInsets.zero,
title: Container(
width: ScreenUtil.getInstance().setWidth(250),
child: Text(
message ?? "Null",
style: ResponsiveUI.textNamaWartawanStylWhite,
overflow: TextOverflow.ellipsis,
),
),
background: Container(
child: CachedNetworkImage(
fit: BoxFit.cover,
imageUrl:
"${Urls.BASE_API_IMAGE}/wartawan/${widget.gambarWartawan}",
),
),
),
actions: <Widget>[
Container(
color: Colors.blue.withOpacity(.5),
child: IconButton(
icon: Icon(Icons.more_horiz),
onPressed: () => "",
),
),
],
);
},
),
],
),
但是我得到这个错误:
The following assertion was thrown building Listener:
A RenderViewport expected a child of type RenderSliver but received a child of type _RenderLayoutBuilder.
RenderObjects expect specific types of children because they coordinate with their children during layout and paint. For example, a RenderSliver cannot be the child of a RenderBox because a RenderSliver does not understand the RenderBox layout protocol.
The RenderViewport that expected a RenderSliver child was created by: Viewport ← IgnorePointer-[GlobalKey#4f27a] ← Semantics ← _PointerListener ← Listener ← _GestureSemantics ← RawGestureDetector-[LabeledGlobalKey<RawGestureDetectorState>#ed3fa] ← _PointerListener ← Listener ← _ScrollableScope ← _ScrollSemantics-[GlobalKey#c7130] ← RepaintBoundary ← ⋯
The _RenderLayoutBuilder that did not match the expected child type was created by: LayoutBuilder ← Viewport ← IgnorePointer-[GlobalKey#4f27a] ← Semantics ← _PointerListener ← Listener ← _GestureSemantics ← RawGestureDetector-[LabeledGlobalKey<RawGestureDetectorState>#ed3fa] ← _PointerListener ← Listener ← _ScrollableScope ← _ScrollSemantics-[GlobalKey#c7130] ← ⋯
User-created ancestor of the error-causing widget was
CustomScrollView
When the exception was thrown, this was the stack
#0 ContainerRenderObjectMixin.debugValidateChild.<anonymous closure>
#1 ContainerRenderObjectMixin.debugValidateChild
您能帮我解决我的案件吗? 谢谢。
答案 0 :(得分:1)
您不能在CustomScrollView中放置非银条。您可能需要将layoutbuilder放置在FlexibleSpace的背景内。
答案 1 :(得分:0)
试试 SliverLayoutBuilder
小部件。
说明:
您不能将非细长小部件(例如 LayoutBuilder
)放置在将细长部件作为子部件的小部件(例如 CustomScrollView
)内。
例如,当您想向条子添加填充时,您应该使用 SliverPadding
小部件,而不是普通的 Padding
小部件。类似地,还有一个 LayoutBuilder
的银色版本,称为 SliverLayoutBuilder
。