Flutter:SingleChildScrollView沿垂直轴错误收缩其子级

问题描述 投票:0回答:2

[在脚手架中,我有一个容器来设置背景。在容器的子代中,我有SingleChildScrollView,在其子代中,我有Column。在此小部件的子级中,我有2个Expanded小部件:

Scaffold
    |
     Container
        |
         SingleChildScrollView
            |
             * Expanded
             * Expanded

这是代码:

 Widget build(BuildContext context) {
    return Scaffold(
      extendBodyBehindAppBar: true,
      resizeToAvoidBottomPadding: true,
      body: Container(
          decoration: BoxDecoration(
            image: DecorationImage(
              image: AssetImage("images/main_background.png"),
              fit: BoxFit.cover,
            ),
          ),
          child: SingleChildScrollView(
            child: Column(
              mainAxisAlignment: MainAxisAlignment.start,
              children: <Widget>[
                Expanded(..)
                Expanded(..)

我的问题是,在此页面上运行时,所有页面的颜色均为粉红色,这表示我收到此错误:

I/flutter ( 8893): ══╡ EXCEPTION CAUGHT BY RENDERING LIBRARY ╞═════════════════════════════════════════════════════════
I/flutter ( 8893): The following assertion was thrown during performLayout():
I/flutter ( 8893): RenderFlex children have non-zero flex but incoming height constraints are unbounded.
I/flutter ( 8893): When a column is in a parent that does not provide a finite height constraint, for example if it is
I/flutter ( 8893): in a vertical scrollable, it will try to shrink-wrap its children along the vertical axis. Setting a
I/flutter ( 8893): flex on a child (e.g. using Expanded) indicates that the child is to expand to fill the remaining
I/flutter ( 8893): space in the vertical direction.
I/flutter ( 8893): These two directives are mutually exclusive. If a parent is to shrink-wrap its child, the child
I/flutter ( 8893): cannot simultaneously expand to fit its parent.
I/flutter ( 8893): Consider setting mainAxisSize to MainAxisSize.min and using FlexFit.loose fits for the flexible
I/flutter ( 8893): children (using Flexible rather than Expanded). This will allow the flexible children to size
I/flutter ( 8893): themselves to less than the infinite remaining space they would otherwise be forced to take, and
I/flutter ( 8893): then will cause the RenderFlex to shrink-wrap the children rather than expanding to fit the maximum
I/flutter ( 8893): constraints provided by the parent.
I/flutter ( 8893): If this message did not help you determine the problem, consider using debugDumpRenderTree():
I/flutter ( 8893):   https://flutter.dev/debugging/#rendering-layer
I/flutter ( 8893):   http://api.flutter.dev/flutter/rendering/debugDumpRenderTree.html
I/flutter ( 8893): The affected RenderFlex is:
I/flutter ( 8893):   RenderFlex#4b35c relayoutBoundary=up13 NEEDS-LAYOUT NEEDS-PAINT NEEDS-COMPOSITING-BITS-UPDATE(creator: Column ← _SingleChildViewport ← IgnorePointer-[GlobalKey#fcff2] ← Semantics ← _PointerListener ← Listener ← _GestureSemantics ← RawGestureDetector-[LabeledGlobalKey<RawGestureDetectorState>#e245d] ← _PointerListener ← Listener ← _ScrollableScope ← _ScrollSemantics-[GlobalKey#471e6] ← ⋯, parentData: <none> (can use size), constraints: BoxConstraints(0.0<=w<=320.0, 0.0<=h<=Infinity), size: MISSING, direction: vertical, mainAxisAlignment: start, mainAxisSize: max, crossAxisAlignment: center, verticalDirection: down)
I/flutter ( 8893): The creator information is set to:
I/flutter ( 8893):   Column ← _SingleChildViewport ← IgnorePointer-[GlobalKey#fcff2] ← Semantics ← .....

怎么了?

flutter flutter-layout
2个回答
0
投票

问题是,两个Expanded在垂直轴上无限扩展Column和SingleChildScrollView。您必须将高度限制在体内某处。

目前尚不清楚您要实现的目标,但是最直接的解决方案是删除两个Expanded或SingleChildScrollView。

您可能还想使用ListView而不是SingleChildScrollView + Column。


0
投票
  • 展开后将根据父列的框约束展开
  • 看起来像SingleChildScrollView将无限的高度约束传递给Column,而Expanded试图将其扩展为无限。

尝试用容器或ConstrainedBox包裹列,并传递有限的高度。为了了解更多,我写了一篇article来说明为什么会发生这种情况。

© www.soinside.com 2019 - 2024. All rights reserved.