要使图像填满其父元素,只需将其包装在FittedBox
中:
FittedBox(
child: Image.asset('foo.png'),
fit: BoxFit.fill,
)
FittedBox
会拉伸图像以填充空间。(请注意,此功能过去由BoxFit.fill
提供,但API已更改,因此BoxFit
不再提供此功能。 FittedBox
应该可以作为替代品使用,不需要更改构造函数参数。)
另外,对于复杂的装饰,您可以使用Container
而不是Image
--并使用decoration
/foregroundDecoration
字段。
要使Container
适合其父级,它应该是以下两者之一:
这里有一个示例,将两个图像和文本组合成单个Container
,同时占用其父级的100%宽度/高度:
![enter image description here](https://istack.dev59.com/0WJ8n.webp)
Container(
foregroundDecoration: const BoxDecoration(
image: DecorationImage(
image: NetworkImage(
'https://p6.storage.canalblog.com/69/50/922142/85510911_o.png'),
fit: BoxFit.fill),
),
decoration: const BoxDecoration(
image: DecorationImage(
alignment: Alignment(-.2, 0),
image: NetworkImage(
'http://www.naturerights.com/blog/wp-content/uploads/2017/12/Taranaki-NR-post-1170x550.png'),
fit: BoxFit.cover),
),
alignment: Alignment.bottomCenter,
padding: EdgeInsets.only(bottom: 20),
child: Text(
"Hello World",
style: Theme.of(context)
.textTheme
.display1
.copyWith(color: Colors.white),
),
),