Foyona Records

FOYONA 012笔记Flutter 学习笔记,第 5 篇

Flutter学习笔记05:动态列表

  • 33 字,75 行代码
  • 约 1 分钟
  • 2,565 次阅读
  • 还没有评论
图:Unisex,Blueboy

构造动态列表的方式:

定义全局列表:

Dart18 行
List l = [
  {
    'name': '雪',
    'avatar': 'https://avatars.githubusercontent.com/u/42002296?v=4',
  },
  {
    'name': 'xue',
    'avatar': 'https://avatars.githubusercontent.com/u/42002296?v=4',
  },
  {
    'name': '雪碧',
    'avatar': 'https://avatars.githubusercontent.com/u/42002296?v=4',
  },
  {
    'name': '雪花',
    'avatar': 'https://avatars.githubusercontent.com/u/42002296?v=4',
  }
];

1:通过builder函数

Dart21 行
home: Scaffold(
            appBar: AppBar(
              title: Text(title),
              backgroundColor: Colors.lightBlue,
            ),
            body: ListView.builder(
              itemCount: l.length,
              itemBuilder: (BuildContext context, int index) {
                return ListTile(
                  leading: CircleAvatar(
                    backgroundImage: NetworkImage(l[index]['avatar']),
                  ),
                  title: Text(l[index]['name']),
                  onTap: () {
                    if (kDebugMode) {
                      print('点击了${l[index]['name']}');
                    }
                  },
                );
              },
            ))

2:通过自定义构造函数

Dart36 行
class MyApp extends StatelessWidget {
  const MyApp({super.key});

  List<Widget> _initListData() {
    List<Widget> list = [];
    for (var i = 0; i < l.length; i++) {
      list.add(ListTile(
        leading: CircleAvatar(
          backgroundImage: NetworkImage(l[i]['avatar']),
        ),
        title: Text(l[i]['name']),
        onTap: () {
          if (kDebugMode) {
            print('点击了${l[i]['name']}');
          }
        },
      ));
    }
    return list;
  }

  @override
  Widget build(BuildContext context) {
    var title = 'Test App';
    return MaterialApp(
        title: title,
        home: Scaffold(
            appBar: AppBar(
              title: Text(title),
              backgroundColor: Colors.lightBlue,
            ),
            body: ListView(
              children: _initListData(),
            )));
  }
}

效果:

评论

邮箱只用来通知回复,不会公开。

还没有人评论,来说第一句。

Esc
  1. FOYONA 028go-zero中的kafka组件笔记 go-zero kafka 微服务 消息队列
  2. FOYONA 027IM服务的连接健康检测机制笔记 Go go-zero 分布式 即时通讯 微服务
  3. FOYONA 026基于go-zero的微服务即时通讯项目day2——社交业务笔记 Go go-zero 分布式 微服务
  4. FOYONA 025基于go-zero的微服务即时通讯项目day1——用户业务笔记 Go gozero redis 分布式 微服务
  5. FOYONA 024基于go-zero的微服务即时通讯项目day0——需求分析笔记 Go go-zero 分布式 微服务
  6. FOYONA 023【转载】Raft一致性算法论文的中文翻译笔记 Go raft 分布式
  7. FOYONA 022MIT 6.824 Lab 1 MapReduce笔记 Go MapReduce 分布式