【问题标题】:Flutter - Positioned Widget in Stack causing ExceptionFlutter - 堆栈中的定位小部件导致异常
【发布时间】:2019-02-10 22:28:05
【问题描述】:

当我尝试使用 Positioned 小部件封装 PandemicCard 时出现以下异常。如果我单独渲染卡片/无定位小部件,它工作得很好。

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 ←

对于这个代码。谁能帮我弄清楚我做错了什么?

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();
}

【问题讨论】:

  • 您不能只将定位小部件作为堆栈小部件的子级。您必须至少有一个未定位的小部件。

标签: dart flutter flutter-layout flutter-positioned


【解决方案1】:

添加一个空的Container 作为堆栈的child

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

【讨论】:

  • 因为Stack 小部件必须至少有一个在构建时可以具有静态大小的项目
  • 我还必须添加 overflow: Overflow.visible 到 Stack,以使项目可见。
  • @MazinIbrahim 这不是真的。只需阅读RenderStack 文档即可。我将复制与您的陈述相关的部分:[...] If there are no non-positioned children, the stack becomes as large as possible [...]
【解决方案2】:

其他解决方法:

(a) 将堆栈包裹在一个已知高度的容器上:

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

(b) 按照建议添加空 Container() 和 Clip.none:

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

【讨论】:

    猜你喜欢
    • 2012-06-03
    • 2019-07-04
    • 2019-07-29
    • 2019-07-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-11-09
    相关资源
    最近更新 更多