You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

240 lines
6.6 KiB

/*
* ====================================================
* package : widgets
* author : Created by nansi.
* time : 2019/5/7 5:54 PM
* remark :
* ====================================================
*/
/*
enableOverScroll : 1.3.5
[smart_refresh.dart]
// build the customScrollView
Widget _buildBodyBySlivers(Widget childView, List<Widget> slivers) {
Widget body;
if (childView is ScrollView) {
print(RefreshPhysics().applyTo(childView.physics));
body = CustomScrollView(
physics: !widget.enableOverScroll ? ClampingScrollPhysics() : _getScrollPhysics(conf).applyTo(childView.physics),
controller: controller.scrollController,
cacheExtent: childView.cacheExtent,
key: childView.key,
semanticChildCount: childView.semanticChildCount,
slivers: slivers,
dragStartBehavior: childView.dragStartBehavior,
reverse: childView.reverse,
);
} else {
body = CustomScrollView(
physics:
RefreshPhysics().applyTo(const AlwaysScrollableScrollPhysics()),
controller: controller.scrollController,
slivers: slivers,
);
}
return body;
}
*/
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:pull_to_refresh/pull_to_refresh.dart';
import 'package:recook/constants/header.dart';
typedef OnRefresh = Function();
typedef OnLoadMore = Function();
class RefreshWidget extends StatefulWidget {
RefreshWidget(
{this.body,
this.onRefresh,
this.onLoadMore,
this.isInNest = false,
GSRefreshController controller,
this.enableOverScroll = true,
this.color = const Color(0xff555555),
this.refreshingText: "正在努力获取数据...",
this.completeText: "刷新完成",
this.failedText: "网络出了一点问题呢",
this.idleText: "下拉刷新",
this.upIdleText: "上拉加载更多",
this.releaseText: "松开刷新",
this.loadingText: "正在加载中...",
this.noDataText: "已经到底了",
this.headerTriggerDistance,
this.header,
GridView child})
: this.controller = controller ?? GSRefreshController();
final Widget body;
final Color color;
final OnRefresh onRefresh;
final OnLoadMore onLoadMore;
final bool isInNest;
final bool enableOverScroll;
final GSRefreshController controller;
final double headerTriggerDistance;
final String refreshingText;
final String completeText;
final String failedText;
final String idleText;
final String upIdleText;
final String releaseText;
final String loadingText;
final String noDataText;
final Widget header;
@override
State<StatefulWidget> createState() {
return _RefreshWidgetState();
}
}
class _RefreshWidgetState extends State<RefreshWidget> {
@override
initState() {
super.initState();
}
@override
Widget build(BuildContext context) {
return RefreshConfiguration(
autoLoad: true,
hideFooterWhenNotFull: true,
headerTriggerDistance: widget.headerTriggerDistance ?? rSize(60),
child: SmartRefresher(
// enableOverScroll: widget.enableOverScroll,
enablePullDown: widget.onRefresh != null,
enablePullUp: widget.onLoadMore != null,
header: widget.header != null
? widget.header
: ClassicHeader(
4 years ago
textStyle: TextStyle(fontSize: 14 * 2.sp, color: widget.color),
idleIcon: Icon(
Icons.arrow_downward,
4 years ago
size: 20 * 2.sp,
color: widget.color,
),
releaseIcon: Icon(
Icons.arrow_upward,
4 years ago
size: 20 * 2.sp,
color: widget.color,
),
refreshingIcon: CupertinoActivityIndicator(
animating: true,
4 years ago
radius: 9.0 * 2.w,
),
completeIcon: Icon(
Icons.check,
4 years ago
size: 20 * 2.sp,
color: widget.color,
),
spacing: rSize(5),
refreshingText: widget.refreshingText,
completeText: widget.completeText,
failedText: widget.failedText,
idleText: widget.idleText,
releaseText: widget.releaseText,
),
footer: ClassicFooter(
4 years ago
textStyle: TextStyle(fontSize: 14 * 2.sp, color: Color(0xff555555)),
idleText: widget.upIdleText,
idleIcon: Icon(
Icons.arrow_upward,
4 years ago
size: 20 * 2.sp,
color: Color(0xff555555),
),
loadingText: widget.loadingText,
failedText: "网络出了一点问题呢",
noDataText: widget.noDataText,
canLoadingText: '',
),
controller: widget.controller._controller,
onRefresh: widget.onRefresh,
onLoading: widget.onLoadMore,
child: widget.body,
// isNestWrapped: widget.isInNest,
),
);
}
}
class GSRefreshController {
RefreshController _controller;
GSRefreshController({
bool initialRefresh = false,
}) {
_controller = RefreshController(initialRefresh: initialRefresh);
}
4 years ago
GSRefreshController.auto() {
_controller = RefreshController(initialRefresh: true);
}
void requestRefresh() {
_controller.requestRefresh();
}
void requestLoadMore() {
_controller.requestLoading();
}
void refreshCompleted({bool resetFooterState = false}) {
_controller.refreshCompleted(resetFooterState: resetFooterState);
}
/// request failed,the header display failed state
void refreshFailed() {
_controller.refreshFailed();
}
/// not show success or failed, it will set header state to idle and spring back at once
void refreshToIdle() {
_controller.refreshToIdle();
}
/// after data returned,set the footer state to idle
void loadComplete() {
_controller.loadComplete();
}
/// If catchError happen,you may call loadFailed indicate fetch data from network failed
void loadFailed() {
_controller.loadFailed();
}
/// load more success without error,but no data returned
void loadNoData() {
_controller.loadNoData();
}
/// reset footer noData state to idle
void resetNoData() {
_controller.resetNoData();
}
bool isRefresh() {
return _controller.isRefresh;
}
bool isLoading() {
return _controller.isLoading;
}
bool get isNoData =>
(_controller?.footerStatus ?? LoadStatus.canLoading) == LoadStatus.noMore;
void dispose() {
_controller.dispose();
}
}