[英]Flutter error when wrapping a Positioned widget inside Stack and Column (A RenderFlex overflowed by Infinity pixels.)
I have a use case where I have to use Stack
and Positioned
to make some overlapping widgets.我有一个用例,我必须使用
Stack
和Positioned
来制作一些重叠的小部件。 But I also have to wrap that inside a Column
.但我还必须将其包装在
Column
中。 When I do that then I get an error当我这样做时,我得到一个错误
A RenderFlex overflowed by Infinity pixels on the bottom.
A minimal example showing the issue:显示问题的最小示例:
import "package:flutter/material.dart";
class TestScreen extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: Text("Example"),
),
body: Column(
children: [
Stack(children: [
cardWidget(),
]),
],
),
),
);
}
Widget cardWidget() {
return Positioned(
left: 10.0,
child: Card(
color: Colors.green,
child: Container(
height: 200,
width: 100,
),
),
);
}
}
If I remove left: 10
then it works fine but thats not what I want.如果我删除
left: 10
那么它可以正常工作,但这不是我想要的。
The full error message (which I don't really understand):完整的错误信息(我不太明白):
The following assertion was thrown while applying parent data.:
Incorrect use of ParentDataWidget.
The ParentDataWidget Positioned(left: 10.0) wants to apply ParentData of type StackParentData to a
RenderObject, which has been set up to accept ParentData of incompatible type FlexParentData.
Usually, this means that the Positioned widget has the wrong ancestor RenderObjectWidget. Typically,
Positioned widgets are placed directly inside Stack widgets.
The offending Positioned is currently placed inside a Column widget.
The ownership chain for the RenderObject that received the incompatible parent data was:
Semantics ← Card ← Positioned ← Column ← _BodyBuilder ← MediaQuery ←
LayoutId-[<_ScaffoldSlot.body>] ← CustomMultiChildLayout ← AnimatedBuilder ← DefaultTextStyle ← ⋯
Turns out I need to wrap my Stack
with Expanded
.原来我需要用
Expanded
包装我的Stack
。 Not sure why.不知道为什么。
import "package:flutter/material.dart";
class TestScreen extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: Text("Example"),
),
body: Column(
crossAxisAlignment: CrossAxisAlignment.center,
children: [
Flexible(
child: Stack(
children: [0, 1, 2, 3].map((e) => cardWidget(e)).toList(),
),
),
],
),
),
);
}
Widget cardWidget(int index) {
return Positioned(
left: index * 10.0,
child: Card(
elevation: 10,
color: Colors.green,
child: Container(
height: 200,
width: 100,
),
),
);
}
}
The issue with this solution is that it won't center my cards (neither horizontal or vertical).这个解决方案的问题是它不会使我的卡片居中(无论是水平的还是垂直的)。 How is that achieved?
这是如何实现的?
This is from the Flutter docs, for the Column class.这来自 Flutter 文档,用于 class 列。
One common reason for this to happen is that the Column has been placed in another Column (without using Expanded or Flexible around the inner nested Column).发生这种情况的一个常见原因是 Column 已放置在另一个 Column 中(没有在内部嵌套 Column 周围使用 Expanded 或 Flexible)。 When a Column lays out its non-flex children (those that have neither Expanded or Flexible around them), it gives them unbounded constraints so that they can determine their own dimensions (passing unbounded constraints usually signals to the child that it should shrink-wrap its contents).
当一个 Column 布置它的非 flex 子节点时(那些既没有 Expanded 也没有 Flexible 的子节点),它会给它们无限的约束,以便它们可以确定自己的尺寸(传递无限约束通常会向子节点发出信号,它应该收缩包装其内容)。 The solution in this case is typically to just wrap the inner column in an Expanded to indicate that it should take the remaining space of the outer column, rather than being allowed to take any amount of room it desires.
在这种情况下,解决方案通常只是将内列包裹在 Expanded 中,以指示它应该占用外列的剩余空间,而不是被允许占用它想要的任何空间。
This is enough to understand what the problem is这足以了解问题所在
The Column is giving the Stack unbounded constraints, due to which you're getting overflow error. Column为 Stack 提供了无限的约束,因此您会遇到溢出错误。
Putting it inside an Expanded widget however, solves it as Expanded is a flex-widget and it tries to cover entire available space.然而,将它放在Expanded小部件中,可以解决它,因为Expanded是一个 flex-widget,它试图覆盖整个可用空间。 It then gives constraints to the Stack which prevents it from overflow.
然后它为堆栈提供约束,以防止它溢出。
Hope I helped.希望我有所帮助。
You can add symmetric Padding
to the container to center the Stack
.您可以向容器添加对称
Padding
以使Stack
居中。 Also, add alignment: AlignmentDirectional.center
to the Stack
to center the widgets inside it(they will only be vertically centered, since they are Positioned
widgets with left
parameter provided).此外,将
alignment: AlignmentDirectional.center
添加到Stack
中以将其中的小部件居中(它们只会垂直居中,因为它们是提供了left
参数的Positioned
小部件)。
@override
Widget build(BuildContext context) {
return Scaffold(
body: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Flexible(
child: Container(
padding: EdgeInsets.symmetric(horizontal: 80.0),
child: Stack(
alignment: AlignmentDirectional.center,
children: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9].map((e) => cardWidget(e)).toList(),
),
),
),
],
),
);
}
Widget cardWidget(int index) {
return Positioned(
left: index * 50.0,
child: Card(
elevation: 10,
color: Colors.green,
child: Container(
height: 200,
width: 100,
),
),
);
}
Note : adjust the padding amount according to the cards' size and the screen size.注意:根据卡片大小和屏幕大小调整填充量。
Result:结果:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.