RenderFlex 子级具有非零 flex,但传入的高度约束在颤动中是无界的

时间:2021-02-26 10:58:38

标签: flutter

当我在频道视图中渲染文章卡片列表小部件时,频道视图的Column组件会像这样(频道元素下方的文章列表卡片):

return GestureDetector(
      onHorizontalDragStart: _onHorizontalDragStart,
      onHorizontalDragUpdate: _onHorizontalDragUpdate,
      onHorizontalDragEnd: _onHorizontalDragEnd,
      child: Container(
        constraints: BoxConstraints(
          minHeight: MediaQuery.of(context).size.height * 0.9,
        ),
        color: Theme.of(context).scaffoldBackgroundColor,
        child: Padding(
          padding: const EdgeInsets.all(
            16.0,
          ),
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.stretch,
            mainAxisSize: MainAxisSize.min,
            children: [
              InkWell(
                onTap: () => {},
                child: Padding(
                  padding: const EdgeInsets.only(bottom: 8.0),
                  child: Container(
                    child: Text(
                      item.subName == "" ? "Comment" : item.subName,
                      style: Theme.of(context).textTheme.headline5.copyWith(
                            fontWeight: FontWeight.w600,
                          ),
                    ),
                  ),
                ),
              ),
              if (item.isFav == 1)
                Padding(
                  padding: const EdgeInsets.only(top: 8, bottom: 8.0, right: 1),
                  child: ButtonTheme(
                      minWidth: 50,
                      height: 30.0,
                      child: RaisedButton.icon(
                        color: Theme.of(context).primaryColor,
                        icon: Icon(
                          Feather.check_circle,
                          size: 16,
                          color: Theme.of(context).canvasColor,
                        ),
                        shape: new RoundedRectangleBorder(borderRadius: new BorderRadius.circular(5.0)),
                        onPressed: () => touchSub(item.id.toString(), SubStatus.UNSUB),
                        label: Text("已订阅"),
                      )),
                ),
              if (item.isFav != 1)
                Padding(
                  padding: const EdgeInsets.only(top: 8, bottom: 8.0, right: 1),
                  child: ButtonTheme(
                      minWidth: 50,
                      height: 30.0,
                      child: RaisedButton(
                        color: Theme.of(context).primaryColor,
                        shape: new RoundedRectangleBorder(borderRadius: new BorderRadius.circular(5.0)),
                        onPressed: () => touchSub(item.id.toString(), SubStatus.SUB),
                        child: Text("订阅"),
                      )),
                ),
              InkWell(
                onTap: () {
                  Navigator.push(
                    context,
                    MaterialPageRoute(builder: (context) => ProfilePage(username: item.author)),
                  );
                },
                child: RichText(
                  text: TextSpan(
                    children: <TextSpan>[
                      TextSpan(
                        text: item.author,
                        style: Theme.of(context).textTheme.caption.copyWith(
                              color: Theme.of(context).primaryColor,
                            ),
                      ),
                    ],
                  ),
                ),
              ),
              if (item.content != "")
                Html(
                  data: item.content,
                  style: {
                    "body": Style(
                      fontSize: FontSize(19.0),
                    ),
                  },
                  onLinkTap: (url) => launchUrl(url),
                ),
              if (item.parts.isNotEmpty)
                Padding(
                  padding: const EdgeInsets.only(top: 8.0),
                ),
              Expanded(
                  child: SizedBox(
                      height: 200.0,
                      child: NestedScrollView(
                        headerSliverBuilder: (BuildContext context, bool innerBoxIsScrolled) {
                          return <Widget>[];
                        },
                        body: viewService.buildComponent("homelistdefault"),
                      )))
            ],
          ),
        ),
      ));

显示错误:

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

