/**
* 下拉刷新组件
*const RefreshIndicator
({
Key key,
@required this.child,
this.displacement: 40.0, //触发下拉刷新的距离
@required this.onRefresh, //下拉回调方法,方法需要有async和await关键字,没有await,刷新图标立马消失,没有async,刷新图标不会消失
this.color, //进度指示器前景色 默认为系统主题色
this.backgroundColor, //背景色
this.notificationPredicate: defaultScrollNotificationPredicate,
})
*/
class Widget_RefreshIndicator_State extends State<Widget_RefreshIndicator_Page> {
var list = [];
int page = 0;
bool isLoading = false;//是否正在请求新数据
bool showMore = false;//是否显示底部加载中提示
bool offState = false;//是否显示进入页面时的圆形进度条
ScrollController scrollController = ScrollController();
@override
void initState() {
super.initState();
scrollController.addListener(() {
if (scrollController.position.pixels ==
scrollController.position.maxScrollExtent) {
print('滑动到了最底部${scrollController.position.pixels}');
setState(() {
showMore = true;
});
getMoreData();
}
});
getListData();
}
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: Text("RefreshIndicator"),
),
body: Stack(
children: <Widget>[
RefreshIndicator(
child: ListView.builder(
controller: scrollController,
itemCount: list.length + 1,//列表长度+底部加载中提示
itemBuilder: choiceItemWidget,
),
onRefresh: _onRefresh,
),
Offstage(
offstage: offState,
child: Center(
child: CircularProgressIndicator(),
),
),
],
)
),
);
}
@override
void dispose() {
super.dispose();
//手动停止滑动监听
scrollController.dispose();
}
/**
* 加载哪个子组件
*/
Widget choiceItemWidget(BuildContext context, int position) {
if (position < list.length) {
return HomeListItem(position, list[position], (position) {
debugPrint("点击了第$position条");
});
} else if (showMore) {
return showMoreLoadingWidget();
}else{
return null;
}
}
/**
* 加载更多提示组件
*/
Widget showMoreLoadingWidget() {
return Container(
height: 50.0,
child: Row(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.center,
children: <Widget>[
Text('加载中...', style: TextStyle(fontSize: 16.0),),
],
),
);
}
/**
* 模拟进入页面获取数据
*/
void getListData() async {
if (isLoading) {
return;
}
setState(() {
isLoading = true;
});
await Future.delayed(Duration(seconds: 3), () {
setState(() {
isLoading = false;
offState = true;
list = List.generate(20, (i) {
return ItemInfo("ListView的一行数据$i");
});
});
});
}
/**
* 模拟到底部加载更多数据
*/
void getMoreData() async {
if (isLoading) {
return;
}
setState(() {
isLoading = true;
page++;
});
print('上拉刷新开始,page = $page');
await Future.delayed(Duration(seconds: 3), () {
setState(() {
isLoading = false;
showMore = false;
list.addAll(List.generate(3, (i) {
return ItemInfo("上拉添加ListView的一行数据$i");
}));
print('上拉刷新结束,page = $page');
});
});
}
/**
* 模拟下拉刷新
*/
Future < void > _onRefresh() async {
if (isLoading) {
return;
}
setState(() {
isLoading = true;
page = 0;
});
print('下拉刷新开始,page = $page');
await Future.delayed(Duration(seconds: 3), () {
setState(() {
isLoading = false;
List tempList = List.generate(3, (i) {
return ItemInfo("下拉添加ListView的一行数据$i");
});
tempList.addAll(list);
list = tempList;
print('下拉刷新结束,page = $page');
});
});
}
}
码云地址:https://gitee.com/xgljh/Flutter.git