【Flutter】ListView 列表 ( List 集合的 map 方法说明 | 垂直列表 | 水平列表 | 代码示例 )

网友投稿 976 2022-05-29

文章目录

一、List 集合的 map 方法说明 ( 生成 listView 组件集合 )

二、ListView 垂直列表

三、ListView 水平列表

四、相关资源

一、List 集合的 map 方法说明 ( 生成 ListView 组件集合 )

ListView 列表的控件条目 , 一般是遍历集合生成的 ;

如 : 给定如下 List 集合 ;

const NAMES = [ '宋江', '卢俊义', '吴用', '公孙胜', '关胜'];

1

调用 List 集合的 map 方法 , 可以遍历操作集合中的每一项 , 返回一个新的数组 ;

map 方法的原型如下 ;

Iterable map(T f(E e)) => MappedIterable(this, f);

1

使用 map 方法 , 遍历 NAMES 集合 , 然后传入的匿名方法中 , 返回 Widget 组件 , 那么上述原型中的泛型 T 就是 Widget 类型 ;

下面的方法中 ,

map 方法传入了一个匿名函数

, 参数是 name , 类型是 String , 返回值是 _generateWidget 函数的返回值 , 其中

_generateWidget 函数返回 Widget 类型

, 最终

map 方法的返回值是 Iterable 类型

, 然后调用

toList() 方法

, 将其转为

List 类型 ;

NAMES.map((name) => _generateWidget(name)).toList();

1

二、ListView 垂直列表

完整代码示例 :

import 'package:flutter/material.dart'; const NAMES = [ '宋江', '卢俊义', '吴用', '公孙胜', '关胜', '林冲', '秦明', '呼延灼', '花荣', '柴进', '李应', '朱仝', '鲁智深', '武松', '董平', '张清', '杨志', '徐宁', '索超', '岱宗', '刘唐', '李逵', '史进', '穆弘' '雷横' ]; void main() { runApp(MyApp()); } class MyApp extends StatefulWidget { const MyApp({Key? key}) : super(key: key); @override _MyAppState createState() => _MyAppState(); } class _MyAppState extends State { @override Widget build(BuildContext context) { /// 材料设计主题 return MaterialApp( home: Scaffold( appBar: AppBar( /// 标题组件 title: Text("ListView 示例"), ), /// 列表组件 body: ListView( children: _buildList(), ), ), ); } /// 创建列表 List _buildList(){ /// 遍历 NAMES 数组 /// 调用 map 方法遍历数组元素 return NAMES.map((name) => _generateWidget(name)).toList(); } Widget _generateWidget(name){ return Container( height: 80, margin: EdgeInsets.only(bottom: 5), alignment: Alignment.center, decoration: BoxDecoration(color: Colors.black), child: Text( name, style: TextStyle( color: Colors.yellowAccent, fontSize: 20 ), ), ); } }

1

2

【Flutter】ListView 列表 ( List 集合的 map 方法说明 | 垂直列表 | 水平列表 | 代码示例 )

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

54

55

56

57

58

59

60

61

执行结果 :

三、ListView 水平列表

import 'package:flutter/cupertino.dart'; import 'package:flutter/material.dart'; const NAMES = [ '宋江', '卢俊义', '吴用', '公孙胜', '关胜', '林冲', '秦明', '呼延灼', '花荣', '柴进', '李应', '朱仝', '鲁智深', '武松', '董平', '张清', '杨志', '徐宁', '索超', '岱宗', '刘唐', '李逵', '史进', '穆弘' '雷横' ]; void main() { runApp(MyApp()); } class MyApp extends StatefulWidget { const MyApp({Key? key}) : super(key: key); @override _MyAppState createState() => _MyAppState(); } class _MyAppState extends State { @override Widget build(BuildContext context) { /// 材料设计主题 return MaterialApp( home: Scaffold( appBar: AppBar( /// 标题组件 title: Text("ListView 示例"), ), /// 列表组件 body: ListView( /// 水平滚动设置 scrollDirection: Axis.horizontal, children: _buildList(), ), ), ); } /// 创建列表 List _buildList(){ /// 遍历 NAMES 数组 /// 调用 map 方法遍历数组元素 return NAMES.map((name) => _generateWidget(name)).toList(); } Widget _generateWidget(name){ return Container( //height: 80, width: 80, //margin: EdgeInsets.only(bottom: 5), margin: EdgeInsets.only(right: 5), alignment: Alignment.center, decoration: BoxDecoration(color: Colors.black), child: Text( name, style: TextStyle( color: Colors.yellowAccent, fontSize: 20 ), ), ); } }

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

54

55

56

57

58

59

60

61

62

63

64

65

66

执行结果 :

四、相关资源

参考资料 :

flutter 官网 : https://flutter.dev/

Flutter 插件下载地址 : https://pub.dev/packages

Flutter 开发文档 : https://flutter.cn/docs ( 强烈推荐 )

官方 GitHub 地址 : https://github.com/flutter

Flutter 中文社区 : https://flutter.cn/

Flutter 实用教程 : https://flutter.cn/docs/cookbook

Flutter CodeLab : https://codelabs.flutter-io.cn/

Dart 中文文档 : https://dart.cn/

Dart 开发者官网 : https://api.dart.dev/

Flutter 中文网 : https://flutterchina.club/ , http://flutter.axuer.com/docs/

Flutter 相关问题 : https://flutterchina.club/faq/ ( 入门阶段推荐看一遍 )

GitHub 上的 Flutter 开源示例 : https://download.csdn.net/download/han1202012/15989510

Flutter 实战电子书 : https://book.flutterchina.club/chapter1/

Dart 语言练习网站 : https://dartpad.dartlang.org/

重要的专题 :

Flutter 动画参考文档 : https://flutterchina.club/animations/

博客源码下载 :

GitHub 地址 : https://github.com/han1202012/flutter_listview ( 随博客进度一直更新 , 有可能没有本博客的源码 )

博客源码快照 : https://download.csdn.net/download/han1202012/21586807 ( 本篇博客的源码快照 , 可以找到本博客的源码 )

Flutter ListView

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:(ง •_•)ง[Python3 OpenCV4]2.图像操作
下一篇:《企业级容器云架构开发指南》—2.4 如何从单体架构迁移到微服务
相关文章