When a column is in a parent that does not provide a finite height constraint, for example if it is in a vertical scrollable, it will try to shrink-wrap its children along the vertical 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 vertical 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#681a6 relayoutBoundary=up7 NEEDS-LAYOUT NEEDS-PAINT NEEDS-COMPOSITING-BITS-UPDATE
  needs compositing
  parentData: offset=Offset(16.0, 16.0) (can use size)
  constraints: BoxConstraints(w=379.4, 706.5<=h<=Infinity)
  size: MISSING
  direction: vertical
  mainAxisAlignment: start
  mainAxisSize: min
  crossAxisAlignment: stretch
  verticalDirection: down
...  child 1: RenderSemanticsAnnotations#e7e5a relayoutBoundary=up8 NEEDS-PAINT NEEDS-COMPOSITING-BITS-UPDATE
...    parentData: offset=Offset(0.0, 0.0); flex=null; fit=null (can use size)
...    constraints: BoxConstraints(w=379.4, 0.0<=h<=Infinity)
...    size: Size(379.4, 43.0)
...    child: RenderMouseRegion#1b035 relayoutBoundary=up9 NEEDS-PAINT NEEDS-COMPOSITING-BITS-UPDATE
...      parentData: <none> (can use size)
...      constraints: BoxConstraints(w=379.4, 0.0<=h<=Infinity)
...      size: Size(379.4, 43.0)
...      listeners: enter, exit
...      cursor: SystemMouseCursor(click)
...      child: RenderSemanticsGestureHandler#215dd relayoutBoundary=up10 NEEDS-PAINT NEEDS-COMPOSITING-BITS-UPDATE
...        parentData: <none> (can use size)
...        constraints: BoxConstraints(w=379.4, 0.0<=h<=Infinity)
...        size: Size(379.4, 43.0)
...        gestures: tap
...        child: RenderPointerListener#b7a1a relayoutBoundary=up11 NEEDS-PAINT NEEDS-COMPOSITING-BITS-UPDATE
...          parentData: <none> (can use size)
...          constraints: BoxConstraints(w=379.4, 0.0<=h<=Infinity)
...          size: Size(379.4, 43.0)
...          behavior: opaque
...          listeners: down
...  child 2: RenderPadding#9c383 relayoutBoundary=up8 NEEDS-PAINT NEEDS-COMPOSITING-BITS-UPDATE
...    needs compositing
...    parentData: offset=Offset(0.0, 0.0); flex=null; fit=null (can use size)
...    constraints: BoxConstraints(w=379.4, 0.0<=h<=Infinity)
...    size: Size(379.4, 64.0)
...    padding: EdgeInsets(0.0, 8.0, 1.0, 8.0)
...    textDirection: ltr
...    child: RenderSemanticsAnnotations#71e7e relayoutBoundary=up9 NEEDS-PAINT NEEDS-COMPOSITING-BITS-UPDATE
...      needs compositing
...      parentData: offset=Offset(0.0, 8.0) (can use size)
...      constraints: BoxConstraints(w=378.4, 0.0<=h<=Infinity)
...      semantic boundary
...      size: Size(378.4, 48.0)
...      child: _RenderInputPadding#1c96b relayoutBoundary=up10 NEEDS-PAINT NEEDS-COMPOSITING-BITS-UPDATE
...        needs compositing
...        parentData: <none> (can use size)
...        constraints: BoxConstraints(w=378.4, 0.0<=h<=Infinity)
...        size: Size(378.4, 48.0)
...        child: RenderConstrainedBox#feb37 relayoutBoundary=up11 NEEDS-PAINT NEEDS-COMPOSITING-BITS-UPDATE
...          needs compositing
...          parentData: offset=Offset(0.0, 9.0) (can use size)
...          constraints: BoxConstraints(w=378.4, 0.0<=h<=Infinity)
...          size: Size(378.4, 30.0)
...          additionalConstraints: BoxConstraints(50.0<=w<=Infinity, 30.0<=h<=Infinity)
...  child 3: RenderSemanticsAnnotations#23aba relayoutBoundary=up8 NEEDS-PAINT NEEDS-COMPOSITING-BITS-UPDATE
...    parentData: offset=Offset(0.0, 0.0); flex=null; fit=null (can use size)
...    constraints: BoxConstraints(w=379.4, 0.0<=h<=Infinity)
...    size: Size(379.4, 16.0)
...    child: RenderMouseRegion#b2320 relayoutBoundary=up9 NEEDS-PAINT NEEDS-COMPOSITING-BITS-UPDATE
...      parentData: <none> (can use size)
...      constraints: BoxConstraints(w=379.4, 0.0<=h<=Infinity)
...      size: Size(379.4, 16.0)
...      listeners: enter, exit
...      cursor: SystemMouseCursor(click)
...      child: RenderSemanticsGestureHandler#48ed1 relayoutBoundary=up10 NEEDS-PAINT NEEDS-COMPOSITING-BITS-UPDATE
...        parentData: <none> (can use size)
...        constraints: BoxConstraints(w=379.4, 0.0<=h<=Infinity)
...        size: Size(379.4, 16.0)
...        gestures: tap
...        child: RenderPointerListener#bc8c8 relayoutBoundary=up11 NEEDS-PAINT NEEDS-COMPOSITING-BITS-UPDATE
...          parentData: <none> (can use size)
...          constraints: BoxConstraints(w=379.4, 0.0<=h<=Infinity)
...          size: Size(379.4, 16.0)
...          behavior: opaque
...          listeners: down
...  child 4: RenderConstrainedBox#160ca NEEDS-LAYOUT NEEDS-PAINT NEEDS-COMPOSITING-BITS-UPDATE
...    needs compositing
...    parentData: offset=Offset(0.0, 0.0); flex=1; fit=FlexFit.tight
...    constraints: MISSING
...    size: MISSING
...    additionalConstraints: BoxConstraints(0.0<=w<=Infinity, h=200.0)
...    child: RenderRepaintBoundary#cacc9 NEEDS-LAYOUT NEEDS-PAINT NEEDS-COMPOSITING-BITS-UPDATE
...      needs compositing
...      parentData: <none>
...      constraints: MISSING
...      size: MISSING
...      usefulness ratio: no metrics collected yet (never painted)
...      child: RenderCustomPaint#65dd2 NEEDS-LAYOUT NEEDS-PAINT NEEDS-COMPOSITING-BITS-UPDATE
...        needs compositing
...        parentData: <none>
...        constraints: MISSING
...        size: MISSING
...        child: RenderRepaintBoundary#5830f NEEDS-LAYOUT NEEDS-PAINT NEEDS-COMPOSITING-BITS-UPDATE
...          needs compositing
...          parentData: <none>
...          constraints: MISSING
...          size: MISSING
...          usefulness ratio: no metrics collected yet (never painted)
The creator information is set to: Column ← Padding ← ColoredBox ← ConstrainedBox ← Container ← _PointerListener ← Listener ← _GestureSemantics ← RawGestureDetector ← GestureDetector ← ComponentWidget<ChannelDetailState>-[<ChannelDetailComponent>] ← SliverToBoxAdapter ← ⋯

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=BUG.md
The relevant error-causing widget was: 
  Column file:///Users/dolphin/Documents/GitHub/cruise-open/lib/src/page/channel/channeldetail_component/view.dart:89:18
