tiktok_scaffold.dart
12.8 KB
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
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
import 'dart:math';
import 'package:flutter/material.dart';
import 'package:flutter/rendering.dart';
import 'package:Parlando/tiktok/style/style.dart';
const double scrollSpeed = 300;
enum TikTokPagePosition {
left,
right,
middle,
}
class TikTokScaffoldController extends ValueNotifier<TikTokPagePosition> {
TikTokScaffoldController([
TikTokPagePosition value = TikTokPagePosition.middle,
]) : super(value);
Future? animateToPage(TikTokPagePosition pagePosition) {
return _onAnimateToPage?.call(pagePosition);
}
Future? animateToLeft() {
return _onAnimateToPage?.call(TikTokPagePosition.left);
}
Future? animateToRight() {
return _onAnimateToPage?.call(TikTokPagePosition.right);
}
Future? animateToMiddle() {
return _onAnimateToPage?.call(TikTokPagePosition.middle);
}
Future Function(TikTokPagePosition pagePositon)? _onAnimateToPage;
}
class TikTokScaffold extends StatefulWidget {
final TikTokScaffoldController? controller;
/// 首页的顶部
final Widget? header;
/// 底部导航
final Widget? tabBar;
/// 左滑页面
final Widget? leftPage;
/// 右滑页面
final Widget? rightPage;
/// 视频序号
final int currentIndex;
final bool hasBottomPadding;
final bool? enableGesture;
final Widget? page;
final Widget? floatingActionButton;
final Widget? bottomNavigationBar;
final FloatingActionButtonLocation? floatingActionButtonLocation;
final Function()? onPullDownRefresh;
const TikTokScaffold({
Key? key,
this.header,
this.tabBar,
this.leftPage,
this.rightPage,
this.hasBottomPadding = false,
this.page,
this.currentIndex = 0,
this.enableGesture,
this.onPullDownRefresh,
this.controller,
this.floatingActionButton,
this.bottomNavigationBar,
this.floatingActionButtonLocation,
}) : super(key: key);
@override
_TikTokScaffoldState createState() => _TikTokScaffoldState();
}
class _TikTokScaffoldState extends State<TikTokScaffold>
with TickerProviderStateMixin {
AnimationController? animationControllerX;
AnimationController? animationControllerY;
late Animation<double> animationX;
late Animation<double> animationY;
double offsetX = 0.0;
double offsetY = 0.0;
double inMiddle = 0;
@override
void initState() {
widget.controller!._onAnimateToPage = animateToPage;
super.initState();
}
Future animateToPage(p) async {
if (screenWidth == null) {
return null;
}
switch (p) {
case TikTokPagePosition.left:
await animateTo(screenWidth!);
break;
case TikTokPagePosition.middle:
await animateTo();
break;
case TikTokPagePosition.right:
await animateTo(-screenWidth!);
break;
}
widget.controller!.value = p;
}
double? screenWidth;
@override
Widget build(BuildContext context) {
screenWidth = MediaQuery.of(context).size.width;
// 先定义正常结构
Widget body = Stack(
children: <Widget>[
_LeftPageTransform(
offsetX: offsetX,
content: widget.leftPage,
),
_MiddlePage(
absorbing: absorbing,
onTopDrag: () {
// absorbing = true;
setState(() {});
},
offsetX: offsetX,
offsetY: offsetY,
header: widget.header,
tabBar: widget.tabBar,
isStack: !widget.hasBottomPadding,
page: widget.page,
),
_RightPageTransform(
offsetX: offsetX,
offsetY: offsetY,
content: widget.rightPage,
),
],
);
// 增加手势控制
body = GestureDetector(
onVerticalDragUpdate: calculateOffsetY,
onVerticalDragEnd: (_) async {
if (!widget.enableGesture!) return;
absorbing = false;
if (offsetY != 0) {
await animateToTop();
widget.onPullDownRefresh?.call();
setState(() {});
}
},
onHorizontalDragEnd: (details) => onHorizontalDragEnd(
details,
screenWidth,
),
// 水平方向滑动开始
onHorizontalDragStart: (_) {
if (!widget.enableGesture!) return;
animationControllerX?.stop();
animationControllerY?.stop();
},
onHorizontalDragUpdate: (details) => onHorizontalDragUpdate(
details,
screenWidth,
),
child: body,
);
body = WillPopScope(
onWillPop: () async {
if (!widget.enableGesture!) return true;
if (inMiddle == 0) {
return true;
}
widget.controller!.animateToMiddle();
return false;
},
child: Scaffold(
body: body,
backgroundColor: Colors.black,
resizeToAvoidBottomInset: false,
floatingActionButton: widget.floatingActionButton,
bottomNavigationBar: widget.bottomNavigationBar,
floatingActionButtonLocation: widget.floatingActionButtonLocation,
),
);
return body;
}
// 水平方向滑动中
void onHorizontalDragUpdate(details, screenWidth) {
if (!widget.enableGesture!) return;
// 控制 offsetX 的值在 -screenWidth 到 screenWidth 之间
if (offsetX + details.delta.dx >= screenWidth) {
setState(() {
offsetX = screenWidth;
});
} else if (offsetX + details.delta.dx <= -screenWidth) {
setState(() {
offsetX = -screenWidth;
});
} else {
setState(() {
offsetX += details.delta.dx;
});
}
}
// 水平方向滑动结束
onHorizontalDragEnd(details, screenWidth) {
if (!widget.enableGesture!) return;
// print('velocity:${details.velocity}');
var vOffset = details.velocity.pixelsPerSecond.dx;
// 速度很快时
if (vOffset > scrollSpeed && inMiddle == 0) {
// 去右边页面
return animateToPage(TikTokPagePosition.left);
} else if (vOffset < -scrollSpeed && inMiddle == 0) {
// 去左边页面
return animateToPage(TikTokPagePosition.right);
} else if (inMiddle > 0 && vOffset < -scrollSpeed) {
return animateToPage(TikTokPagePosition.middle);
} else if (inMiddle < 0 && vOffset > scrollSpeed) {
return animateToPage(TikTokPagePosition.middle);
}
// 当滑动停止的时候 根据 offsetX 的偏移量进行动画
if (offsetX.abs() < screenWidth * 0.5) {
// 中间页面
return animateToPage(TikTokPagePosition.middle);
} else if (offsetX > 0) {
// 去左边页面
return animateToPage(TikTokPagePosition.left);
} else {
// 去右边页面
return animateToPage(TikTokPagePosition.right);
}
}
/// 滑动到顶部
///
/// [offsetY] to 0.0
Future animateToTop() {
animationControllerY = AnimationController(
duration: Duration(milliseconds: offsetY.abs() * 1000 ~/ 60),
vsync: this);
final curve = CurvedAnimation(
parent: animationControllerY!, curve: Curves.easeOutCubic);
animationY = Tween(begin: offsetY, end: 0.0).animate(curve)
..addListener(() {
setState(() {
offsetY = animationY.value;
});
});
return animationControllerY!.forward();
}
CurvedAnimation curvedAnimation() {
animationControllerX = AnimationController(
duration: Duration(milliseconds: max(offsetX.abs(), 60) * 1000 ~/ 500),
vsync: this);
return CurvedAnimation(
parent: animationControllerX!, curve: Curves.easeOutCubic);
}
Future animateTo([double end = 0.0]) {
final curve = curvedAnimation();
animationX = Tween(begin: offsetX, end: end).animate(curve)
..addListener(() {
setState(() {
offsetX = animationX.value;
});
});
inMiddle = end;
return animationControllerX!.animateTo(1);
}
bool absorbing = false;
double endOffset = 0.0;
/// 计算[offsetY]
///
/// 手指上滑,[absorbing]为false,不阻止事件,事件交给底层PageView处理
/// 处于第一页且是下拉,则拦截滑动事件
void calculateOffsetY(DragUpdateDetails details) {
if (!widget.enableGesture!) return;
if (inMiddle != 0) {
setState(() => absorbing = false);
return;
}
final tempY = offsetY + details.delta.dy / 2;
if (widget.currentIndex == 0) {
// absorbing = true; // TODO:暂时屏蔽了下拉刷新
if (tempY > 0) {
if (tempY < 40) {
offsetY = tempY;
} else if (offsetY != 40) {
offsetY = 40;
// vibrate();
}
} else {
absorbing = false;
}
setState(() {});
} else {
absorbing = false;
offsetY = 0;
setState(() {});
}
// print(absorbing.toString());
}
@override
void dispose() {
animationControllerX?.dispose();
animationControllerY?.dispose();
super.dispose();
}
}
class _MiddlePage extends StatelessWidget {
final bool? absorbing;
final bool isStack;
final Widget? page;
final double? offsetX;
final double? offsetY;
final Function? onTopDrag;
final Widget? header;
final Widget? tabBar;
const _MiddlePage({
Key? key,
this.absorbing,
this.onTopDrag,
this.offsetX,
this.offsetY,
this.isStack = false,
required this.header,
required this.tabBar,
this.page,
}) : super(key: key);
@override
Widget build(BuildContext context) {
Widget mainVideoList = Container(
color: ColorPlate.back1,
padding: EdgeInsets.only(
bottom: isStack ? 0 : 44 + MediaQuery.of(context).padding.bottom,
),
child: page,
);
// 刷新标志
Widget _headerContain;
if (offsetY! >= 20) {
_headerContain = Opacity(
opacity: (offsetY! - 20) / 20,
child: Transform.translate(
offset: Offset(0, offsetY!),
child: const SizedBox(
height: 44,
child: Center(
child: Text(
"下拉刷新内容",
style: TextStyle(
color: Colors.white,
fontSize: SysSize.normal,
),
),
),
),
),
);
} else {
_headerContain = Opacity(
opacity: max(0, 1 - offsetY! / 20),
child: Transform.translate(
offset: Offset(0, offsetY!),
child: SafeArea(
child: SizedBox(
height: 44,
child: header ?? const Placeholder(color: Colors.green),
),
),
),
);
}
Widget middle = Transform.translate(
offset: Offset(offsetX! > 0 ? offsetX! : offsetX! / 5, 0),
child: Stack(
children: <Widget>[
Stack(
alignment: Alignment.bottomCenter,
children: <Widget>[
mainVideoList,
],
),
_headerContain,
],
),
);
if (page is! PageView) {
return middle;
}
return AbsorbPointer(
absorbing: absorbing!,
child: NotificationListener<OverscrollIndicatorNotification>(
onNotification: (notification) {
notification.disallowGlow();
return;
} as bool Function(OverscrollIndicatorNotification)?,
child: NotificationListener<UserScrollNotification>(
onNotification: (notification) {
// 当手指离开时,并且处于顶部则拦截PageView的滑动事件 TODO: 没有触发下拉刷新
if (notification.direction == ScrollDirection.idle &&
notification.metrics.pixels == 0.0) {
onTopDrag?.call();
return false;
}
return true;
},
child: middle,
),
),
);
}
}
/// 左侧Widget
///
/// 通过 [Transform.scale] 进行根据 [offsetX] 缩放
/// 最小 0.88 最大为 1
class _LeftPageTransform extends StatelessWidget {
final double? offsetX;
final Widget? content;
const _LeftPageTransform({Key? key, this.offsetX, this.content})
: super(key: key);
@override
Widget build(BuildContext context) {
final screenWidth = MediaQuery.of(context).size.width;
return Transform.scale(
scale: 0.88 + 0.12 * offsetX! / screenWidth < 0.88
? 0.88
: 0.88 + 0.12 * offsetX! / screenWidth,
child: content ?? const Placeholder(color: Colors.pink),
);
}
}
class _RightPageTransform extends StatelessWidget {
final double? offsetX;
final double? offsetY;
final Widget? content;
const _RightPageTransform({
Key? key,
this.offsetX,
this.offsetY,
this.content,
}) : super(key: key);
@override
Widget build(BuildContext context) {
final screenWidth = MediaQuery.of(context).size.width;
final screenHeight = MediaQuery.of(context).size.height;
return Transform.translate(
offset: Offset(max(0, offsetX! + screenWidth), 0),
child: Container(
width: screenWidth,
height: screenHeight,
color: Colors.transparent,
child: content ?? Placeholder(fallbackWidth: screenWidth),
));
}
}