我正在使用对齐在我的堆栈中定位小部件。当我在堆栈中的任何子项中使用对齐时,堆栈会占据全屏。否则堆栈是最高的 child 的高度。我在这里很困惑。 align 不是用来对齐堆栈中的 child 吗?
这是我的代码:
bottomNavigationBar: Stack(
children: <Widget>[
Container(
color: Colors.greenAccent,
width: double.infinity,
height: 45,
),
Align(
alignment: Alignment.bottomCenter,
child: Container(
width: 80,
height: 80,
decoration: BoxDecoration(color: Colors.deepPurpleAccent, shape: BoxShape.circle),
child: IconButton(icon: Icon(Icons.search), onPressed: (){}),
),
)
],
)
现在,当我从我的第二个 child 中删除 Align 小部件时,堆栈和它的 child 一样大。
最佳答案
您没有为其 widthFactor
和 heightFactor
提供 Align
小部件值,根据 Align class documentation ,您提到的行为是预期的:
This widget will be as big as possible if its dimensions are constrained and widthFactor and heightFactor are null.
为了确认这一点,我将值 1.0
分配给 widthFactor
和 heightFactor
,然后它返回到预期大小(更大的小部件,即 FloatingActionButton
) :
Align(
alignment: Alignment.bottomCenter,
widthFactor: 1.0,
heightFactor: 1.0,
child: Container(
width: 80,
height: 80,
decoration: BoxDecoration(color: Colors.deepPurpleAccent, shape: BoxShape.circle),
child: IconButton(icon: Icon(Icons.search), onPressed: (){}),
),
)
关于flutter - Align Widget inside Stack 占全屏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56199162/