When the exception was thrown, this was the stack: 
#0      RenderFlex.performLayout.<anonymous closure> (package:flutter/src/rendering/flex.dart:717:11)
#1      RenderFlex.performLayout (package:flutter/src/rendering/flex.dart:744:10)
#2      RenderObject.layout (package:flutter/src/rendering/object.dart:1777:7)
#3      RenderPadding.performLayout (package:flutter/src/rendering/shifted_box.dart:207:12)
#4      RenderObject.layout (package:flutter/src/rendering/object.dart:1777:7)
...
The following RenderObject was being processed when the exception was fired: RenderFlex#681a6 relayoutBoundary=up7 NEEDS-LAYOUT NEEDS-PAINT NEEDS-COMPOSITING-BITS-UPDATE
...  needs compositing
...  parentData: offset=Offset(16.0, 16.0) (can use size)
...  constraints: BoxConstraints(w=379.4, 706.5<=h<=Infinity)
...  size: MISSING
...  direction: vertical
...  mainAxisAlignment: start
...  mainAxisSize: min
...  crossAxisAlignment: stretch
...  verticalDirection: down
RenderObject: RenderFlex#681a6 relayoutBoundary=up7 NEEDS-LAYOUT NEEDS-PAINT NEEDS-COMPOSITING-BITS-UPDATE
  needs compositing
  parentData: offset=Offset(16.0, 16.0) (can use size)
  constraints: BoxConstraints(w=379.4, 706.5<=h<=Infinity)
  size: MISSING
  direction: vertical
  mainAxisAlignment: start
  mainAxisSize: min
  crossAxisAlignment: stretch
  verticalDirection: down
