首页 > 解决方案 > 将小部件添加到列表中

问题描述

我有这样的课

class View1 extends StatelessWidget {
  @override
  Widget build(BuildContext ctx) {
    return Center(child: Text('Something'));
  }
}
class View2 extends StatelessWidget {
....
}

如何将它们添加到列表中并分配给children: <Widget>[]. 例如,而不是

children: <Widget>[
                   view1() 
                   //or view2 
             ]

我想

children: <Widget>[
                   //for loop or something
                   list[i] 
                 ]

更新:

列出这样的创作 List list=['view1()','view(2)']

The argument type 'String' can't be assigned to the parameter type 'Widget'.当我尝试时我得到了

children: <Widget>[
        Container(
         child: list,
          )]

标签: flutter

解决方案


您可以使用

    children:list // list is widget list in your case

推荐阅读