Flutter:SingleChildScrollView沿垂直轴收缩包裹其子对象的错误



在Scaffold中,我有一个容器来设置背景。在容器的子级中我有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 ← .....

问题出在哪里?

问题是两个Expanded在垂直轴上无限扩展Column和SingleChildScrollView。你必须在身体的某个地方限制高度。

目前尚不清楚您试图在这里实现什么,但最简单的解决方案是删除两个Expanded或SingleChildScrollView。

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

  • 展开将基于父列的框约束展开
  • 看起来SingleChildScrollView正在向列传递无限高度约束,而Expanded正试图扩展到无限

尝试用Container或ConstraintdBox包装Column并传递有限的高度。为了了解更多,我写了一篇关于为什么会发生这种情况的文章。

最新更新