flutter conditionals inside a widget code example

Example 1: conditionalstatement in widget flutter

Container(
  color: Colors.white,
  child: ('condition')
  ? Widget1(...)
  : Widget2(...)
)

Example 2: flutter conditional statement

condition? Text("True"): null,

Example 3: flutter conditional parent widget

return includeBlueContainer  ? blueWidget() : redWidget();

Widget blueWidget() {
return Container(
  color: Colors.blue,
  child: Container(
    child: Text("Child"),
  ),
)
}

Widget redWidget() {
return Container(
  color: Colors.red,
  child: Container(
    child: Text("Child"),
  ),
)
}

Tags:

Dart Example