Flutter组件学习三-ListView组件

1、Flutter 列表组件

Flutter 中我们可以通过 ListView 来定义 列表项,支持垂直和水平方向展示。通过一个属性就可以控制列表的显示方向。列表有一下 分类:

  • 1、垂直列表
  • 2、垂直图文列表
  • 3、水平列表
  • 4、动态列表
  • 5、矩阵式列表
1.1、列表参数
名称类型说明
scrollDirectionAxisAxis.horizontal 水平列表 Axis.vertical 垂直列表
paddingEdgeInsetsGeometry内边距
resolvebool组件反向排序
childrenList列表元素
1.2、代码示例
import 'package:flutter/material.dart';


// 垂直列表
class MyListView extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return Center(
      child: ListView(
        // 内边距
//        padding: EdgeInsets.all(10),
        //方向排序,从底部开始,与正常的相反
        reverse: true,
        children: <Widget>[
          ListTile(
            leading: Icon(Icons.phone),
            title: Text(
              "This is list1",
              style: TextStyle(fontSize: 20),
            ),
            subtitle: Text("This is subtitle"),
          ),
          ListTile(
            leading: Icon(Icons.phone),
            title: Text(
              "This is list2",
              style: TextStyle(fontSize: 20),
            ),
            subtitle: Text("This is subtitle"),
          ),
          ListTile(
            leading: Icon(Icons.phone),
            title: Text(
              "This is list3",
              style: TextStyle(fontSize: 20),
            ),
            subtitle: Text("This is subtitle"),
          ),
          ListTile(
            leading: Icon(Icons.phone),
            title: Text(
              "This is list4",
              style: TextStyle(fontSize: 20),
            ),
            subtitle: Text("This is subtitle"),
          ),
        ],
      ),
    );
  }
}

// 水平列表
class MyListView2 extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Container(
      height: 200,
      margin: EdgeInsets.all(2),
      child: ListView(
        scrollDirection: Axis.horizontal,
        children: <Widget>[

          // 一个纯色的块
          Container(
            width: 180.0,
            color: Colors.lightBlue,
          ),

          // 加载上去一张照片
          Container(
            width: 180,
            color: Colors.amber,
            child: ListView(
              children: <Widget>[
                Image.network(
                    'https://resources.ninghao.org/images/childhood-in-a-picture.jpg'),
                SizedBox(height: 16.0),
                Text(
                  '这是一个文本信息',
                  textAlign: TextAlign.center,
                  style: TextStyle(fontSize: 16.0),
                ),

              ],
            ),
          ),

          // 屏幕只显示了2个块,之后的可以滑动出来
          Container(
            width:180.0,
            color: Colors.deepOrange, ),
          Container(
            width:180.0,
            color: Colors.deepPurpleAccent,
          ),

        ],
      ),
    );
  }
}

// 加载动态数据
class MyListView3 extends StatelessWidget {
  List list = new List();

  MyListView3() {
    for (var i = 0; i < 20; i++) {
      list.add("这是第${i}条数据");
    }
    print(list);
  }

  @override
  Widget build(BuildContext context) {
    return ListView.builder(
      // 指定列数
      itemCount: this.list.length,
      // 指定内容
      itemBuilder: (context, index) {
        return ListTile(
          leading: Icon(Icons.access_time),
          title: Text("${list[index]}"),
        );
      },
    );
  }
}

在页面中组件的使用

//指定导入自定义组件
import 'package:flutter_app/MyListView.dart';

void main() => runApp(MyMaterialApp());

class MyMaterialApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return new MaterialApp(
        title: "test",
        home: new Scaffold(
          // 设置title
          appBar: AppBar(title: Text("ListViewDemo")),
          // 使用组件
          body: MyListView3(),
        ));
  }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值