使用List.view时如何解决RenderStack错误

时间:2019-06-19 15:08:57

标签: flutter dart flutter-layout

我想在listView内实现这样的事情:

enter image description here

但是当我实现下面的代码时,出现渲染框错误:

  
    

RenderStack对象在布局期间被赋予了无限大小。 I / flutter(16418):正在处理以下RenderObject时     引发了异常:I / flutter(16418):RenderStack#10b27     relayoutBoundary = up5 NEEDS-LAYOUT NEEDS-PAINT I / flutter(16418):
    创建者:堆栈←RepaintBoundary-[<0>]←IndexedSemantics←I / flutter     (16418):NotificationListener←保持活动←     AutomaticKeepAlive←SliverList←I / flutter(16418):MediaQuery←     SliverPadding←视口←IgnorePointer- [GlobalKey#7cbed]←语义     ←⋯I / flutter(16418):parentData:(可以使用大小)I / flutter     (16418):约束条件:BoxConstraints(w = 411.4,0.0 <= h <= Infinity)     I /颤振(16418):大小:大小(411.4,Infinity)I /颤振(16418):
    对齐:中心I /颤振(16418):文本方向:LTR I /颤振     (16418):适合:宽松的I /颤振(16418):溢出:剪辑

  

如何解决此问题?

代码:

Scaffold(
        body: ListView.builder(
          itemCount: model.data.length,
          itemBuilder: (BuildContext context, int index) {
            return _buildCard(context, index);
          },
        )
        );
  }
}

Widget _buildCard(
    BuildContext context, int index) {
  return Stack(
    alignment: Alignment.center,
    children: <Widget>[
      Positioned(
        top: 80.0,
        right: 30.0,
        left: 30.0,
        child: Container(
          height: MediaQuery.of(context).size.height * 0.12,
          decoration: BoxDecoration(
              color: Colors.black),
        ),
      ),
      Positioned(
        top: 20,
        child: Container(
          height: 100,
          width: 100,
          decoration: BoxDecoration(
              color: Colors.white,
              shape: BoxShape.circle,
              border: Border.all(color: Colors.black, width: 13.0)),
        ),
      )
    ],
  );

1 个答案:

答案 0 :(得分:0)

您必须给堆栈一个小部件,堆栈的大小将与第一个小部件相同。

否则,堆栈没有大小,并且无法定位。

Widget _buildCard(BuildContext context, int index) {
return Stack(
    alignment: Alignment.center,
    children: <Widget>[
        Container(
        height: MediaQuery
            .of(context)
            .size
            .height * 0.12,
        decoration: BoxDecoration(
        color: Colors.black),
        ),
        Positioned(
            top: 80.0,
            right: 30.0,
            left: 30.0,
            child: Container(
                height: MediaQuery
                    .of(context)
                    .size
                    .height * 0.12,
                decoration: BoxDecoration(
                    color: Colors.black),
            ),
        ),
        Positioned(
            top: 20,
            child: Container(
                height: 100,
                width: 100,
                decoration: BoxDecoration(
                    color: Colors.white,
                    shape: BoxShape.circle,
                    border: Border.all(color: Colors.black, width: 13.0)),
            ),
        )
    ],
);

}