-
Notifications
You must be signed in to change notification settings - Fork 9
/
custom_scroll_widget2.dart
318 lines (276 loc) · 7.14 KB
/
custom_scroll_widget2.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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
import 'dart:async';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_trip/util/toast.dart';
/*
* @ClassName custom_scroll_widget
* 作者: szj
* 时间: 2020/12/23 9:41
* CSDN:https://blog.csdn.net/weixin_44819566
* 公众号:码上变有钱
*/
class CustomScrollWidget2 extends StatefulWidget {
@override
_CustomScrollWidget2State createState() => _CustomScrollWidget2State();
}
class _CustomScrollWidget2State extends State<CustomScrollWidget2>
with SingleTickerProviderStateMixin {
TabController _tabController;
PageController _pageController;
// ignore: non_constant_identifier_names
Timer _timer;
@override
void initState() {
// TODO: implement initState
super.initState();
//Tab控制器
initTabController();
///Page控制器
initPageController();
//初始化Timer
initTimer();
}
void initTabController() {
_tabController = new TabController(
vsync: this,
length: 2,
);
}
PageController initPageController() {
return _pageController = PageController(
viewportFraction: 0.9, //设置每一个Page不占满屏幕,漏出上一个和下一个Page!
initialPage: _index, //设置默认Page
keepPage: true //是否保存当前滚动page
);
}
void initTimer() {
_timer = new Timer.periodic(new Duration(seconds: 3), (timer) {
if (mounted) {
setState(() {
_index++;
if (_index == 4) {
_index = 0;
}
//设置PageView滑动到的页面
_pageController.animateToPage(_index,
duration: Duration(seconds: 2), curve: Curves.ease);
});
}
});
}
@override
void dispose() {
// TODO: implement dispose
_tabController.dispose();
_timer.cancel();
super.dispose();
}
@override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: Colors.white,
body: RefreshIndicator(
//下拉刷新回调方法
onRefresh: initRefresh,
//可滚动组件在滚动时会发送ScrollNotification类型的通知
notificationPredicate: (ScrollNotification notifation) {
//该属性包含当前ViewPort及滚动位置等信息
ScrollMetrics scrollMetrics = notifation.metrics;
if (scrollMetrics.minScrollExtent == 0) {
return true;
} else {
return false;
}
},
//NestedScrollView(
child: initNestedScrollView(),
),
);
}
initRefresh() async {
//模拟网络刷新 等待2秒
await Future.delayed(Duration(milliseconds: 2000)).then((value) {
Toast.toast(context, msg: "已刷新");
});
//返回值以结束刷新
return Future.value(true);
}
NestedScrollView initNestedScrollView() {
return NestedScrollView(
headerSliverBuilder: (BuildContext context, bool innerBoxIsScrolled) {
return [
///初始化SliverAppBar
initSliverAppBar(),
];
},
//初始化TabBar
body: _initTabBarView(),
);
}
bool isCheck = false;
///SliverAppBar
Widget initSliverAppBar() {
return SliverAppBar(
expandedHeight: 280,
//当snap = true时 这个参数必须为true!!!
floating: false,
//AppBar固定
pinned: true,
//AppBar跟随手指滑动而滑动 floating必须为true才可以使用
snap: false,
flexibleSpace: FlexibleSpaceBar(
centerTitle: true,
background: initPageView(),
),
//搜索按钮
title: initsearch(),
bottom: PreferredSize(
preferredSize: Size(MediaQuery.of(context).size.width, 44),
//初始化initTabBar
child: initTabBar(),
),
);
}
TabBar initTabBar() {
return TabBar(
controller: _tabController,
//Tab控制条颜色
indicatorColor: Colors.blue,
//字体颜色
labelColor: Colors.black,
//Tab粗细
indicatorWeight: 3,
//设置Tab字体与控制器一样大
indicatorSize: TabBarIndicatorSize.label,
//Tab是否平分
isScrollable: false,
tabs: [
Tab(
child: Text(
"数学",
),
),
Tab(
child: Text(
"英语",
),
),
],
);
}
InkWell initsearch() {
return InkWell(
onTap: () {
Toast.toast(context, msg: "点击了搜索");
},
child: Container(
height: 38,
decoration: BoxDecoration(
color: Colors.white,
border: Border.all(color: Colors.white),
borderRadius: BorderRadius.circular(20)),
alignment: Alignment.center,
child: Text(
"搜索",
style: TextStyle(color: Colors.black, fontSize: 18),
),
),
);
}
///TabBar
Widget _initTabBarView() {
return TabBarView(
controller: _tabController,
children: [
initListView("数学"),
initListView("英语"),
],
);
}
//默认从0的位置开始
int _index = 0;
//初始化PageView
Widget initPageView() {
return Stack(
children: [
//PageView
buildPageView(),
//滑动圆点
buildRound(),
],
);
}
//滑动圆点
Positioned buildRound() {
return Positioned(
left: 20,
right: 20,
bottom: 60,
child: Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
initRound(_index == 0),
initRound(_index == 1),
initRound(_index == 2),
initRound(_index == 3),
],
),
);
}
//创建PageView
Widget buildPageView() {
return Container(
color: Colors.white,
padding: EdgeInsets.all(10),
child: PageView(
controller: _pageController,
children: [
initPageItem(Colors.deepPurple, "Page1"),
initPageItem(Colors.deepOrange, "Page2"),
initPageItem(Colors.yellow, "Page3"),
initPageItem(Colors.lightGreenAccent, "Page4"),
],
onPageChanged: (index) {
setState(() {
_index = index;
});
},
),
);
}
Widget initPageItem(Color color, String title) {
return Container(
decoration:
BoxDecoration(color: color, borderRadius: BorderRadius.circular(20)),
alignment: Alignment.center,
child: Text(title),
);
}
//初始化圆
Widget initRound(bool isselect) {
return AnimatedContainer(
duration: Duration(milliseconds: 800),
width: isselect ? 30 : 10,
height: 10,
margin: EdgeInsets.only(left: 5, right: 5),
curve: Curves.fastOutSlowIn,
decoration: BoxDecoration(
color: isselect ? Colors.black : Colors.white,
borderRadius: BorderRadius.circular(20),
),
);
}
//初始化ListView
initListView(String s) {
return ListView.builder(
itemBuilder: (BuildContext context, int index) {
return Container(
height: 50,
alignment: Alignment.center,
child: Text("$s$index"),
);
},
itemCount: 30,
);
}
}