-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRefreshBox.dart
89 lines (71 loc) · 2 KB
/
RefreshBox.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
import 'package:flutter/material.dart';
import './sleep.dart';
// class RefreshBox extends StatelessWidget {
// final List<Widget> children;
// final Function onReachBottom; // 上拉加载
// final Function onRefresh; // 下拉刷新
// final String log;
// RefreshBox({
// this.children,
// this.onReachBottom,
// this.onRefresh,
// this.log = 'abc'
// });
// @override
// Widget build (BuildContext context) {
// return Text(log);
// }
// }
class RefreshBox extends StatefulWidget {
final List<Widget> children;
final Function onReachBottom; // 上拉加载
final Function onRefresh; // 下拉刷新
final String log;
RefreshBox({
this.children,
this.onReachBottom,
this.onRefresh,
this.log = '0'
});
@override
_RefreshView createState() => new _RefreshView();
}
class _RefreshView extends State<RefreshBox> {
ScrollController _scrollController = new ScrollController();
bool _bottomReaching = false; // 刚刚到达上拉加载条件,并执行回调函数
bool _botomEnding = true; // 加载数据后,渲染过程中不再执行
@override
void initState () {
super.initState();
// print('齿梳化');
if(widget.onReachBottom!=null) {
_scrollController.addListener(() async {
double diff = _scrollController.position.pixels - _scrollController.position.maxScrollExtent;
if(_botomEnding && !_bottomReaching && diff >= 0) {
print('下拉加载');
widget.onReachBottom();
setState(() {
_bottomReaching = true;
_botomEnding = false;
});
await sleep(5000);
setState(() {
_bottomReaching = false;
});
await sleep(500);
setState(() {
_botomEnding = true;
});
}
});
}
}
@override
Widget build(BuildContext context) {
return Text(widget.log);
}
// @override
// void dispose () {
// // _scrollController?.dispose();
// }
}