dart - 如何将图像作为容器小部件的背景图像?
问题描述
我正在尝试创建类别卡,并将图像设置为子容器小部件的背景,但图像未显示在容器上
Container job1category(String imgpath, String name, String nikename) {
return Container(
width: 170,
child: Card(
child: Wrap(
children: <Widget>[
Container(
decoration: BoxDecoration(
image: DecorationImage(
image: AssetImage(imgpath),
fit: BoxFit.cover
)
),
child: null
),
ListTile(
title: Text(name),
subtitle: Text(nikename),
),
],
),
),
);
}
解决方案
您的容器没有分配高度,因为您已将图像放入容器,因此您必须为容器指定特定高度。我修改了你的代码:
Container(
child: Card(
child: Wrap(
children: <Widget>[
Container(
height:300,
decoration: BoxDecoration(
image: DecorationImage(
image: NetworkImage("https://upload.wikimedia.org/wikipedia/commons/1/17/Google-flutter-logo.png"),
fit: BoxFit.cover
)
),
child: null
),
ListTile(
title: Text("Flutter"),
subtitle: Text("Image of flutter"),
),
],
),
),
);
完全背景更改:将您的文本设置为容器的子项。
Container(
child: Card(
child: Wrap(
children: <Widget>[
Container(
height:300,
decoration: BoxDecoration(
image: DecorationImage(
image: NetworkImage("https://upload.wikimedia.org/wikipedia/commons/1/17/Google-flutter-logo.png"),
fit: BoxFit.cover
)
),
child: ListTile(
title: Text("Flutter"),
subtitle: Text("Image of flutter"),
),
),
],
),
),
);
推荐阅读
- arrays - 在 swift 4.2 中的表视图中重复数组的 n 索引
- javascript - ElectronJS - 我如何控制光标(移动,点击)
- reactjs - 将来自 GraphQL 查询的单个属性存储在本地 Apollo 存储中
- google-cloud-platform - 为个人用户的 Google Cloud Platform 创建报告
- android - Navigation.findNavController(Activity, @IdRes int viewId) 中的第二个参数是什么
- qt - QStyledItemDelegate + QTreeView::setIndenation 导致文本绘制出现故障
- apache-kafka - 消费者仅从具有复制因子 3 和分区 1 的 3 代理配置中的特定代理读取
- javascript - dynamodb.put().promise() 不返回 put 对象
- java - 检测 HashMap 是否包含相同列表的更好方法
- git - 如何让 git 子模块使用相对路径?