I have conditional statement to set image from Flutter assets, but is not working in Scaffold body.
How to set conditional statement for image in 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
),
),
),
);
}
You could do something like this:
String _setImage() {
String _mTitle = "${widget.title.data}";
if(_mTitle == “Goodmorrning”) {
return "assets/mobil_hello/goodmorrning.jpg";
} else if(_mTitle == “Good day”) {
return "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(_setImage()) // not working
),
),
),
);
}