Home > OS >  Dart Flutter : How to use multiple widget in ternary operator inside a flutter widget
Dart Flutter : How to use multiple widget in ternary operator inside a flutter widget

Time:03-27

I wanna use multiple widget in ternary operator like this. But it's error.Can I do?

Container(
  child: (if value != null)
  ? Widget1(...),Widget2(...)
    : const SizedBox.shrink(),
)

CodePudding user response:

Container only support a single child widget, so you cannot use it this way. What you can do is use something like Column or Row:

Container(
  child: value != null
    ? Column(children: [Widget1(...), Widget2(...)])
   : const SizedBox.shrink(),
)

CodePudding user response:

Try wrapping each of those Widgets inside a Column() (or into an other widget you find appropriate for your app as Row).

Container(
  child: value != null
  ? Column(children:
                [Widget1(...),
                 Widget2(...)]
            )
    : const SizedBox.shrink(),
)
  • Related