Flutter中通过循环渲染组件

class ContactsState extends State{

  List formList;
    initState() {
      super.initState();
        formList = [
            {"icon": Icon(Icons.alarm),"title": '车牌号'},
            {"icon": Icon(Icons.album),"title": '所有人'},
            {"icon": Icon(Icons.archive),"title": '号牌颜色'},
        ];
    }
    Widget buildGrid() {
        List tiles = [];//先建一个数组用于存放循环生成的widget
        Widget content; //单独一个widget组件,用于返回需要生成的内容widget
        for(var item in formList) {
            tiles.add(
              new Row(
                children: [
                  new Icon(Icons.alarm),
                  new Text(item['title']),
                ]
              )
            );
        }
        content = new Column(
            children: tiles //重点在这里,因为用编辑器写Column生成的children后面会跟一个[],
            //此时如果我们直接把生成的tiles放在[]中是会报一个类型不匹配的错误,把[]删了就可以了
        );
        return content;
    }
    
    @override
    Widget build(BuildContext context) {
        return Scaffold(
            appBar: AppBar(
                title: Text('循环渲染组件案例'),
            ),
            body: new Center(
                child: buildGrid(),
            )
        );
    }

}

 

转载于:https://www.cnblogs.com/joe235/p/11174342.html

你可能感兴趣的:(移动开发,ui)