功能演示

Json数组循环

  1. // 自定义Json数据
  2. List listData = [
  3. {
  4. "title": "模拟Json数据1",
  5. "author": "Dart",
  6. "imageUrl": "http://sucai.suoluomei.cn/sucai_zs/images/20200226173153-2.jpg"
  7. },
  8. {
  9. "title": "模拟Json数据2",
  10. "author": "Dart",
  11. "imageUrl": "http://sucai.suoluomei.cn/sucai_zs/images/20200226173153-2.jpg"
  12. },
  13. {
  14. "title": "模拟Json数据3",
  15. "author": "Dart",
  16. "imageUrl": "http://sucai.suoluomei.cn/sucai_zs/images/20200226173153-2.jpg"
  17. }
  18. ];
  19. class HomeContent extends StatelessWidget {
  20. //自定义方法
  21. List<Widget> getData() {
  22. var tempList = listData.map((value) {
  23. return ListTile(
  24. leading: Image.network(value["imageUrl"]),
  25. title: Text(value["title"]),
  26. subtitle: Text(value["author"]),
  27. );
  28. });
  29. return tempList.toList();
  30. }
  31. @override
  32. Widget build(BuildContext context) {
  33. // TODO: implement build
  34. return ListView(
  35. children: this.getData(),
  36. );
  37. }
  38. }

组件演示

Wrap()

循环自动换行

  1. Wrap({
  2. Key key,
  3. this.direction = Axis.horizontal, //排列方向,默认水平方向排列
  4. this.alignment = WrapAlignment.start, //子控件在主轴上的对齐方式
  5. this.spacing = 0.0, //主轴上子控件中间的间距
  6. this.runAlignment = WrapAlignment.start, //子控件在交叉轴上的对齐方式
  7. this.runSpacing = 0.0, //交叉轴上子控件之间的间距
  8. this.crossAxisAlignment = WrapCrossAlignment.start, //交叉轴上子控件的对齐方式
  9. this.textDirection, //textDirection水平方向上子控件的起始位置
  10. this.verticalDirection = VerticalDirection.down, //垂直方向上子控件的其实位置
  11. List<Widget> children = const <Widget>[], //要显示的子控件集合
  12. })
  1. Wrap(
  2. spacing: 10, //主轴上子控件的间距
  3. runSpacing: 10, //交叉轴上子控件之间的间距
  4. children: imgList.map((v) {
  5. return Container(
  6. width: 150,
  7. height: 150,
  8. child: Image.network(v),
  9. padding: EdgeInsets.all(5),
  10. decoration: BoxDecoration(
  11. borderRadius: BorderRadius.circular(4),
  12. border: Border.all(
  13. color: Colors.grey.shade300,
  14. ),
  15. ),
  16. );
  17. }).toList(),
  18. ),