...  child 1: RenderSemanticsAnnotations#e7e5a relayoutBoundary=up8 NEEDS-PAINT NEEDS-COMPOSITING-BITS-UPDATE
...    parentData: offset=Offset(0.0, 0.0); flex=null; fit=null (can use size)
...    constraints: BoxConstraints(w=379.4, 0.0<=h<=Infinity)
...    size: Size(379.4, 43.0)
...    child: RenderMouseRegion#1b035 relayoutBoundary=up9 NEEDS-PAINT NEEDS-COMPOSITING-BITS-UPDATE
...      parentData: <none> (can use size)
...      constraints: BoxConstraints(w=379.4, 0.0<=h<=Infinity)
...      size: Size(379.4, 43.0)
...      listeners: enter, exit
...      cursor: SystemMouseCursor(click)
...      child: RenderSemanticsGestureHandler#215dd relayoutBoundary=up10 NEEDS-PAINT NEEDS-COMPOSITING-BITS-UPDATE
...        parentData: <none> (can use size)
...        constraints: BoxConstraints(w=379.4, 0.0<=h<=Infinity)
...        size: Size(379.4, 43.0)
...        gestures: tap
...        child: RenderPointerListener#b7a1a relayoutBoundary=up11 NEEDS-PAINT NEEDS-COMPOSITING-BITS-UPDATE
...          parentData: <none> (can use size)
...          constraints: BoxConstraints(w=379.4, 0.0<=h<=Infinity)
...          size: Size(379.4, 43.0)
...          behavior: opaque
...          listeners: down
...  child 2: RenderPadding#9c383 relayoutBoundary=up8 NEEDS-PAINT NEEDS-COMPOSITING-BITS-UPDATE
...    needs compositing
...    parentData: offset=Offset(0.0, 0.0); flex=null; fit=null (can use size)
...    constraints: BoxConstraints(w=379.4, 0.0<=h<=Infinity)
...    size: Size(379.4, 64.0)
...    padding: EdgeInsets(0.0, 8.0, 1.0, 8.0)
...    textDirection: ltr
...    child: RenderSemanticsAnnotations#71e7e relayoutBoundary=up9 NEEDS-PAINT NEEDS-COMPOSITING-BITS-UPDATE
...      needs compositing
...      parentData: offset=Offset(0.0, 8.0) (can use size)
...      constraints: BoxConstraints(w=378.4, 0.0<=h<=Infinity)
...      semantic boundary
...      size: Size(378.4, 48.0)
...      child: _RenderInputPadding#1c96b relayoutBoundary=up10 NEEDS-PAINT NEEDS-COMPOSITING-BITS-UPDATE
...        needs compositing
...        parentData: <none> (can use size)
...        constraints: BoxConstraints(w=378.4, 0.0<=h<=Infinity)
...        size: Size(378.4, 48.0)
...        child: RenderConstrainedBox#feb37 relayoutBoundary=up11 NEEDS-PAINT NEEDS-COMPOSITING-BITS-UPDATE
...          needs compositing
...          parentData: offset=Offset(0.0, 9.0) (can use size)
...          constraints: BoxConstraints(w=378.4, 0.0<=h<=Infinity)
...          size: Size(378.4, 30.0)
...          additionalConstraints: BoxConstraints(50.0<=w<=Infinity, 30.0<=h<=Infinity)
...  child 3: RenderSemanticsAnnotations#23aba relayoutBoundary=up8 NEEDS-PAINT NEEDS-COMPOSITING-BITS-UPDATE
...    parentData: offset=Offset(0.0, 0.0); flex=null; fit=null (can use size)
...    constraints: BoxConstraints(w=379.4, 0.0<=h<=Infinity)
...    size: Size(379.4, 16.0)
...    child: RenderMouseRegion#b2320 relayoutBoundary=up9 NEEDS-PAINT NEEDS-COMPOSITING-BITS-UPDATE
...      parentData: <none> (can use size)
...      constraints: BoxConstraints(w=379.4, 0.0<=h<=Infinity)
...      size: Size(379.4, 16.0)
...      listeners: enter, exit
...      cursor: SystemMouseCursor(click)
...      child: RenderSemanticsGestureHandler#48ed1 relayoutBoundary=up10 NEEDS-PAINT NEEDS-COMPOSITING-BITS-UPDATE
...        parentData: <none> (can use size)
...        constraints: BoxConstraints(w=379.4, 0.0<=h<=Infinity)
...        size: Size(379.4, 16.0)
...        gestures: tap
...        child: RenderPointerListener#bc8c8 relayoutBoundary=up11 NEEDS-PAINT NEEDS-COMPOSITING-BITS-UPDATE
...          parentData: <none> (can use size)
...          constraints: BoxConstraints(w=379.4, 0.0<=h<=Infinity)
...          size: Size(379.4, 16.0)
...          behavior: opaque
...          listeners: down
...  child 4: RenderConstrainedBox#160ca NEEDS-LAYOUT NEEDS-PAINT NEEDS-COMPOSITING-BITS-UPDATE
...    needs compositing
...    parentData: offset=Offset(0.0, 0.0); flex=1; fit=FlexFit.tight
...    constraints: MISSING
...    size: MISSING
...    additionalConstraints: BoxConstraints(0.0<=w<=Infinity, h=200.0)
...    child: RenderRepaintBoundary#cacc9 NEEDS-LAYOUT NEEDS-PAINT NEEDS-COMPOSITING-BITS-UPDATE
...      needs compositing
...      parentData: <none>
...      constraints: MISSING
...      size: MISSING
...      usefulness ratio: no metrics collected yet (never painted)
...      child: RenderCustomPaint#65dd2 NEEDS-LAYOUT NEEDS-PAINT NEEDS-COMPOSITING-BITS-UPDATE
...        needs compositing
...        parentData: <none>
...        constraints: MISSING
...        size: MISSING
...        child: RenderRepaintBoundary#5830f NEEDS-LAYOUT NEEDS-PAINT NEEDS-COMPOSITING-BITS-UPDATE
...          needs compositing
...          parentData: <none>
...          constraints: MISSING
...          size: MISSING
...          usefulness ratio: no metrics collected yet (never painted)
====================================================================================================

