如何在Flutter中添加列小部件到行小部件?

k2arahey  于 2022-12-24  发布在  Flutter
关注(0)|答案(1)|浏览(239)

我想将Column小工具添加到Row小工具。
我有一个数据结构如下:

Scaffold
 - Padding
    - SingleChildScrollView
       - Column
          - Row
             - CircleAvatar
             - Column (The Column I am talking about below)
                - Text
                - Row
                   - Expanded
                      - MaterialButton
                   - Expanded
                      - Container
                   - Expanded
                      - MaterialButton
          - ListTile

在将Column添加到Row时,出现错误:

════════ Exception caught by rendering library ═════════════════════════════════
The following assertion was thrown during performLayout():
RenderFlex children have non-zero flex but incoming width constraints are unbounded.

When a row is in a parent that does not provide a finite width constraint, for example if it is in a horizontal scrollable, it will try to shrink-wrap its children along the horizontal axis. Setting a flex on a child (e.g. using Expanded) indicates that the child is to expand to fill the remaining space in the horizontal direction.
These two directives are mutually exclusive. If a parent is to shrink-wrap its child, the child cannot simultaneously expand to fit its parent.

Consider setting mainAxisSize to MainAxisSize.min and using FlexFit.loose fits for the flexible children (using Flexible rather than Expanded). This will allow the flexible children to size themselves to less than the infinite remaining space they would otherwise be forced to take, and then will cause the RenderFlex to shrink-wrap the children rather than expanding to fit the maximum constraints provided by the parent.

If this message did not help you determine the problem, consider using debugDumpRenderTree():
  https://flutter.dev/debugging/#rendering-layer
  http://api.flutter.dev/flutter/rendering/debugDumpRenderTree.html
The affected RenderFlex is: RenderFlex#acbae relayoutBoundary=up15 NEEDS-LAYOUT NEEDS-PAINT NEEDS-COMPOSITING-BITS-UPDATE
    parentData: offset=Offset(0.0, 0.0); flex=null; fit=null (can use size)
    constraints: BoxConstraints(unconstrained)
    size: MISSING
    direction: horizontal
    mainAxisAlignment: start
    mainAxisSize: max
    crossAxisAlignment: center
    textDirection: ltr
    verticalDirection: down
    child 1: RenderConstrainedBox#404ec NEEDS-LAYOUT NEEDS-PAINT NEEDS-COMPOSITING-BITS-UPDATE
        parentData: offset=Offset(0.0, 0.0); flex=3; fit=FlexFit.tight
        constraints: MISSING
        size: MISSING
        additionalConstraints: BoxConstraints(w=180.0, h=50.0)
        child: RenderSemanticsAnnotations#4f838 NEEDS-LAYOUT NEEDS-PAINT NEEDS-COMPOSITING-BITS-UPDATE
            parentData: <none>
            constraints: MISSING
            semantic boundary
            size: MISSING
            child: _RenderInputPadding#04251 NEEDS-LAYOUT NEEDS-PAINT NEEDS-COMPOSITING-BITS-UPDATE
                parentData: <none>
                constraints: MISSING
                size: MISSING
                child: RenderConstrainedBox#3337d NEEDS-LAYOUT NEEDS-PAINT NEEDS-COMPOSITING-BITS-UPDATE
                    parentData: offset=Offset(0.0, 0.0)
                    constraints: MISSING
                    size: MISSING
                    additionalConstraints: BoxConstraints(88.0<=w<=Infinity, 36.0<=h<=Infinity)
    child 2: RenderLimitedBox#593aa NEEDS-LAYOUT NEEDS-PAINT NEEDS-COMPOSITING-BITS-UPDATE
        parentData: offset=Offset(0.0, 0.0); flex=1; fit=FlexFit.tight
        constraints: MISSING
        size: MISSING
        maxWidth: 0.0
        maxHeight: 0.0
        child: RenderConstrainedBox#d1172 NEEDS-LAYOUT NEEDS-PAINT
            parentData: <none>
            constraints: MISSING
            size: MISSING
            additionalConstraints: BoxConstraints(biggest)
    child 3: RenderConstrainedBox#42764 NEEDS-LAYOUT NEEDS-PAINT NEEDS-COMPOSITING-BITS-UPDATE
        parentData: offset=Offset(0.0, 0.0); flex=3; fit=FlexFit.tight
        constraints: MISSING
        size: MISSING
        additionalConstraints: BoxConstraints(w=180.0, h=50.0)
        child: RenderSemanticsAnnotations#7ec96 NEEDS-LAYOUT NEEDS-PAINT NEEDS-COMPOSITING-BITS-UPDATE
            parentData: <none>
            constraints: MISSING
            semantic boundary
            size: MISSING
            child: _RenderInputPadding#6aa1f NEEDS-LAYOUT NEEDS-PAINT NEEDS-COMPOSITING-BITS-UPDATE
                parentData: <none>
                constraints: MISSING
                size: MISSING
                child: RenderConstrainedBox#e1f60 NEEDS-LAYOUT NEEDS-PAINT NEEDS-COMPOSITING-BITS-UPDATE
                    parentData: offset=Offset(0.0, 0.0)
                    constraints: MISSING
                    size: MISSING
                    additionalConstraints: BoxConstraints(88.0<=w<=Infinity, 36.0<=h<=Infinity)
