• flutter循环


    flutter for循环:

    Wrap(
         children: <Widget>[
             for (int i = 0; i < (xx.yy.data?.items?.length ?? 0); i++)
                TextButton(onPressed: (){}, child: Text("${xx.yy.data?.items?[i].name.toString()} (${xx.yy.data?.items?[i].connId.toString()}) "))
        ],
    );
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    另一种方法:

    import 'package:flutter/material.dart';
    import './fonts.dart';
    import 'package:flutter_basic/res/listData.dart';
    
    void main() {
      runApp(MaterialApp(
        home: Scaffold(
          appBar: AppBar(
            title: const Text('hello flutter'),
          ),
          body: const Myapp(),
        ),
      ));
    }
    
    class Myapp extends StatelessWidget {
      const Myapp({Key? key}) : super(key: key);
      
      /// 定义组件
      /// 第一种方法:for循环
      // List _initListData() {
      //   List tempList = [];
      //   for (var i = 0; i < listData.length; i++) {
      //     tempList.add(ListTile(
      //       leading: Image.network("${listData[i]["imageUrl"]}"),
      //       title: Text("${listData[i]["title"]}"),
      //       subtitle: Text("${listData[i]["author"]}"),
      //     ));
      //   }
      //   return tempList;
      // }
    
    
      ///第二种方法:map
      List<Widget> _initListData() {
        var tempList = listData.map((e) {
          return ListTile(
            leading: Image.network("${e["imageUrl"]}"),
            title: Text("${e["title"]}"),
            subtitle: Text("${e["author"]}"),
          );
        });
        return tempList.toList();
      }
    
      
      Widget build(BuildContext context) {
        print(listData);
        return ListView(
          children: _initListData(),///使用
        );
      }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53

    使用ListView提供的builder构造函数来生成一个动态列表,在builder里需要传人两个必传参数itemCount、itemBuilder

    class Myapp extends StatelessWidget {
      const Myapp({Key? key}) : super(key: key);
    
      ///使用ListView提供的builder构造函数来生成一个动态列表
      /// 在builder里需要传人两个必传参数itemCount、itemBuilder
    
      
      Widget build(BuildContext context) {
        return ListView.builder(
            itemCount: listData.length,
            itemBuilder: (context, index) {
              return ListTile(
               leading: Image.network("${listData[index]["imageUrl"]}"),
                title: Text("${listData[index]["title"]}"),
                subtitle: Text("${listData[index]["author"]}"),
              );
            });
      }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
  • 相关阅读:
    (哈希表 ) 202. 快乐数——【Leetcode每日一题】
    迁移vm到pve
    数据赋能(67)——概念:数据变现
    Git---git shortlog查看贡献者,对比贡献次数(功能型命令)
    一起学数据结构(8)——二叉树中堆的代码实现
    Tortoise SVN 察看本地缓存密码
    了解比特币分叉:演变与分歧
    Hadoop完全分布式的搭建
    1.3.0windows cpu版pip安装出现HTTP error 404报错
    Netgear R6700v3 1.0.4.102(CVE-2021-27239)
  • 原文地址:https://blog.csdn.net/shelutai/article/details/133854048