flutter中通过循环渲染组件

直接贴代码:

 1 import 'package:flutter/material.dart';
 2 
 3 class Example extends StatefulWidget {
 4     @override
 5     _ExampleState createState() => _ExampleState();
 6 }
 7 
 8 class _ExampleState extends State<ExamplePage> {
 9     List formList;
10     initState() {
11       super.initState();
12         formList = [
13             {"title": '车牌号'},
14             {"title": '所有人'},
15             {"title": '号牌颜色'},
16         ];
17     }
18      Widget buildGrid() {
19             List<Widget> tiles = [];//先建一个数组用于存放循环生成的widget
20             Widget content; //单独一个widget组件,用于返回需要生成的内容widget
21             for(var item in formList) {
22                 tiles.add(
23                     new Row(
24                        children: <Widget>[
25                          new Text(item['title'])
26                        ]
27                     )
28                 );
29             }
30             content = new Column(
31                 children: tiles //重点在这里,因为用编辑器写Column生成的children后面会跟一个<Widget>[],
32                 //此时如果我们直接把生成的tiles放在<Widget>[]中是会报一个类型不匹配的错误,把<Widget>[]删了就可以了
33             );
34             return content;
35         }
36       Widget ExampleWidget = buildGrid();
37     @override
38     Widget build(BuildContext context) {
39         return Scaffold(
40             key: scaffoldKey,
41             appBar: AppBar(
42                 title: Text('循环渲染组件案例'),
43             ),
44             body: new Center(
45                 child: ExampleWidget
46             )
47         );
48     }
49 }

 

转载于:https://www.cnblogs.com/pjl43/p/9427647.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值