======== Exception caught by rendering library =====================================================
RenderBox was not laid out: RenderFlex#681a6 relayoutBoundary=up7 NEEDS-PAINT NEEDS-COMPOSITING-BITS-UPDATE
'package:flutter/src/rendering/box.dart':
Failed assertion: line 1785 pos 12: 'hasSize'
The relevant error-causing widget was: 
  Padding file:///Users/dolphin/Documents/GitHub/cruise-open/lib/src/page/channel/channeldetail_component/view.dart:85:16
====================================================================================================

======== Exception caught by rendering library =====================================================
RenderBox was not laid out: RenderPadding#9b6ca relayoutBoundary=up6 NEEDS-PAINT NEEDS-COMPOSITING-BITS-UPDATE
'package:flutter/src/rendering/box.dart':
Failed assertion: line 1785 pos 12: 'hasSize'
The relevant error-causing widget was: 
  Container file:///Users/dolphin/Documents/GitHub/cruise-open/lib/src/page/channel/channeldetail_component/view.dart:80:14

我已经尝试在 Expand 的外部添加 SizedBoxNestedScrollView 但仍然没有解决问题,我该怎么做才能解决它?

1 个答案:

答案 0 :(得分:1)

像这样使用带有 GestureDetector 的 SingleChildScrollView

