@@ -5,14 +5,14 @@ import 'package:git_touch/models/theme.dart';
5
5
import 'package:git_touch/scaffolds/common.dart' ;
6
6
import 'package:git_touch/utils/utils.dart' ;
7
7
import 'package:provider/provider.dart' ;
8
- import '.. /widgets/error_reload.dart' ;
9
- import '.. /widgets/loading.dart' ;
10
- import '.. /widgets/empty.dart' ;
8
+ import 'package:git_touch /widgets/error_reload.dart' ;
9
+ import 'package:git_touch /widgets/loading.dart' ;
10
+ import 'package:git_touch /widgets/empty.dart' ;
11
11
12
12
class ListPayload <T , K > {
13
13
K cursor;
14
- Iterable <T >? items;
15
- bool ? hasMore;
14
+ Iterable <T > items;
15
+ bool hasMore;
16
16
17
17
ListPayload ({
18
18
required this .items,
@@ -56,7 +56,15 @@ class _ListStatefulScaffoldState<T, K>
56
56
void initState () {
57
57
super .initState ();
58
58
_refresh ();
59
- _controller.addListener (onScroll);
59
+ _controller.addListener (() {
60
+ if (_controller.position.maxScrollExtent - _controller.offset < 100 &&
61
+ ! _controller.position.outOfRange &&
62
+ ! loading &&
63
+ ! loadingMore &&
64
+ hasMore != false ) {
65
+ _loadMore ();
66
+ }
67
+ });
60
68
}
61
69
62
70
@override
@@ -65,40 +73,17 @@ class _ListStatefulScaffoldState<T, K>
65
73
super .dispose ();
66
74
}
67
75
68
- void onScroll () {
69
- // Fimber.d(_controller.position.maxScrollExtent - _controller.offset);
70
- if (_controller.position.maxScrollExtent - _controller.offset < 100 &&
71
- ! _controller.position.outOfRange &&
72
- ! loading &&
73
- ! loadingMore &&
74
- hasMore! ) {
75
- _loadMore ();
76
- }
77
- }
78
-
79
- // if items not enough, fetch next page
80
- // This should be triggered after build
81
- // TODO: disabled
82
- void _makeSureItemsFill () {
83
- // Future.delayed(Duration(milliseconds: 300)).then((_) {
84
- // onScroll();
85
- // });
86
- }
87
-
88
- Future <void > _refresh ({bool force = false }) async {
76
+ Future <void > _refresh () async {
89
77
// Fimber.d('list scaffold refresh');
90
78
setState (() {
91
79
error = '' ;
92
80
loading = true ;
93
- if (force) {
94
- items = [];
95
- }
96
81
});
97
82
try {
98
- final ListPayload <T , K ?> _payload = await widget.fetch (null );
99
- items = _payload .items! .toList ();
100
- cursor = _payload .cursor;
101
- hasMore = _payload .hasMore;
83
+ final ListPayload <T , K > p = await widget.fetch (null );
84
+ items = p .items.toList ();
85
+ cursor = p .cursor;
86
+ hasMore = p .hasMore;
102
87
} catch (err) {
103
88
error = err.toString ();
104
89
throw err;
@@ -107,7 +92,6 @@ class _ListStatefulScaffoldState<T, K>
107
92
setState (() {
108
93
loading = false ;
109
94
});
110
- _makeSureItemsFill ();
111
95
}
112
96
}
113
97
}
@@ -118,10 +102,10 @@ class _ListStatefulScaffoldState<T, K>
118
102
loadingMore = true ;
119
103
});
120
104
try {
121
- ListPayload <T , K ?> _payload = await widget.fetch (cursor);
122
- items.addAll (_payload .items! );
123
- cursor = _payload .cursor;
124
- hasMore = _payload .hasMore;
105
+ ListPayload <T , K > p = await widget.fetch (cursor);
106
+ items.addAll (p .items);
107
+ cursor = p .cursor;
108
+ hasMore = p .hasMore;
125
109
} catch (err) {
126
110
error = err.toString ();
127
111
throw err;
@@ -130,25 +114,22 @@ class _ListStatefulScaffoldState<T, K>
130
114
setState (() {
131
115
loadingMore = false ;
132
116
});
133
- _makeSureItemsFill ();
134
117
}
135
118
}
136
119
}
137
120
138
121
Widget _buildItem (BuildContext context, int index) {
139
122
if (index == 2 * items.length) {
140
- if (hasMore! ) {
123
+ if (hasMore != false ) {
141
124
return Loading (more: true );
142
125
} else {
143
126
return Container ();
144
127
}
145
- }
146
-
147
- if (index % 2 == 1 ) {
128
+ } else if (index % 2 == 1 ) {
148
129
return CommonStyle .border;
130
+ } else {
131
+ return widget.itemBuilder (items[index ~ / 2 ]);
149
132
}
150
-
151
- return widget.itemBuilder (items[index ~ / 2 ]);
152
133
}
153
134
154
135
Widget _buildCupertinoSliver () {
0 commit comments