前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >【Flutter】ListView 列表 ( List 集合的 map 方法说明 | 垂直列表 | 水平列表 | 代码示例 )

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

作者头像
韩曙亮
发布2023-03-29 15:48:31
1.3K0
发布2023-03-29 15:48:31
举报
文章被收录于专栏:韩曙亮的移动开发专栏

文章目录

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


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

如 : 给定如下 List 集合 ;

代码语言:javascript
复制
const NAMES = [ '宋江', '卢俊义', '吴用', '公孙胜', '关胜'];

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

map 方法的原型如下 ;

代码语言:javascript
复制
  Iterable<T> map<T>(T f(E e)) => MappedIterable<E, T>(this, f);

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

下面的方法中 , map 方法传入了一个匿名函数 , 参数是 name , 类型是 String , 返回值是 _generateWidget 函数的返回值 , 其中 _generateWidget 函数返回 Widget 类型 , 最终 map 方法的返回值是 Iterable<Widget> 类型 , 然后调用 toList() 方法 , 将其转为 List<Widget> 类型 ;

代码语言:javascript
复制
NAMES.map((name) => _generateWidget(name)).toList();

二、ListView 垂直列表


完整代码示例 :

代码语言:javascript
复制
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<MyApp> {
  @override
  Widget build(BuildContext context) {
    /// 材料设计主题
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          /// 标题组件
          title: Text("ListView 示例"),
        ),

        /// 列表组件
        body: ListView(
          children: _buildList(),
        ),
      ),
    );
  }

  /// 创建列表
  List<Widget> _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
        ),
      ),
    );
  }
}

执行结果 :

在这里插入图片描述
在这里插入图片描述

三、ListView 水平列表


代码语言:javascript
复制
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<MyApp> {
  @override
  Widget build(BuildContext context) {
    /// 材料设计主题
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          /// 标题组件
          title: Text("ListView 示例"),
        ),

        /// 列表组件
        body: ListView(
          /// 水平滚动设置
          scrollDirection: Axis.horizontal,
          children: _buildList(),
        ),
      ),
    );
  }

  /// 创建列表
  List<Widget> _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
        ),
      ),
    );
  }
}

执行结果 :

请添加图片描述
请添加图片描述

四、相关资源


参考资料 :

重要的专题 :

博客源码下载 :

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2021-08-26,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 文章目录
  • 一、List 集合的 map 方法说明 ( 生成 ListView 组件集合 )
  • 二、ListView 垂直列表
  • 三、ListView 水平列表
  • 四、相关资源
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档