您可以尝试的一种方法是使用FractionalTranslation 将孩子移动其大小的一小部分。或Transform.translate 将孩子移动一个硬编码的距离。这些不要求孩子具有硬编码的大小。
class MyWidget extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Column(
children: [
FractionalTranslation(
translation: Offset(0, 0.2),
child: Container(
width: 200,
height: 80,
color: Colors.red.withOpacity(1),
),
),
Container(
width: 500,
height: 80,
color: Colors.greenAccent.withOpacity(0.7),
),
Transform.translate(
offset: Offset(0, -10),
child: Container(
width: 500,
height: 80,
color: Colors.deepPurple.withOpacity(0.7),
),
),
],
);
}
}
结果:
编辑:
要让绿色盒子上的红色盒子,我们可以做这样的事情。
Widget build(BuildContext context) {
return Column(
children: [
FractionalTranslation(
translation: Offset(0, 1),
child: Container(
width: 500,
height: 80,
color: Colors.greenAccent.withOpacity(0.7),
),
),
FractionalTranslation(
translation: Offset(0, -0.8),
child: Container(
width: 200,
height: 80,
color: Colors.red.withOpacity(0.7),
),
),
Transform.translate(
offset: Offset(0, -10),
child: Container(
width: 500,
height: 80,
color: Colors.deepPurple.withOpacity(0.7),
),
),
],
);
}