return SingleChildScrollView (
child:
 GestureDetector(
      onHorizontalDragStart: _onHorizontalDragStart,
      onHorizontalDragUpdate: _onHorizontalDragUpdate,
      onHorizontalDragEnd: _onHorizontalDragEnd,
      child: Container(
        constraints: BoxConstraints(
          minHeight: MediaQuery.of(context).size.height * 0.9,
        ),
        color: Theme.of(context).scaffoldBackgroundColor,
        child: Padding(
          padding: const EdgeInsets.all(
            16.0,
          ),
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.stretch,
            mainAxisSize: MainAxisSize.min,
            children: [
              InkWell(
                onTap: () => {},
                child: Padding(
                  padding: const EdgeInsets.only(bottom: 8.0),
                  child: Container(
                    child: Text(
                      item.subName == "" ? "Comment" : item.subName,
                      style: Theme.of(context).textTheme.headline5.copyWith(
                            fontWeight: FontWeight.w600,
                          ),
                    ),
                  ),
                ),
              ),
              if (item.isFav == 1)
                Padding(
                  padding: const EdgeInsets.only(top: 8, bottom: 8.0, right: 1),
                  child: ButtonTheme(
                      minWidth: 50,
                      height: 30.0,
                      child: RaisedButton.icon(
                        color: Theme.of(context).primaryColor,
                        icon: Icon(
                          Feather.check_circle,
                          size: 16,
                          color: Theme.of(context).canvasColor,
                        ),
                        shape: new RoundedRectangleBorder(borderRadius: new BorderRadius.circular(5.0)),
                        onPressed: () => touchSub(item.id.toString(), SubStatus.UNSUB),
                        label: Text("已订阅"),
                      )),
                ),
              if (item.isFav != 1)
                Padding(
                  padding: const EdgeInsets.only(top: 8, bottom: 8.0, right: 1),
                  child: ButtonTheme(
                      minWidth: 50,
                      height: 30.0,
                      child: RaisedButton(
                        color: Theme.of(context).primaryColor,
                        shape: new RoundedRectangleBorder(borderRadius: new BorderRadius.circular(5.0)),
                        onPressed: () => touchSub(item.id.toString(), SubStatus.SUB),
                        child: Text("订阅"),
                      )),
                ),
              InkWell(
                onTap: () {
                  Navigator.push(
                    context,
                    MaterialPageRoute(builder: (context) => ProfilePage(username: item.author)),
                  );
                },
                child: RichText(
                  text: TextSpan(
                    children: <TextSpan>[
                      TextSpan(
                        text: item.author,
                        style: Theme.of(context).textTheme.caption.copyWith(
                              color: Theme.of(context).primaryColor,
                            ),
                      ),
                    ],
                  ),
                ),
              ),
              if (item.content != "")
                Html(
                  data: item.content,
                  style: {
                    "body": Style(
                      fontSize: FontSize(19.0),
                    ),
                  },
                  onLinkTap: (url) => launchUrl(url),
                ),
              if (item.parts.isNotEmpty)
                Padding(
                  padding: const EdgeInsets.only(top: 8.0),
                ),
              Expanded(
                  child: SizedBox(
                      height: 200.0,
                      child: NestedScrollView(
                        headerSliverBuilder: (BuildContext context, bool innerBoxIsScrolled) {
                          return <Widget>[];
                        },
                        body: viewService.buildComponent("homelistdefault"),
                      )))
            ],
          ),
        ),
      )));