Flutter ListView详解,全靠这套面试题

   ));
}
return new ListView(
  children: _list,
);

}


### [](
)[](
)ListView builder

**builder** 顾名思义 构造 可以非常方便的构建我们自己定义的**child**布局,所以在**Flutter**中非常的常用。

#### [](
)builder属性详细介绍

//设置滑动方向 Axis.horizontal 水平 默认 Axis.vertical 垂直
scrollDirection: Axis.vertical,
//内间距
padding: EdgeInsets.all(10.0),
//是否倒序显示 默认正序 false 倒序true
reverse: false,
//false,如果内容不足,则用户无法滚动 而如果[primary]为true,它们总是可以尝试滚动。
primary: true,
//确定每一个item的高度 会让item加载更加高效
itemExtent: 50.0,
//内容适配
shrinkWrap: true,
//item 数量
itemCount: list.length,
//滑动类型设置
physics: new ClampingScrollPhysics(),
//cacheExtent 设置预加载的区域
cacheExtent: 30.0,
//滑动监听
// controller ,


#### [](
)分析几个比较难理解的属性

**shrinkWrap**特别推荐  
**child** 高度会适配 **item**填充的内容的高度,我们非常的不希望**child**的高度固定,因为这样的话,如果里面的内容超出就会造成布局的溢出。  
**shrinkWrap**多用于嵌套**listView**中 内容大小不确定 比如 垂直布局中 先后放入文字 **listView** (需要**Expend**包裹否则无法显示无穷大高度 但是需要确定**listview**高度 **shrinkWrap**使用内容适配不会有这样的影响)

**primary**  
**If the \[primary\] argument is true, the \[controller\] must be null.**  
在构造中默认是false 它的意思就是为主的意思,**primary**为true时,我们的**controller** 滑动监听就不能使用了

**physics**  
这个属性几个滑动的选择  
**AlwaysScrollableScrollPhysics**() 总是可以滑动  
**NeverScrollableScrollPhysics**禁止滚动  
**BouncingScrollPhysics** 内容超过一屏 上拉有回弹效果  
**ClampingScrollPhysics** 包裹内容 不会有回弹

**cacheExtent**  
这个属性的意思就是预加载的区域  
设置预加载的区域 **cacheExtent** 强制设置为了 0.0,从而关闭了“预加载”

**controller**  
滑动监听,我们多用于上拉加载更多,通过监听滑动的距离来执行操作。

##### [](
)效果

![在这里插入图片描述](https://user-gold-cdn.xitu.io/2018/11/17/1672067a1bb17c72?imageView2/0/w/1280/h/960/ignore-error/1)

///listView builder 构建
Widget listViewLayoutBuilder(List list) {
return ListView.builder(
scrollDirection: Axis.vertical,
padding: EdgeInsets.all(10.0),
reverse: false,
primary: true,
itemExtent: 50.0,
shrinkWrap: true,
itemCount: list.length,
cacheExtent: 30.0,
physics: new ClampingScrollPhysics(),
// controller ,
itemBuilder: (context, i) => new Container(
child: new Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: [
new Text(
l i s t [ i ] . n a m e " , s t y l e : n e w T e x t S t y l e ( f o n t S i z e : 18.0 , c o l o r : C o l o r s . r e d ) , ) , n e w T e x t ( " {list[i].name}", style: new TextStyle(fontSize: 18.0, color: Colors.red), ), new Text( " list[i].name",st

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值