【问题标题】:How can I use a CustomScrollView as a child如何将 CustomScrollView 用作孩子
【发布时间】:2020-10-24 17:07:28
【问题描述】:

我是刚开始使用 Flutter 的新手。

我想使用 CustomScrollView 作为 Row/Column Widget 的子级

CustomScrollView 为 root 时效果很好。 但是当我把它放到 Row Widget 的 child 中时,它就报错了。

CustomScrollview 不能用作 Row 小部件的子级吗? 如果是,请告诉我原因,最好的选择是什么?

或者如果我的代码有错误,我希望你纠正它。

What I want to make

my CustomScrollViewWidget

我不知道这是否是你想要的,但这是我的错误。

══╡ EXCEPTION CAUGHT BY RENDERING LIBRARY ╞═════════════════════════════════════════════════════════
The following assertion was thrown during performLayout():
'package:flutter/src/rendering/viewport.dart': Failed assertion: line 1758 pos 16:
'constraints.hasBoundedHeight': is not true.
Either the assertion indicates an error in the framework itself, or we should provide substantially
more information in this error message to help you determine and fix the underlying cause.
In either case, please report this assertion by filing a bug on GitHub:
  https://github.com/flutter/flutter/issues/new?template=BUG.md
The relevant error-causing widget was:
  CustomScrollView



The following RenderObject was being processed when the exception was fired: RenderShrinkWrappingViewport#0344f relayoutBoundary=up14 NEEDS-LAYOUT NEEDS-PAINT NEEDS-COMPOSITING-BITS-UPDATE:
  needs compositing
  creator: ShrinkWrappingViewport ← IgnorePointer-[GlobalKey#e4530] ← Semantics ← _PointerListener ←
    Listener ← _GestureSemantics ←
    RawGestureDetector-[LabeledGlobalKey<RawGestureDetectorState>#a61b3] ← _PointerListener ← Listener
    ← _ScrollableScope ← _ScrollSemantics-[GlobalKey#42b3c] ← RepaintBoundary ← ⋯
  parentData: <none> (can use size)
  constraints: BoxConstraints(unconstrained)
  size: MISSING
  axisDirection: right
  crossAxisDirection: down
  offset: ScrollPositionWithSingleContext#46fe1(offset: 0.0, range: null..null, viewport: null,
    ScrollableState, ClampingScrollPhysics -> RangeMaintainingScrollPhysics, IdleScrollActivity#66c28,
    ScrollDirection.idle)
This RenderObject had the following child:
    child 0: RenderSliverList#42dfb NEEDS-LAYOUT NEEDS-PAINT
════════════════════════════════════════════════════════════════════════════════════════════════════

════════ Exception caught by rendering library ═════════════════════════════════
RenderBox was not laid out: RenderShrinkWrappingViewport#0344f relayoutBoundary=up14 NEEDS-PAINT NEEDS-COMPOSITING-BITS-UPDATE
'package:flutter/src/rendering/box.dart':
Failed assertion: line 1702 pos 12: 'hasSize'
The relevant error-causing widget was
CustomScrollView
lib\0. TestCode.dart:31
════════════════════════════════════════════════════════════════════════════════

这是我的代码!

class WrapVisit extends StatefulWidget {
  WrapVisitState createState() => WrapVisitState();
}

class WrapVisitState extends State<WrapVisit> {
  @override
  Widget build(BuildContext context) {
    return new Container(
      padding: EdgeInsets.all(10),
      color: Colors.white,
      child: Column(
        children: [
          Row(
            mainAxisAlignment: MainAxisAlignment.spaceBetween,
            children: [
              new Text(
                "브랜드관",
                textAlign: TextAlign.left,
                style: TextStyle(fontWeight: FontWeight.bold),
              ),
              new Container(
                margin: EdgeInsets.all(5),
                child: new Text(
                  "전체보기",
                  textAlign: TextAlign.right,
                  style: TextStyle(
                    fontSize: 10,
                    color: Colors.black,
                  ),
                ),
              ),
              CustomScrollView(
                shrinkWrap: true,
                scrollDirection: Axis.horizontal,
                slivers: [
                  SliverList(
                    delegate: SliverChildListDelegate(
                      [
                        ... SliverListChildWidgets ...
                      ],
                    ),
                  ),
                ],
              ),
            ],
          ),
        ],
      ),
    );
  }
}

【问题讨论】:

  • 你能补充一下你得到了什么错误
  • 我添加了一个错误。

标签: flutter dart


【解决方案1】:

Column 不喜欢身高不限的孩子。要修复它,请将CustomScrollViewshrinkWrap 参数设置为true

【讨论】:

  • 感谢您的回答。我将修改我的示例代码。但它没有用......
【解决方案2】:

您需要先将滚动视图包装在一个大小合适的盒子或容器中。它需要一个具有设定尺寸的父级。有几个小部件不能在行/列中工作,除非您先将它们放在大小合适的父级中。

【讨论】:

  • 感谢您的回答。这就是我想要的!
猜你喜欢
  • 2019-11-14
  • 1970-01-01
  • 2017-02-09
  • 2022-10-14
  • 1970-01-01
  • 2021-08-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多