android - 根据列表中的字符串显示图像
问题描述
我有一个字符串列表和一组图像。
import 'dart:math';
var list= [
"one",
"two",
"three",
];
final _random = new Random();
var stringslist = list[_random.nextInt(list.length)];
图像集如下。“一张图片.jpg”、“两张图片.jpg”、“三张图片.jpg”
将从列表中选择一个随机字符串,并将其设置在var stringslist
. 如何获取所选字符串的图像。
屏幕中两者的代码如下,
final planetCard = new Container(
child: new Text(''' Did You Know?
$stringsfacts''',
textAlign: TextAlign.center,
style: TextStyle(
letterSpacing: 1.2,
fontSize: 24.0,
color: Colors.red,
),
),
height: 250.0,
width: 350.0,
margin: new EdgeInsets.only(left: 60.0,top: 70.0),
padding: EdgeInsets.only(top: 12.0),
decoration: new BoxDecoration(
color: new Color(0xFF333366),
shape: BoxShape.rectangle,
borderRadius: new BorderRadius.circular(16.0),
boxShadow: <BoxShadow>[
new BoxShadow(
color: Colors.black12,
blurRadius: 25.0,
offset: new Offset(5.0, 10.0),
),
],
),
);
final planetThumbnail = new Container(
margin: new EdgeInsets.symmetric(
vertical: 10.0
),
alignment: FractionalOffset.topLeft,
child: new Image(
image: new AssetImage("assets/images/moon.png"), //This should change accroding to the selected String
height: 125.0,
width: 125.0,
),
);
字符串和图像应该匹配。
解决方案
child: new Image(
image: stringslist = "one" ? AssetImage("assets/images/one-image.png") : stringslist = "two" ? AssetImage("assets/images/two-image.png") : stringslist = "three" ? AssetImage("assets/images/three-image.png") : AssetImage("assets/images/moon.png") ,
如果您没有很多图像,则可以。
推荐阅读
- java - 需要建议:Java 反 If 语句反循环
- python - 无法从导入函数运行单元测试 - Python
- javascript - Javascript getDate 拉取看似随机的日期
- java - 尝试在 IntelliJ 中启动 Java GUI 时发生错误
- r - 如何在 R Studio 中使用唯一名称命名嵌套 for 循环的输出?
- asp.net-mvc - 如何让两次插入都成功和失败?
- java - 更新集合的最佳方法
- python - Plotly:如何自定义 3D 散点图的符号?
- python - 裁剪图像时出现 Python 错误:图块无法扩展到图像之外
- javascript - LeetCode 两和问题 - 结果未定义