Home > Software design >  Flutter - how to add list of widgets to column element?
Flutter - how to add list of widgets to column element?

Time:11-03

Let's say we have a column. Is it possible to add several widgets using one method? Something like .addAll()?

Column(
  children: [
    SomeWidget(),
    _someBigWidgetMethod(),
    _severalWidgets(),
  ]
)

_severalWidgets(){
  return [
    Widget(),
    Widget(),
    Widget(),
  ];
}

CodePudding user response:

In order to add all items in a list into another list, you can use the ... operator:

List<Widget> _myMethod() => [Widget1(), Widget2(), Widget3(), Widget4()];

Widget build(BuildContext context) {
  return Column(
    children: [
      SomeWidget(),
      ..._myMethod(),
    ]
  );
}
  • Related