The creator information is set to: Row ← Column ← Row ← Column ← _SingleChildViewport ← IgnorePointer-[GlobalKey#10aed] ← Semantics ← Listener ← _GestureSemantics ← RawGestureDetector-[LabeledGlobalKey<RawGestureDetectorState>#23575] ← Listener ← _ScrollableScope ← ⋯
The nearest ancestor providing an unbounded width constraint is: RenderFlex#ede32 relayoutBoundary=up13 NEEDS-LAYOUT NEEDS-PAINT NEEDS-COMPOSITING-BITS-UPDATE

See also: https://flutter.dev/layout/

If none of the above helps enough to fix this problem, please don't hesitate to file a bug:
  https://github.com/flutter/flutter/issues/new?template=2_bug.md
The relevant error-causing widget was
Row
When the exception was thrown, this was the stack
#0      RenderFlex.performLayout.<anonymous closure>
#1      RenderFlex.performLayout
#2      RenderObject.layout
#3      RenderBox.layout
#4      ChildLayoutHelper.layoutChild
#5      RenderFlex._computeSizes
#6      RenderFlex.performLayout
#7      RenderObject.layout
#8      RenderBox.layout
#9      ChildLayoutHelper.layoutChild
#10     RenderFlex._computeSizes
#11     RenderFlex.performLayout
#12     RenderObject.layout
#13     RenderBox.layout
#14     ChildLayoutHelper.layoutChild
#15     RenderFlex._computeSizes
#16     RenderFlex.performLayout
#17     RenderObject.layout
#18     RenderBox.layout
#19     _RenderSingleChildViewport.performLayout
#20     RenderObject.layout
#21     RenderBox.layout
#22     RenderProxyBoxMixin.performLayout
#23     RenderObject.layout
#24     RenderBox.layout
#25     RenderProxyBoxMixin.performLayout
#26     RenderObject.layout
#27     RenderBox.layout
#28     RenderProxyBoxMixin.performLayout
#29     RenderObject.layout
#30     RenderBox.layout
#31     RenderProxyBoxMixin.performLayout
#32     RenderObject.layout
#33     RenderBox.layout
#34     RenderProxyBoxMixin.performLayout
#35     RenderObject.layout
#36     RenderBox.layout
#37     RenderProxyBoxMixin.performLayout
#38     RenderObject.layout
#39     RenderBox.layout
#40     RenderProxyBoxMixin.performLayout
#41     RenderObject.layout
#42     RenderBox.layout
#43     RenderProxyBoxMixin.performLayout
#44     RenderCustomPaint.performLayout
#45     RenderObject.layout
#46     RenderBox.layout
#47     RenderProxyBoxMixin.performLayout
#48     RenderObject.layout
#49     RenderBox.layout
#50     RenderPadding.performLayout
#51     RenderObject.layout
#52     RenderBox.layout
#53     MultiChildLayoutDelegate.layoutChild
#54     _ScaffoldLayout.performLayout
#55     MultiChildLayoutDelegate._callPerformLayout
#56     RenderCustomMultiChildLayoutBox.performLayout
#57     RenderObject._layoutWithoutResize
#58     PipelineOwner.flushLayout
#59     RendererBinding.drawFrame
#60     WidgetsBinding.drawFrame
#61     RendererBinding._handlePersistentFrameCallback
#62     SchedulerBinding._invokeFrameCallback
#63     SchedulerBinding.handleDrawFrame
#64     SchedulerBinding._handleDrawFrame
#68     _invoke (dart:ui/hooks.dart:150:10)
#69     PlatformDispatcher._drawFrame (dart:ui/platform_dispatcher.dart:318:5)
#70     _drawFrame (dart:ui/hooks.dart:115:31)
(elided 3 frames from dart:async)
The following RenderObject was being processed when the exception was fired: RenderFlex#acbae relayoutBoundary=up15 NEEDS-LAYOUT NEEDS-PAINT NEEDS-COMPOSITING-BITS-UPDATE
RenderObject: RenderFlex#acbae relayoutBoundary=up15 NEEDS-LAYOUT NEEDS-PAINT NEEDS-COMPOSITING-BITS-UPDATE
    parentData: offset=Offset(0.0, 0.0); flex=null; fit=null (can use size)
    constraints: BoxConstraints(unconstrained)
    size: MISSING
    direction: horizontal
    mainAxisAlignment: start
    mainAxisSize: max
    crossAxisAlignment: center
    textDirection: ltr
    verticalDirection: down
    child 1: RenderConstrainedBox#404ec NEEDS-LAYOUT NEEDS-PAINT NEEDS-COMPOSITING-BITS-UPDATE
        parentData: offset=Offset(0.0, 0.0); flex=3; fit=FlexFit.tight
        constraints: MISSING
        size: MISSING
        additionalConstraints: BoxConstraints(w=180.0, h=50.0)
        child: RenderSemanticsAnnotations#4f838 NEEDS-LAYOUT NEEDS-PAINT NEEDS-COMPOSITING-BITS-UPDATE
            parentData: <none>
            constraints: MISSING
            semantic boundary
            size: MISSING
            child: _RenderInputPadding#04251 NEEDS-LAYOUT NEEDS-PAINT NEEDS-COMPOSITING-BITS-UPDATE
                parentData: <none>
                constraints: MISSING
                size: MISSING
                child: RenderConstrainedBox#3337d NEEDS-LAYOUT NEEDS-PAINT NEEDS-COMPOSITING-BITS-UPDATE
                    parentData: offset=Offset(0.0, 0.0)
                    constraints: MISSING
                    size: MISSING
                    additionalConstraints: BoxConstraints(88.0<=w<=Infinity, 36.0<=h<=Infinity)
    child 2: RenderLimitedBox#593aa NEEDS-LAYOUT NEEDS-PAINT NEEDS-COMPOSITING-BITS-UPDATE
        parentData: offset=Offset(0.0, 0.0); flex=1; fit=FlexFit.tight
        constraints: MISSING
        size: MISSING
        maxWidth: 0.0
        maxHeight: 0.0
        child: RenderConstrainedBox#d1172 NEEDS-LAYOUT NEEDS-PAINT
            parentData: <none>
            constraints: MISSING
            size: MISSING
            additionalConstraints: BoxConstraints(biggest)
    child 3: RenderConstrainedBox#42764 NEEDS-LAYOUT NEEDS-PAINT NEEDS-COMPOSITING-BITS-UPDATE
        parentData: offset=Offset(0.0, 0.0); flex=3; fit=FlexFit.tight
        constraints: MISSING
        size: MISSING
        additionalConstraints: BoxConstraints(w=180.0, h=50.0)
        child: RenderSemanticsAnnotations#7ec96 NEEDS-LAYOUT NEEDS-PAINT NEEDS-COMPOSITING-BITS-UPDATE
            parentData: <none>
            constraints: MISSING
            semantic boundary
            size: MISSING
            child: _RenderInputPadding#6aa1f NEEDS-LAYOUT NEEDS-PAINT NEEDS-COMPOSITING-BITS-UPDATE
                parentData: <none>
                constraints: MISSING
                size: MISSING
                child: RenderConstrainedBox#e1f60 NEEDS-LAYOUT NEEDS-PAINT NEEDS-COMPOSITING-BITS-UPDATE
                    parentData: offset=Offset(0.0, 0.0)
                    constraints: MISSING
                    size: MISSING
                    additionalConstraints: BoxConstraints(88.0<=w<=Infinity, 36.0<=h<=Infinity)
════════════════════════════════════════════════════════════════════════════════

…

我的代码:

Row(
  children: [
    CircleAvatar(
      backgroundImage: NetworkImage(userAvatarUrl),
    ),
    Column(
      children: [
        Text(userName),
        Row(
          children: …,
        ),
      ],
    ),
  ],
),

如果您需要更多信息,请随时发表评论。
如何添加Column小工具到Row小工具?我将感谢任何帮助。提前感谢你!

3gtaxfhh

3gtaxfhh1#

如果Column内的Row内的children可以是Expanded,则考虑将Column Package 在Expanded中,如下所示:

Row(
  children: [
    CircleAvatar(
      backgroundImage: NetworkImage(userAvatarUrl),
    ),
    Expanded(
      child: Column(
        children: [
          Text(userName),
          Row(
            children: …,
          ),
      ],
    ),
    )
  ],
),

相关问题