conditional rendering in flutter
While working on a recent project of mine, I wanted to display an image conditionally. And it turned out you could do it in flutter.
Here’s how:
- type an inline if statement
- add a new line for the widget
- profit
Widget build(BuildContext context) => Column(
color: Colors.red,
children: <Widget>[
if(true)
Text("conditional text"),
Text("hello"),
],
);
Bonus⌗
Conditional attribute using one line if statement.
Widget build(BuildContext build) => Container(
color: true ? Colors.red : null
);
Which goes as follows
condition ? what happens if true : what happens if false