实现思路
- 使用
Stack
作为根布局,Stack
允许子部件堆叠显示。
- 放置一个
Image
部件作为背景,通过 BoxFit.fill
使其铺满整个背景。
- 在
Stack
中添加一个居中的 Text
部件,使用 Positioned
并结合 top
和 left
等属性设置其位置在图片上方居中。
- 同样使用
Positioned
将按钮放置在右下角。
关键代码片段
Stack(
children: [
Image.asset(
'your_image_path',
fit: BoxFit.fill,
width: double.infinity,
height: double.infinity,
),
Positioned(
top: MediaQuery.of(context).size.height * 0.4,
left: 0,
right: 0,
child: Text(
'居中显示的文本',
textAlign: TextAlign.center,
),
),
Positioned(
bottom: 20,
right: 20,
child: ElevatedButton(
onPressed: () {},
child: Text('右下角按钮'),
),
),
],
)