import 'package:flutter/cupertino.dart';
|
import 'package:flutter/material.dart';
|
import '../../ui/widget/button.dart';
|
import 'package:pull_to_refresh/pull_to_refresh.dart';
|
|
typedef OnRefresh = void Function();
|
typedef OnLoadMore = void Function();
|
typedef BoolCallback = void Function(bool b);
|
typedef StringCallback = void Function(String s);
|
typedef GetViewState = RefreshListViewState Function();
|
|
class MyRefreshController extends RefreshController {
|
MyRefreshController({
|
bool initialRefresh = true,
|
}) : super(
|
initialRefresh: initialRefresh, initialLoadStatus: LoadStatus.idle);
|
|
//api错误
|
VoidCallback? apiError;
|
|
//空数据
|
VoidCallback? dataEmpty;
|
|
//正常数据
|
VoidCallback? dataNormal;
|
|
//下拉刷新
|
BoolCallback? setPullDownEnable;
|
|
//上拉加载
|
BoolCallback? setPullUpEnable;
|
|
//编辑模式,编辑模式下不允许下拉刷新与上拉加载
|
BoolCallback? setEditMode;
|
|
//界面的状态
|
GetViewState? viewState;
|
|
void dispose() {
|
apiError = null;
|
dataEmpty = null;
|
dataNormal = null;
|
setPullDownEnable = null;
|
setPullUpEnable = null;
|
setEditMode = null;
|
}
|
}
|
|
class RefreshListView extends StatefulWidget {
|
final MyRefreshController refreshController;
|
final Widget content;
|
final Widget? emptyView;
|
final OnRefresh? refresh;
|
final OnLoadMore? loadMore;
|
final Color loadTextColor;
|
bool? enablePullDown;
|
|
bool? enablePullUp;
|
|
RefreshListView(
|
{required this.refreshController,
|
required this.content,
|
this.emptyView,
|
this.refresh,
|
this.loadTextColor = const Color(0xFFB8AFB5),
|
this.loadMore,
|
this.enablePullDown = true,
|
this.enablePullUp = true});
|
|
@override
|
State<StatefulWidget> createState() => _RefreshListViewState();
|
}
|
|
class _RefreshListViewState extends State<RefreshListView> {
|
bool editMode = false;
|
|
@override
|
void initState() {
|
super.initState();
|
bindController();
|
}
|
|
bindController() {
|
widget.refreshController.apiError = _apiError;
|
widget.refreshController.dataEmpty = _dataEmpty;
|
widget.refreshController.dataNormal = _dataNormal;
|
widget.refreshController.setPullDownEnable = _pullDownEnable;
|
widget.refreshController.setPullUpEnable = _pullUpEnable;
|
widget.refreshController.viewState = () {
|
return _viewState;
|
};
|
widget.refreshController.setEditMode = (bool enable) {
|
setState(() {
|
editMode = enable;
|
});
|
};
|
}
|
|
//视图状态 0-正常 1-空视图 2-网络请求错误
|
RefreshListViewState _viewState = RefreshListViewState.normal;
|
|
_apiError() {
|
setState(() {
|
_viewState = RefreshListViewState.error;
|
});
|
}
|
|
_dataEmpty() {
|
setState(() {
|
_viewState = RefreshListViewState.empty;
|
});
|
}
|
|
_dataNormal() {
|
setState(() {
|
_viewState = RefreshListViewState.normal;
|
});
|
}
|
|
_pullDownEnable(bool enable) {
|
setState(() {
|
widget.enablePullDown = enable;
|
});
|
}
|
|
_pullUpEnable(bool enable) {
|
setState(() {
|
widget.enablePullUp = enable;
|
});
|
}
|
|
void _onRefresh() async {
|
if (widget.refresh != null) {
|
widget.refresh!();
|
}
|
}
|
|
void _onLoading() async {
|
if (widget.loadMore != null) {
|
widget.loadMore!();
|
}
|
}
|
|
Widget getView() {
|
switch (_viewState) {
|
case RefreshListViewState.normal:
|
return contentView();
|
case RefreshListViewState.empty:
|
return emptyView();
|
case RefreshListViewState.error:
|
return errorView();
|
}
|
}
|
|
Widget contentView() {
|
return editMode
|
? widget.content
|
: SmartRefresher(
|
enablePullDown: widget.enablePullDown!,
|
enablePullUp: widget.enablePullUp!,
|
header:
|
MaterialClassicHeader(),
|
|
// const WaterDropHeader(
|
// complete: Text(
|
// "刷新完成",
|
// style: TextStyle(color: Color(0xFFB8AFB5)),
|
// ),
|
// ),
|
footer: CustomFooter(
|
builder: (BuildContext context, LoadStatus? mode) {
|
Widget body;
|
if (mode == LoadStatus.idle) {
|
body = const Text("",
|
style: TextStyle(color: Color(0xFFB8AFB5)));
|
} else if (mode == LoadStatus.loading) {
|
body = const CupertinoActivityIndicator();
|
} else if (mode == LoadStatus.failed) {
|
body = const Text("加载失败!点击重试!",
|
style: TextStyle(color: Color(0xFFB8AFB5)));
|
} else if (mode == LoadStatus.canLoading) {
|
body = const Text("松手,加载更多!",
|
style: TextStyle(color: Color(0xFFB8AFB5)));
|
} else {
|
body = Text("~到底了~",
|
style: TextStyle(color: widget.loadTextColor));
|
}
|
return SizedBox(
|
height: 55.0,
|
child: Center(child: body),
|
);
|
},
|
),
|
controller: widget.refreshController,
|
onRefresh: _onRefresh,
|
onLoading: _onLoading,
|
child: widget.content,
|
);
|
}
|
|
Widget emptyView() {
|
return widget.emptyView ??
|
Container(
|
width: MediaQuery.of(context).size.width * 2 / 3,
|
alignment: Alignment.center,
|
// color: Colors.yellow,
|
child: Stack(
|
alignment: Alignment.bottomCenter,
|
children: [
|
Image.asset(
|
"assets/imgs/common/ic_empty.png",
|
),
|
const Text(
|
"~空空如也~",
|
style: TextStyle(color: Color(0xFF999999), fontSize: 18),
|
)
|
],
|
),
|
);
|
}
|
|
Widget errorView() {
|
return Container(
|
alignment: Alignment.center,
|
// color: Colors.yellow,
|
child: Stack(
|
alignment: Alignment.bottomCenter,
|
children: [
|
Image.asset("assets/imgs/common/ic_network_error.png",
|
width: MediaQuery.of(context).size.width * 3 / 5),
|
const Positioned(
|
bottom: 50,
|
left: 0,
|
right: 0,
|
child: Text(
|
"网络请求出错",
|
textAlign: TextAlign.center,
|
style: TextStyle(fontSize: 15, color: Color(0xFF999999)),
|
)),
|
MyFillButton(
|
"点击重新加载",
|
18,
|
width: 170,
|
height: 36,
|
fontSize: 15,
|
onClick: () {
|
_onRefresh();
|
},
|
)
|
],
|
),
|
);
|
}
|
|
@override
|
Widget build(BuildContext context) {
|
return Stack(alignment: Alignment.center, children: [
|
getView()
|
//网络错误页面
|
]);
|
}
|
}
|
|
enum RefreshListViewState { normal, empty, error }
|