Flutter:嵌套 ListView 有效。嵌套的 SliverList 没有,但 ListView 包含 Sliver

Posted

技术标签:

【中文标题】Flutter:嵌套 ListView 有效。嵌套的 SliverList 没有,但 ListView 包含 Sliver【英文标题】:Flutter: nested ListView works. Nested SliverList don't, yet ListView contains Slivers 【发布时间】:2021-07-04 11:40:02 【问题描述】:

我遇到了一个特定的问题。我能够组成一个垂直外部ListView,其中包含水平ListViews。把它想象成 Facebook 上的多个 Friend Suggestion Row 堆叠在一起。这很好用。

因为我想利用 SliverAppBar 附带的特权,所以我需要将 CustomScrollView 作为父 Widget,然后在其 slivers 集合中只添加条子。

当我尝试在SliverChildBuilderDelegate 中添加SliverList 时,我得到了各种各样的错误,主要是与渲染相关的类型错误。 SliverChildBuilderDelegate 可以培养的孩子似乎有一个限制。

NEEDS-COMPOSITING-BITS-UPDATE:
  needs compositing
  creator: Viewport ← IgnorePointer-[GlobalKey#a5a09] ← Semantics ← Listener ← _GestureSemantics ←
    RawGestureDetector-[LabeledGlobalKey<RawGestureDetectorState>#db221] ← Listener ← _ScrollableScope
    ← _ScrollSemantics-[GlobalKey#57142] ← Scrollable ← CustomScrollView ← Padding ← ⋯
  parentData: <none> (can use size)
  constraints: BoxConstraints(w=412.0, 0.0<=h<=Infinity)
  size: MISSING
  axisDirection: right
  crossAxisDirection: down
  offset: ScrollPositionWithSingleContext#415a2(offset: 0.0, range: null..null, viewport: null,
    ScrollableState, BouncingScrollPhysics -> RangeMaintainingScrollPhysics, IdleScrollActivity#74aea,
    ScrollDirection.idle)
  anchor: 0.0
This RenderObject had the following child:
    center child: RenderSliverList#5219f NEEDS-LAYOUT NEEDS-PAINT
════════════════════════════════════════════════════════════════════════════════════════════════════

Another exception was thrown: RenderBox was not laid out: RenderViewport#e26b4 NEEDS-LAYOUT NEEDS-PAINT NEEDS-COMPOSITING-BITS-UPDATE

Another exception was thrown: RenderBox was not laid out: RenderViewport#e26b4 NEEDS-PAINT NEEDS-COMPOSITING-BITS-UPDATE

Another exception was thrown: RenderBox was not laid out: RenderIgnorePointer#e30f7 relayoutBoundary=up10 NEEDS-PAINT NEEDS-COMPOSITING-BITS-UPDATE

Another exception was thrown: RenderBox was not laid out: RenderSemanticsAnnotations#d70ab relayoutBoundary=up9 NEEDS-PAINT
NEEDS-COMPOSITING-BITS-UPDATE

如您所见,我尝试将 SliverList 包装在非 sliver 类型的 Widget 中,但无济于事。

我尝试使用NestedScrollView 解决它,但我不断收到相同类型的错误。

以前有没有人遇到过这种情况。我浏览了源代码,我可以看到ListView 基本上是一个BoxScrollView,内部有一个ScrollView

我是否需要创建一个专门的小部件来完成这个布局设计。如果是这样的话,我已经对 Flutter 摸不着头脑了。

【问题讨论】:

【参考方案1】:

解决了。我所缺少的只是 heightwidth 在我的嵌套 SliverList 的包含 Container 中。

【讨论】:

以上是关于Flutter:嵌套 ListView 有效。嵌套的 SliverList 没有,但 ListView 包含 Sliver的主要内容,如果未能解决你的问题,请参考以下文章

Flutter 小技巧之 ListView 和 PageView 的各种花式嵌套

Flutter中Column嵌套ListView报错处理方案

Flutter—ListView嵌套ListView不显示的问题

Flutter—ListView嵌套ListView不显示的问题

Flutter—ListView嵌套ListView不显示的问题

带有嵌套 TabBarView 的 Flutter ListView