Flutter:将ListView以动画形式定位在容器中,成为堆栈的一部分

问题描述

也许你们其中之一已经遇到了这些问题。我花了很多时间试图将ListView(我也尝试过ExpansionPanelList)放到一个容器中,该容器从右边缘平移到现有小部件的屏幕(帮助菜单)上。我已经创建了一个从基本ListView开始的模型,然后又逐个功能添加一个模型。但是现在我被困住了,错误消息对我没有多大帮助。

一旦添加AnimatedPositioned,就会抛出这些异常。主要消息似乎是 constraints.hasBoundedWidth'的问题:不正确。

anchor

异常消息的下部重复多次,每个动画步骤可能重复一次。然后消息以下面的消息结束,其中引用容器是AnimatedPositiones> IgnorePointer> Container

之一。
════════ Exception caught by rendering library ═════════════════════════════════════════════════════
The following assertion was thrown during performlayout():
'package:Flutter/src/rendering/viewport.dart': Failed assertion: line 1754 pos 16: 'constraints.hasBoundedWidth': 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: 
  ListView file:///Users/holger/IdeaProjects/math/lib/tmp.dart:496:40
When the exception was thrown,this was the stack: 
#2      RenderShrinkWrappingViewport.performlayout (package:Flutter/src/rendering/viewport.dart:1754:16)
#3      RenderObject.layout (package:Flutter/src/rendering/object.dart:1775:7)
#4      RenderProxyBoxMixin.performlayout (package:Flutter/src/rendering/proxy_Box.dart:115:13)
#5      RenderObject.layout (package:Flutter/src/rendering/object.dart:1775:7)
#6      RenderProxyBoxMixin.performlayout (package:Flutter/src/rendering/proxy_Box.dart:115:13)
...
The following RenderObject was being processed when the exception was fired: RenderShrinkWrappingViewport#a1875 relayoutBoundary=up7 NEEDS-LAYOUT NEEDS-PAINT NEEDS-COMPOSITING-BITS-UPDATE
...  needs compositing
...  parentData: <none> (can use size)
...  constraints: BoxConstraints(0.0<=w<=Infinity,0.0<=h<=716.8)
...  size: MISSING
...  axisDirection: down
...  crossAxisDirection: right
...  offset: ScrollPositionWithSingleContext#20c01(offset: 0.0,range: null..null,viewport: null,ScrollableState,AlwaysScrollableScrollPhysics -> BouncingScrollPhysics,IdleScrollActivity#3e990,ScrollDirection.idle)
RenderObject: RenderShrinkWrappingViewport#a1875 relayoutBoundary=up7 NEEDS-LAYOUT NEEDS-PAINT NEEDS-COMPOSITING-BITS-UPDATE
  needs compositing
  parentData: <none> (can use size)
  constraints: BoxConstraints(0.0<=w<=Infinity,0.0<=h<=716.8)
  size: MISSING
  axisDirection: down
  crossAxisDirection: right
  offset: ScrollPositionWithSingleContext#20c01(offset: 0.0,ScrollDirection.idle)
...  child 0: RenderSliverPadding#078d5 NEEDS-LAYOUT NEEDS-PAINT NEEDS-COMPOSITING-BITS-UPDATE
...    parentData: layoutOffset=None
...    constraints: MISSING
...    geometry: null
...    padding: EdgeInsets.zero
...    textDirection: ltr
...    child: RenderSliverList#fd0a6 NEEDS-LAYOUT NEEDS-PAINT
...      parentData: paintOffset=Offset(0.0,0.0)
...      constraints: MISSING
...      geometry: null
...      no children current live
════════════════════════════════════════════════════════════════════════════════════════════════════

════════ Exception caught by rendering library ═════════════════════════════════════════════════════
RenderBox was not laid out: RenderShrinkWrappingViewport#a1875 relayoutBoundary=up7 NEEDS-PAINT NEEDS-COMPOSITING-BITS-UPDATE
'package:Flutter/src/rendering/Box.dart':
Failed assertion: line 1713 pos 12: 'hasSize'
The relevant error-causing widget was: 
  ListView file:///Users/holger/IdeaProjects/math/lib/tmp.dart:496:40

ListView看起来像这样,它显示了不同标签标题,每个标题都可以扩展以显示详细信息。

════════ Exception caught by rendering library ═════════════════════════════════════════════════════
The following assertion was thrown during paint():
RenderBox was not laid out: RenderDecoratedBox#a83d8
'package:Flutter/src/rendering/Box.dart':
Failed assertion: line 1713 pos 12: 'hasSize'


Either the assertion indicates an error in the framework itself,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: 
  Container file:///Users/holger/IdeaProjects/math/lib/tmp3.dart:474:32
