简体   繁体   English

Flutter - 堆栈中的定位小部件导致异常

[英]Flutter - Positioned Widget in Stack causing Exception

I'm getting the below exception when I try to encapsulate the PandemicCard with a Positioned widget.当我尝试使用 Positioned 小部件封装 PandemicCard 时,出现以下异常。 If I render the card lone/no Positioned widget, it works just fine.如果我渲染卡片单独/没有定位小部件,它工作得很好。

I/flutter ( 7331): ══╡ EXCEPTION CAUGHT BY RENDERING LIBRARY ╞═════════════════════════════════════════════════════════
I/flutter ( 7331): The following assertion was thrown during performLayout():
I/flutter ( 7331): RenderStack object was given an infinite size during layout.
I/flutter ( 7331): This probably means that it is a render object that tries to be as big as possible, but it was put
I/flutter ( 7331): inside another render object that allows its children to pick their own size.
I/flutter ( 7331): The nearest ancestor providing an unbounded height constraint is:
I/flutter ( 7331):   RenderFlex#2b18c relayoutBoundary=up2 NEEDS-LAYOUT NEEDS-PAINT OVERFLOWING
I/flutter ( 7331):   creator: Column ← Center ← MediaQuery ← LayoutId-[<_ScaffoldSlot.body>] ← CustomMultiChildLayout ←

For this code.对于这段代码。 Anyone able to help me figure out what I'm doing wrong?任何人都可以帮助我弄清楚我做错了什么?

class PandemicCard extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Container(
      height: 120.0,
      width: 76.0,
      decoration: BoxDecoration(
          color: Colors.blue,
          boxShadow: [
            BoxShadow(
                blurRadius: 5.0,
                color: Colors.grey)
          ]),
      child: Text('Hi'),
    );
  }
}

class PandemicCardStackState extends State<PandemicCardStack> {
  // final _cards = <PandemicCard>[ PandemicCard(), PandemicCard()];
  final _cards = <PandemicCard>[ PandemicCard()];

  @override
  Widget build( BuildContext context) {
    return Stack(
      // This Bombs!
      children: <Widget>[ Positioned( left: 0.0, top: 0.0, child: _cards[0])]
      // This works!
      // children: <Widget>[ _cards[0]]
    );
  }
}

class PandemicCardStack extends StatefulWidget {
  @override
  PandemicCardStackState createState() => PandemicCardStackState();
}

Add an empty Container as a child of the stack : 添加一个空Container作为堆栈的child

@override
Widget build( BuildContext context) {
return Stack(
  children: <Widget>[ 
    Container(),
    Positioned( left: 0.0, top: 0.0, child: _cards[0]),
   ]
  );
 }

Other workarounds:其他解决方法:

(a) Wrap the Stack on a Container with a known height: (a)将 Stack 包裹在一个已知高度的容器上:

Container(
  height: 50,
  child: Stack(
    children: [
      Positioned(top: 10, left: 10, child: Text('My child'))
  ]),
),

(b) As suggested add empty Container() and Clip.none: (b)按照建议添加空的 Container() 和 Clip.none:

Stack(
  clipBehavior: Clip.none,
  children: [
    Container(),
    Positioned(top: 10, left: 10, child: Text('My child'))
  ]),

Since this is still an issue.因为这仍然是一个问题。 I also came across another option.我还遇到了另一种选择。

You can wrap your stack in an IntrinsicHeight widget as well.您也可以将堆栈包装在 IntrinsicHeight 小部件中。

IntrinsicHeight(
    child: 
        Stack( 
            children: [Positioned(top: 10, left: 10, child:Text('My child')
        )]
    ),
),

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM