【发布时间】:2018-10-01 06:02:12
【问题描述】:
我有条件语句从 Flutter 资产中设置图像,但在 Scaffold 主体中不起作用。
如何在 Flutter 中为图片设置条件语句?
String _backgroundImage;
void _setImage() {
String _mTitle = "${widget.title.data}";
if(_mTitle == “Goodmorrning”) {
_backgroundImage = "assets/mobil_hello/goodmorrning.jpg";
} else if(_mTitle == “Good day”) {
_backgroundImage = "assets/mobil_hello/goodday.jpg";
}
print("_mTitle: $_mTitle"); // works
print("_backgroundImage: $_backgroundImage"); // works
}
Widget build(BuildContext contest) {
return Scaffold(
body: new Container(
decoration: BoxDecoration(
color: widget.backgroundColor,
image: new DecorationImage(
fit: BoxFit.cover,
image: new AssetImage("$_backgroundImage") // not working
),
),
),
);
}
【问题讨论】:
-
你应该在 setImage 中调用 setState
-
如果您使用的是
statefullwidget,那么。 if(_mTitle == "Goodmorrning") { setState((){ _backgroundImage = "assets/mobil_hello/goodmorrning.jpg"; }); } 同样适用于elseif块
标签: image dart conditional flutter assets