面试题答案
一键面试在Flutter的Stack布局中,可以通过Stack
的children
参数的顺序来调整子Widget的层级顺序,先添加的子Widget在底层,后添加的在顶层。
例如:
Stack(
children: [
// 这是底层的Widget
Container(
color: Colors.blue,
width: 200,
height: 200,
),
// 这是顶层的Widget
Container(
color: Colors.red,
width: 100,
height: 100,
),
],
)
上述代码中,蓝色的Container
先添加,处于底层,红色的Container
后添加,处于顶层。
另外,还可以使用Positioned
组件来控制子Widget的位置和层级。例如:
Stack(
children: [
Positioned(
left: 0,
top: 0,
child: Container(
color: Colors.blue,
width: 200,
height: 200,
),
),
Positioned(
left: 50,
top: 50,
child: Container(
color: Colors.red,
width: 100,
height: 100,
),
),
],
)
这里同样是蓝色Container
在底层,红色Container
在顶层。Positioned
只是额外控制了子Widget在Stack
中的位置。