-
Notifications
You must be signed in to change notification settings - Fork 200
/
personal_center_page3.dart
296 lines (266 loc) · 8.7 KB
/
personal_center_page3.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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
// ignore_for_file: avoid_print
// ignore_for_file: unused_local_variable
import 'package:flutter/material.dart';
import 'package:extended_nested_scroll_view/extended_nested_scroll_view.dart';
import 'package:easy_refresh/easy_refresh.dart';
import '/jh_common/widgets/jh_empty_view.dart';
import '/jh_common/widgets/jh_network_image.dart';
import '/jh_common/widgets/jh_tabbar.dart';
import '/project/configs/project_config.dart';
import '../http/http_page_test_cell.dart';
import '../http/http_page_test_model.dart';
const _headerHeight = 50.0;
class PersonalCenterPage3 extends StatefulWidget {
const PersonalCenterPage3({Key? key}) : super(key: key);
@override
State<PersonalCenterPage3> createState() => _PersonalCenterPage3State();
}
class _PersonalCenterPage3State extends State<PersonalCenterPage3> with SingleTickerProviderStateMixin {
late TabController _tabController;
List<JhTabBarModel> model = [
const JhTabBarModel(title: '标题一'),
const JhTabBarModel(title: '标题二'),
];
final EasyRefreshController _controller = EasyRefreshController(
controlFinishRefresh: true,
controlFinishLoad: true,
);
final int _limit = 15;
List _dataArr = [];
int _pageIndex = 0;
List _dataArr2 = [];
int _pageIndex2 = 0;
@override
void initState() {
super.initState();
_tabController = TabController(length: model.length, vsync: this);
_requestData(isShowLoading: true);
}
void _requestData({isShowLoading = false, isLoadMore = false}) {
int currentIndex = _tabController.index;
var params = {'limit': _limit};
if (currentIndex == 0) {
_pageIndex = isLoadMore ? _pageIndex + 1 : 0;
params = {'page': _pageIndex, 'limit': _limit};
} else {
_pageIndex2 = isLoadMore ? _pageIndex2 + 1 : 0;
params = {'page': _pageIndex2, 'limit': _limit};
}
var loadingText = isShowLoading == true ? 'Loading...' : null;
HttpUtils.get(APIs.getPage, params, loadingText: loadingText, success: (res) {
var tempData = res['data'];
setState(() {
if (isLoadMore) {
if (currentIndex == 0) {
_dataArr = _dataArr + tempData;
} else {
_dataArr2 = _dataArr2 + tempData;
}
_controller.finishLoad(tempData.length == _limit ? IndicatorResult.success : IndicatorResult.noMore);
} else {
if (currentIndex == 0) {
_dataArr = tempData;
} else {
_dataArr2 = tempData;
}
_controller.finishRefresh();
_controller.resetFooter();
}
});
}, fail: (code, msg) {
if (isLoadMore) {
_controller.finishLoad();
} else {
_controller.finishRefresh();
_controller.resetFooter();
}
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: EasyRefresh.builder(
controller: _controller,
header: _refreshHeader(),
footer: _refreshFooter(),
onRefresh: () async => _requestData(),
onLoad: () async => _requestData(isLoadMore: true),
childBuilder: (BuildContext context, ScrollPhysics physics) {
return _mainBody(physics);
},
),
);
}
_refreshHeader() {
const classicHeader = ClassicHeader(
triggerOffset: 60,
clamping: true,
position: IndicatorPosition.locator,
mainAxisAlignment: MainAxisAlignment.end,
dragText: '下拉刷新',
armedText: '释放刷新',
readyText: '加载中...',
processingText: '加载中...',
processedText: '加载完成',
noMoreText: '没有更多',
failedText: '加载失败',
messageText: '最后更新于 %T',
);
const materialHeader = MaterialHeader(
triggerOffset: 60, clamping: true, position: IndicatorPosition.locator, showBezierBackground: false);
return materialHeader;
}
_refreshFooter() {
const classicFooter = ClassicFooter(
triggerOffset: 60,
position: IndicatorPosition.locator,
dragText: '上拉加载',
armedText: '释放刷新',
readyText: '加载中...',
processingText: '加载中...',
processedText: '加载完成',
noMoreText: '没有更多',
failedText: '加载失败',
messageText: '最后更新于 %T',
showMessage: false, // 隐藏更新时间
);
const materialFooter = MaterialFooter(
triggerOffset: 60, clamping: false, position: IndicatorPosition.locator, showBezierBackground: false);
return materialFooter;
}
_mainBody(physics) {
var body = ScrollConfiguration(
behavior: const ERScrollBehavior(),
child: ExtendedNestedScrollView(
physics: physics,
onlyOneScrollInBody: true,
pinnedHeaderSliverHeightBuilder: () {
return JhScreenUtils.navigationBarHeight;
},
headerSliverBuilder: (context, innerBoxIsScrolled) => [
const HeaderLocator.sliver(clearExtent: false),
_buildSliverAppBar(),
],
body: TabBarView(
controller: _tabController,
children: <Widget>[
ContentPage2(physics: physics, dataArr: _dataArr),
ContentPage2(physics: physics, dataArr: _dataArr2),
// ExtendedVisibilityDetector(
// uniqueKey: const Key('Tab0'),
// child: ContentPage2(physics: physics, dataArr: _dataArr),
// ),
// ExtendedVisibilityDetector(
// uniqueKey: const Key('Tab1'),
// child: ContentPage2(physics: physics, dataArr: _dataArr),
// ),
],
),
),
);
return body;
}
Widget _buildSliverAppBar() {
return SliverAppBar(
title: const Text('Title', style: TextStyle(color: Colors.white)),
centerTitle: true,
leading: IconButton(
icon: const ImageIcon(AssetImage('assets/images/common/ic_nav_back_white.png')),
iconSize: 18,
padding: const EdgeInsets.fromLTRB(0, 0, 10, 0),
onPressed: () => JhNavUtils.goBack(context),
),
pinned: true,
expandedHeight: 200,
flexibleSpace: FlexibleSpaceBar(background: JhNetworkImage(KStrings.headBgImage)),
bottom: JhTabBar(
tabModelList: model,
tabController: _tabController,
switchPageCallBack: (index) {
debugPrint('切换到第$index页');
if (index == 0 && _dataArr.isEmpty) {
_requestData(isShowLoading: true);
}
if (index == 1 && _dataArr2.isEmpty) {
_requestData(isShowLoading: true);
}
},
),
);
}
}
class ContentPage2 extends StatefulWidget {
const ContentPage2({
Key? key,
this.physics,
this.dataArr = const [],
}) : super(key: key);
final ScrollPhysics? physics;
final List dataArr;
@override
State<ContentPage2> createState() => _ContentPage2State();
}
class _ContentPage2State extends State<ContentPage2> with AutomaticKeepAliveClientMixin {
@override
bool get wantKeepAlive => true;
@override
Widget build(BuildContext context) {
super.build(context);
return CustomScrollView(
physics: widget.physics,
slivers: [
SliverToBoxAdapter(child: _header()),
SliverToBoxAdapter(child: _listWidget(widget.dataArr)),
SliverToBoxAdapter(child: _footer()),
const FooterLocator.sliver(),
],
);
}
Widget _header() {
return Column(
children: [
Container(
alignment: Alignment.center,
color: Colors.orange,
height: _headerHeight,
child: const Text('header', style: TextStyle(color: Colors.white)),
)
],
);
}
Widget _footer() {
return Container(
alignment: Alignment.center,
color: Colors.orange,
height: _headerHeight,
child: const Text('footer', style: TextStyle(color: Colors.white)),
);
}
Widget _listWidget(List dataArr) {
if (dataArr.isEmpty) {
return const JhEmptyView();
} else {
return ListView.separated(
padding: const EdgeInsets.all(0),
// 加header要加上这两个属性
shrinkWrap: true,
physics: const NeverScrollableScrollPhysics(),
// 加header要加上这两个属性
itemCount: dataArr.length,
itemBuilder: (context, index) {
HttpPageTestModel model = HttpPageTestModel.fromJson(dataArr[index]);
return HttPageTestCell(
model: model,
onClickCell: (model) {
debugPrint('点击的index $index');
debugPrint('点击的地点${model['place']}');
},
);
},
separatorBuilder: (context, index) {
return const Divider(height: .5, indent: 15, endIndent: 15, color: Colors.purple);
},
);
}
}
}