When the exception was thrown,this was the stack: 
#2      RenderBox.size (package:Flutter/src/rendering/Box.dart:1713:12)
#3      RenderDecoratedBox.paint (package:Flutter/src/rendering/proxy_Box.dart:2067:12)
#4      RenderObject._paintWithContext (package:Flutter/src/rendering/object.dart:2307:7)
#5      PaintingContext.paintChild (package:Flutter/src/rendering/object.dart:191:13)
#6      RenderProxyBoxMixin.paint (package:Flutter/src/rendering/proxy_Box.dart:133:15)
...
The following RenderObject was being processed when the exception was fired: RenderDecoratedBox#a83d8
...  needs compositing
...  parentData: <none> (can use size)
...  constraints: BoxConstraints(w=331.2,h=716.8)
...  size: MISSING
...  decoration: Boxdecoration
...    color: Color(0xffffffff)
...  configuration: ImageConfiguration(bundle: PlatformAssetBundle#b6597(),devicePixelRatio: 3.0,locale: en_US,textDirection: TextDirection.ltr,platform: iOS)
RenderObject: RenderDecoratedBox#a83d8
  needs compositing
  parentData: <none> (can use size)
  constraints: BoxConstraints(w=331.2,h=716.8)
  size: MISSING
  decoration: Boxdecoration
    color: Color(0xffffffff)
  configuration: ImageConfiguration(bundle: PlatformAssetBundle#b6597(),platform: iOS)
...  child: RenderFlex#28045 NEEDS-PAINT
...    needs compositing
...    parentData: <none> (can use size)
...    constraints: BoxConstraints(w=331.2,h=716.8)
...    size: MISSING
...    direction: horizontal
...    mainAxisAlignment: start
...    mainAxisSize: max
...    crossAxisAlignment: center
...    textDirection: ltr
...    verticalDirection: down
...    textBaseline: alphabetic
...    child 1: RenderPadding#b537d relayoutBoundary=up1 NEEDS-PAINT
...      parentData: offset=Offset(0.0,0.0); flex=null; fit=null (can use size)
...      constraints: BoxConstraints(0.0<=w<=Infinity,0.0<=h<=716.8)
...      size: Size(59.0,716.8)
...      padding: EdgeInsets(3.0,0.0,0.0)
...      textDirection: ltr
...      child: RenderFlex#4c2a2 relayoutBoundary=up2 NEEDS-PAINT
...        parentData: offset=Offset(3.0,0.0) (can use size)
...        constraints: BoxConstraints(0.0<=w<=Infinity,0.0<=h<=716.8)
...        size: Size(56.0,716.8)
...        direction: vertical
...        mainAxisAlignment: spaceEvenly
...        mainAxisSize: max
...        crossAxisAlignment: center
...        verticalDirection: down
...        child 1: RenderSemanticsGestureHandler#c07e2 relayoutBoundary=up3 NEEDS-PAINT
...          parentData: offset=Offset(0.0,330.4); flex=null; fit=null (can use size)
...          constraints: BoxConstraints(unconstrained)
...          size: Size(56.0,56.0)
...          gestures: tap
...    child 2: _RenderScrollSemantics#80100 relayoutBoundary=up1 NEEDS-PAINT
...      needs compositing
...      parentData: offset=Offset(0.0,0.0<=h<=716.8)
...      semantic boundary
...      size: MISSING
...      child: RenderPointerListener#695c7 relayoutBoundary=up2 NEEDS-PAINT
...        needs compositing
...        parentData: <none> (can use size)
...        constraints: BoxConstraints(0.0<=w<=Infinity,0.0<=h<=716.8)
...        size: MISSING
...        behavior: defertochild
...        listeners: signal
...        child: RenderSemanticsGestureHandler#db442 relayoutBoundary=up3 NEEDS-PAINT
...          needs compositing
...          parentData: <none> (can use size)
...          constraints: BoxConstraints(0.0<=w<=Infinity,0.0<=h<=716.8)
...          size: MISSING
...          gestures: <none>
════════════════════════════════════════════════════════════════════════════════════════════════════

════════ Exception caught by rendering library ═════════════════════════════════════════════════════
RenderBox was not laid out: RenderDecoratedBox#a83d8
'package:Flutter/src/rendering/Box.dart':
Failed assertion: line 1713 pos 12: 'hasSize'
The relevant error-causing widget was: 
  Container file:///Users/holger/IdeaProjects/math/lib/tmp3.dart:474:32

AnimatedPositioned是一个小部件

AnimatedPositioned(duration: Duration(milliseconds: 800),left: _drawerLeft,top:5,child: IgnorePointer(ignoring: (_drawerLeft <= MediaQuery.of(context).size.width / 3 ? false:true),child: Container(
                          height:0.8*1 * MediaQuery.of(context).size.height,width:0.8 * MediaQuery.of(context).size.width,child: Row(
                            mainAxisAlignment: MainAxisAlignment.start,children: <Widget>[                           
                                    CupertinoButton(
                                      child: Icon(fwdBtn),onpressed: () {
                                        setState(() {
                                          _drawerLeft = 1 * MediaQuery.of(context).size.width;
                                          controller.reverse();
                                        });
                                      },),ListView.builder(
                                scrollDirection: Axis.vertical,shrinkWrap: true,itemCount: usedTagData.length,itemBuilder: (BuildContext context,int index) {
                                  return Column(
                                    children: <Widget>[
                                      Row(
                                        mainAxisAlignment: MainAxisAlignment.spaceBetween,children: <Widget>[
                                          Text(usedTagData[index].header,style: TextStyle(color: CupertinoColors.link)),CupertinoButton(
                                            child: Icon(dwnBtn),onpressed: () {
                                              setState(() {
                                                usedTagData[index].isExpanded = !usedTagData[index].isExpanded;
                                              });
                                            },],(usedTagData[index].isExpanded ?
                                        Text(usedTagData[index].content)
                                      :
                                        Container()
                                      ),]
                                  );
                                }
                             ),)),)

您是否知道如何进行此操作,或者异常消息为您提供了任何提示

解决方法

ListViewRow一起使用的唯一方法是设置预定义的宽度。您的ListView尝试无限扩展,因此您应该通过设置预定义的宽度来限制它。 这是一个示例:

Row(
 children:[
  SizedBox(
    width:200,child:ListView()
  ),]),