Flutter-ListView

Flutter组件之ListView

ListView的使用分为静态和动态:

  1. 静态:固定写死的ListView
  2. 动态:从外部动态获取数据显示在List

静态

简介:直接建立ListView并填写好参数

必选

1. children => List<Widget>  组件数组

可选

1. scrollDirection 	=> Axis					滚动方向(水平还是垂直)默认垂直
2. padding			=> EdgeInsetsGeometry	内边距(与scrollDirection同向的padding,
											只有头和尾才有边距,即滚动时头部的padding会“滚走”)

例子

class MyList1 extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return new ListView(
      padding: EdgeInsets.fromLTRB(10.0, 10.0, 10.0, 10.0),
      scrollDirection: Axis.horizontal,
      children: <Widget>[
        new Container(
          width: 100.0,
          color: Colors.red,
        ),
        new Container(
          width: 100.0,
          color: Colors.orange,
        ),
        new Container(
          width: 100.0,
          color: Colors.yellow,
        ),
        new Container(
          width: 100.0,
          color: Colors.green,
        ),
        new Container(
          width: 100.0,
          color: Colors.blue,
        )
      ],
    );
  }
}

动态

简介:采用Builder的模式创建组件

必选

1. itemBuilder => List<Widget>  组件数组

可选

1. itemCount 		=> int					列表的item数量
2. padding			=> EdgeInsetsGeometry	内边距(与scrollDirection同向的padding,
											只有头和尾才有边距,即滚动时头部的padding会“滚走”)
3. scrollDirection 	=> Axis					滚动方向(水平还是垂直)默认垂直

例子

class MyList extends StatelessWidget {
  //接收传入参数所需要的设置
  final List<String> data;
  MyList({Key key, @required this.data}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return new ListView.builder(
      itemCount: data.length,
      itemBuilder: (context, index) {
        return new ListTile(
          title: new Text("${data[index]}"),
        );
      },
    );
  }
}

使用:
items: new List<String>.generate(100, (i) => "item $i")
MyList(data: items)//items为list数组数据
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值