首页 > 解决方案 > 根据列表中的字符串显示图像

问题描述

我有一个字符串列表和一组图像。

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,
   ),
);

这是屏幕

字符串和图像应该匹配。

标签: androidandroid-studioflutterdart

解决方案


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")  ,

如果您没有很多图像,则可以。


推荐阅读