理解为ListView 的三种形式吧
ListView 默认构造
但是这种方式创建的列表存在一个问题:对于那些长列表
或者需要较昂贵渲染开销
的子组件,即使还没有出现在屏幕中但仍然会被ListView
所创建,这将是一项较大的开销,使用不当可能引起性能问题甚至卡顿
直接返回的是每一行的Widget,相当于ios的row。行高按Widget(cell)高设置
ListView.build 就和ios的tableview差不多了
需要设置itemCount,itemBuilder
:
ListView.separated 每一行都加了下划线的ListView.build,当然下划线也可以用上面两种模式直接自己画
class FriendList extends StatelessWidget {
const FriendList({Key key}) : super(key: key);
@override
Widget build(BuildContext context) {
return ListView.separated(
itemCount: friendListData.length,
itemBuilder: (context, index) {
return FriendCard(data: friendListData[index]);
},
separatorBuilder: (context, index) {
return Divider(
height: .5,
indent: 75,
color: Color(0xFFDDDDDD),
);
},
);
}
}
Flutter还提供了下拉刷新的组件:
RefreshIndicator
参考代码
class PullDownRefreshList extends StatefulWidget {
const PullDownRefreshList({Key key}) : super(key: key);
@override
_PullDownRefreshListState createState() => _PullDownRefreshListState();
}
class _PullDownRefreshListState extends State<PullDownRefreshList> {
Future onRefresh() {
return Future.delayed(Duration(seconds: 1), () {
Toast.show('当前已是最新数据', context);
});
}
@override
Widget build(BuildContext context) {
return RefreshIndicator(
onRefresh: this.onRefresh,
child: ListView.separated(
itemCount: friendListData.length,
itemBuilder: (context, index) {
return FriendCard(data: friendListData[index]);
},
separatorBuilder: (context, index) {
return Divider(
height: .5,
indent: 75,
color: Color(0xFFDDDDDD),
);
},
),
);
}
}