我想在 Stack 的顶部中心放置一个小部件,但它被拉伸(stretch)到父级的宽度,如下面的屏幕截图所示:
这是我使用的代码:
@override
Widget build(BuildContext context) {
return Scaffold(
key: key,
appBar: AppBar(
title: Text("Just a test app"),
),
body: Stack(
children: <Widget>[
Positioned(
left: 0,
right: 0,
child: Card(
child: Text("A variable sized text"),
),
),
],
),
);
}
最佳答案
将您的Card
包裹成Align
或Center
:
Stack(
children: <Widget>[
Positioned(
top: 50,
left: 0,
right: 0,
child: Center(
child: Card(
child: Text("A variable sized text"),
),
),
),
Align(
alignment: Alignment.topCenter, //that also works
child: Card(
child: Text("Another variable sized text"),
),
)
],
),
关于dart - 如何在堆栈中定位小部件,但不匹配父级的宽度/高度?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54442602/