-
Notifications
You must be signed in to change notification settings - Fork 700
/
swiper.dart
959 lines (845 loc) · 25.9 KB
/
swiper.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
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
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
import 'package:flutter/material.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter_page_indicator/flutter_page_indicator.dart';
import 'package:flutter_swiper/flutter_swiper.dart';
import 'dart:async';
import 'package:transformer_page_view/transformer_page_view.dart';
part 'custom_layout.dart';
typedef void SwiperOnTap(int index);
typedef Widget SwiperDataBuilder(BuildContext context, dynamic data, int index);
/// default auto play delay
const int kDefaultAutoplayDelayMs = 3000;
/// Default auto play transition duration (in millisecond)
const int kDefaultAutoplayTransactionDuration = 300;
const int kMaxValue = 2000000000;
const int kMiddleValue = 1000000000;
enum SwiperLayout { DEFAULT, STACK, TINDER, CUSTOM }
class Swiper extends StatefulWidget {
/// If set true , the pagination will display 'outer' of the 'content' container.
final bool outer;
/// Inner item height, this property is valid if layout=STACK or layout=TINDER or LAYOUT=CUSTOM,
final double itemHeight;
/// Inner item width, this property is valid if layout=STACK or layout=TINDER or LAYOUT=CUSTOM,
final double itemWidth;
// height of the inside container,this property is valid when outer=true,otherwise the inside container size is controlled by parent widget
final double containerHeight;
// width of the inside container,this property is valid when outer=true,otherwise the inside container size is controlled by parent widget
final double containerWidth;
/// Build item on index
final IndexedWidgetBuilder itemBuilder;
/// Support transform like Android PageView did
/// `itemBuilder` and `transformItemBuilder` must have one not null
final PageTransformer transformer;
/// count of the display items
final int itemCount;
final ValueChanged<int> onIndexChanged;
///auto play config
final bool autoplay;
///Duration of the animation between transactions (in millisecond).
final int autoplayDelay;
///disable auto play when interaction
final bool autoplayDisableOnInteraction;
///auto play transition duration (in millisecond)
final int duration;
///horizontal/vertical
final Axis scrollDirection;
///transition curve
final Curve curve;
/// Set to false to disable continuous loop mode.
final bool loop;
///Index number of initial slide.
///If not set , the `Swiper` is 'uncontrolled', which means manage index by itself
///If set , the `Swiper` is 'controlled', which means the index is fully managed by parent widget.
final int index;
///Called when tap
final SwiperOnTap onTap;
///The swiper pagination plugin
final SwiperPlugin pagination;
///the swiper control button plugin
final SwiperPlugin control;
///other plugins, you can custom your own plugin
final List<SwiperPlugin> plugins;
///
final SwiperController controller;
final ScrollPhysics physics;
///
final double viewportFraction;
/// Build in layouts
final SwiperLayout layout;
/// this value is valid when layout == SwiperLayout.CUSTOM
final CustomLayoutOption customLayoutOption;
// This value is valid when viewportFraction is set and < 1.0
final double scale;
// This value is valid when viewportFraction is set and < 1.0
final double fade;
final PageIndicatorLayout indicatorLayout;
Swiper({
this.itemBuilder,
this.indicatorLayout: PageIndicatorLayout.NONE,
///
this.transformer,
@required this.itemCount,
this.autoplay: false,
this.layout: SwiperLayout.DEFAULT,
this.autoplayDelay: kDefaultAutoplayDelayMs,
this.autoplayDisableOnInteraction: true,
this.duration: kDefaultAutoplayTransactionDuration,
this.onIndexChanged,
this.index,
this.onTap,
this.control,
this.loop: true,
this.curve: Curves.ease,
this.scrollDirection: Axis.horizontal,
this.pagination,
this.plugins,
this.physics,
Key key,
this.controller,
this.customLayoutOption,
/// since v1.0.0
this.containerHeight,
this.containerWidth,
this.viewportFraction: 1.0,
this.itemHeight,
this.itemWidth,
this.outer: false,
this.scale,
this.fade,
}) : assert(itemBuilder != null || transformer != null,
"itemBuilder and transformItemBuilder must not be both null"),
assert(
!loop ||
((loop &&
layout == SwiperLayout.DEFAULT &&
(indicatorLayout == PageIndicatorLayout.SCALE ||
indicatorLayout == PageIndicatorLayout.COLOR ||
indicatorLayout == PageIndicatorLayout.NONE)) ||
(loop && layout != SwiperLayout.DEFAULT)),
"Only support `PageIndicatorLayout.SCALE` and `PageIndicatorLayout.COLOR`when layout==SwiperLayout.DEFAULT in loop mode"),
super(key: key);
factory Swiper.children({
List<Widget> children,
bool autoplay: false,
PageTransformer transformer,
int autoplayDelay: kDefaultAutoplayDelayMs,
bool reverse: false,
bool autoplayDisableOnInteraction: true,
int duration: kDefaultAutoplayTransactionDuration,
ValueChanged<int> onIndexChanged,
int index,
SwiperOnTap onTap,
bool loop: true,
Curve curve: Curves.ease,
Axis scrollDirection: Axis.horizontal,
SwiperPlugin pagination,
SwiperPlugin control,
List<SwiperPlugin> plugins,
SwiperController controller,
Key key,
CustomLayoutOption customLayoutOption,
ScrollPhysics physics,
double containerHeight,
double containerWidth,
double viewportFraction: 1.0,
double itemHeight,
double itemWidth,
bool outer: false,
double scale: 1.0,
}) {
assert(children != null, "children must not be null");
return new Swiper(
transformer: transformer,
customLayoutOption: customLayoutOption,
containerHeight: containerHeight,
containerWidth: containerWidth,
viewportFraction: viewportFraction,
itemHeight: itemHeight,
itemWidth: itemWidth,
outer: outer,
scale: scale,
autoplay: autoplay,
autoplayDelay: autoplayDelay,
autoplayDisableOnInteraction: autoplayDisableOnInteraction,
duration: duration,
onIndexChanged: onIndexChanged,
index: index,
onTap: onTap,
curve: curve,
scrollDirection: scrollDirection,
pagination: pagination,
control: control,
controller: controller,
loop: loop,
plugins: plugins,
physics: physics,
key: key,
itemBuilder: (BuildContext context, int index) {
return children[index];
},
itemCount: children.length);
}
factory Swiper.list({
PageTransformer transformer,
List list,
CustomLayoutOption customLayoutOption,
SwiperDataBuilder builder,
bool autoplay: false,
int autoplayDelay: kDefaultAutoplayDelayMs,
bool reverse: false,
bool autoplayDisableOnInteraction: true,
int duration: kDefaultAutoplayTransactionDuration,
ValueChanged<int> onIndexChanged,
int index,
SwiperOnTap onTap,
bool loop: true,
Curve curve: Curves.ease,
Axis scrollDirection: Axis.horizontal,
SwiperPlugin pagination,
SwiperPlugin control,
List<SwiperPlugin> plugins,
SwiperController controller,
Key key,
ScrollPhysics physics,
double containerHeight,
double containerWidth,
double viewportFraction: 1.0,
double itemHeight,
double itemWidth,
bool outer: false,
double scale: 1.0,
}) {
return new Swiper(
transformer: transformer,
customLayoutOption: customLayoutOption,
containerHeight: containerHeight,
containerWidth: containerWidth,
viewportFraction: viewportFraction,
itemHeight: itemHeight,
itemWidth: itemWidth,
outer: outer,
scale: scale,
autoplay: autoplay,
autoplayDelay: autoplayDelay,
autoplayDisableOnInteraction: autoplayDisableOnInteraction,
duration: duration,
onIndexChanged: onIndexChanged,
index: index,
onTap: onTap,
curve: curve,
key: key,
scrollDirection: scrollDirection,
pagination: pagination,
control: control,
controller: controller,
loop: loop,
plugins: plugins,
physics: physics,
itemBuilder: (BuildContext context, int index) {
return builder(context, list[index], index);
},
itemCount: list.length);
}
@override
State<StatefulWidget> createState() {
return new _SwiperState();
}
}
abstract class _SwiperTimerMixin extends State<Swiper> {
Timer _timer;
SwiperController _controller;
@override
void initState() {
_controller = widget.controller;
if (_controller == null) {
_controller = new SwiperController();
}
_controller.addListener(_onController);
_handleAutoplay();
super.initState();
}
void _onController() {
switch (_controller.event) {
case SwiperController.START_AUTOPLAY:
{
if (_timer == null) {
_startAutoplay();
}
}
break;
case SwiperController.STOP_AUTOPLAY:
{
if (_timer != null) {
_stopAutoplay();
}
}
break;
}
}
@override
void didUpdateWidget(Swiper oldWidget) {
if (_controller != oldWidget.controller) {
if (oldWidget.controller != null) {
oldWidget.controller.removeListener(_onController);
_controller = oldWidget.controller;
_controller.addListener(_onController);
}
}
_handleAutoplay();
super.didUpdateWidget(oldWidget);
}
@override
void dispose() {
if (_controller != null) {
_controller.removeListener(_onController);
// _controller.dispose();
}
_stopAutoplay();
super.dispose();
}
bool _autoplayEnabled() {
return _controller.autoplay ?? widget.autoplay;
}
void _handleAutoplay() {
if (_autoplayEnabled() && _timer != null) return;
_stopAutoplay();
if (_autoplayEnabled()) {
_startAutoplay();
}
}
void _startAutoplay() {
assert(_timer == null, "Timer must be stopped before start!");
_timer =
Timer.periodic(Duration(milliseconds: widget.autoplayDelay), _onTimer);
}
void _onTimer(Timer timer) {
_controller.next(animation: true);
}
void _stopAutoplay() {
if (_timer != null) {
_timer.cancel();
_timer = null;
}
}
}
class _SwiperState extends _SwiperTimerMixin {
int _activeIndex;
TransformerPageController _pageController;
Widget _wrapTap(BuildContext context, int index) {
return new GestureDetector(
behavior: HitTestBehavior.opaque,
onTap: () {
this.widget.onTap(index);
},
child: widget.itemBuilder(context, index),
);
}
@override
void initState() {
_activeIndex = widget.index ?? 0;
if (_isPageViewLayout()) {
_pageController = new TransformerPageController(
initialPage: widget.index,
loop: widget.loop,
itemCount: widget.itemCount,
reverse:
widget.transformer == null ? false : widget.transformer.reverse,
viewportFraction: widget.viewportFraction);
}
super.initState();
}
bool _isPageViewLayout() {
return widget.layout == null || widget.layout == SwiperLayout.DEFAULT;
}
@override
void didChangeDependencies() {
super.didChangeDependencies();
}
bool _getReverse(Swiper widget) =>
widget.transformer == null ? false : widget.transformer.reverse;
@override
void didUpdateWidget(Swiper oldWidget) {
super.didUpdateWidget(oldWidget);
if (_isPageViewLayout()) {
if (_pageController == null ||
(widget.index != oldWidget.index ||
widget.loop != oldWidget.loop ||
widget.itemCount != oldWidget.itemCount ||
widget.viewportFraction != oldWidget.viewportFraction ||
_getReverse(widget) != _getReverse(oldWidget))) {
_pageController = new TransformerPageController(
initialPage: widget.index,
loop: widget.loop,
itemCount: widget.itemCount,
reverse: _getReverse(widget),
viewportFraction: widget.viewportFraction);
}
} else {
scheduleMicrotask(() {
// So that we have a chance to do `removeListener` in child widgets.
if (_pageController != null) {
_pageController.dispose();
_pageController = null;
}
});
}
if (widget.index != null && widget.index != _activeIndex) {
_activeIndex = widget.index;
}
}
void _onIndexChanged(int index) {
setState(() {
_activeIndex = index;
});
if (widget.onIndexChanged != null) {
widget.onIndexChanged(index);
}
}
Widget _buildSwiper() {
IndexedWidgetBuilder itemBuilder;
if (widget.onTap != null) {
itemBuilder = _wrapTap;
} else {
itemBuilder = widget.itemBuilder;
}
if (widget.layout == SwiperLayout.STACK) {
return new _StackSwiper(
loop: widget.loop,
itemWidth: widget.itemWidth,
itemHeight: widget.itemHeight,
itemCount: widget.itemCount,
itemBuilder: itemBuilder,
index: _activeIndex,
curve: widget.curve,
duration: widget.duration,
onIndexChanged: _onIndexChanged,
controller: _controller,
scrollDirection: widget.scrollDirection,
);
} else if (_isPageViewLayout()) {
PageTransformer transformer = widget.transformer;
if (widget.scale != null || widget.fade != null) {
transformer =
new ScaleAndFadeTransformer(scale: widget.scale, fade: widget.fade);
}
Widget child = new TransformerPageView(
pageController: _pageController,
loop: widget.loop,
itemCount: widget.itemCount,
itemBuilder: itemBuilder,
transformer: transformer,
viewportFraction: widget.viewportFraction,
index: _activeIndex,
duration: new Duration(milliseconds: widget.duration),
scrollDirection: widget.scrollDirection,
onPageChanged: _onIndexChanged,
curve: widget.curve,
physics: widget.physics,
controller: _controller,
);
if (widget.autoplayDisableOnInteraction && widget.autoplay) {
return new NotificationListener(
child: child,
onNotification: (ScrollNotification notification) {
if (notification is ScrollStartNotification) {
if (notification.dragDetails != null) {
//by human
if (_timer != null) _stopAutoplay();
}
} else if (notification is ScrollEndNotification) {
if (_timer == null) _startAutoplay();
}
return false;
},
);
}
return child;
} else if (widget.layout == SwiperLayout.TINDER) {
return new _TinderSwiper(
loop: widget.loop,
itemWidth: widget.itemWidth,
itemHeight: widget.itemHeight,
itemCount: widget.itemCount,
itemBuilder: itemBuilder,
index: _activeIndex,
curve: widget.curve,
duration: widget.duration,
onIndexChanged: _onIndexChanged,
controller: _controller,
scrollDirection: widget.scrollDirection,
);
} else if (widget.layout == SwiperLayout.CUSTOM) {
return new _CustomLayoutSwiper(
loop: widget.loop,
option: widget.customLayoutOption,
itemWidth: widget.itemWidth,
itemHeight: widget.itemHeight,
itemCount: widget.itemCount,
itemBuilder: itemBuilder,
index: _activeIndex,
curve: widget.curve,
duration: widget.duration,
onIndexChanged: _onIndexChanged,
controller: _controller,
scrollDirection: widget.scrollDirection,
);
} else {
return new Container();
}
}
SwiperPluginConfig _ensureConfig(SwiperPluginConfig config) {
if (config == null) {
config = new SwiperPluginConfig(
outer: widget.outer,
itemCount: widget.itemCount,
layout: widget.layout,
indicatorLayout: widget.indicatorLayout,
pageController: _pageController,
activeIndex: _activeIndex,
scrollDirection: widget.scrollDirection,
controller: _controller,
loop: widget.loop);
}
return config;
}
List<Widget> _ensureListForStack(
Widget swiper, List<Widget> listForStack, Widget widget) {
if (listForStack == null) {
listForStack = [swiper, widget];
} else {
listForStack.add(widget);
}
return listForStack;
}
@override
Widget build(BuildContext context) {
Widget swiper = _buildSwiper();
List<Widget> listForStack;
SwiperPluginConfig config;
if (widget.control != null) {
//Stack
config = _ensureConfig(config);
listForStack = _ensureListForStack(
swiper, listForStack, widget.control.build(context, config));
}
if (widget.plugins != null) {
config = _ensureConfig(config);
for (SwiperPlugin plugin in widget.plugins) {
listForStack = _ensureListForStack(
swiper, listForStack, plugin.build(context, config));
}
}
if (widget.pagination != null) {
config = _ensureConfig(config);
if (widget.outer) {
return _buildOuterPagination(
widget.pagination,
listForStack == null ? swiper : new Stack(children: listForStack),
config);
} else {
listForStack = _ensureListForStack(
swiper, listForStack, widget.pagination.build(context, config));
}
}
if (listForStack != null) {
return new Stack(
children: listForStack,
);
}
return swiper;
}
Widget _buildOuterPagination(
SwiperPagination pagination, Widget swiper, SwiperPluginConfig config) {
List<Widget> list = [];
//Only support bottom yet!
if (widget.containerHeight != null || widget.containerWidth != null) {
list.add(swiper);
} else {
list.add(new Expanded(child: swiper));
}
list.add(new Align(
alignment: Alignment.center,
child: pagination.build(context, config),
));
return new Column(
children: list,
crossAxisAlignment: CrossAxisAlignment.stretch,
mainAxisSize: MainAxisSize.min,
);
}
}
abstract class _SubSwiper extends StatefulWidget {
final IndexedWidgetBuilder itemBuilder;
final int itemCount;
final int index;
final ValueChanged<int> onIndexChanged;
final SwiperController controller;
final int duration;
final Curve curve;
final double itemWidth;
final double itemHeight;
final bool loop;
final Axis scrollDirection;
_SubSwiper(
{Key key,
this.loop,
this.itemHeight,
this.itemWidth,
this.duration,
this.curve,
this.itemBuilder,
this.controller,
this.index,
this.itemCount,
this.scrollDirection: Axis.horizontal,
this.onIndexChanged})
: super(key: key);
@override
State<StatefulWidget> createState();
int getCorrectIndex(int indexNeedsFix) {
if (itemCount == 0) return 0;
int value = indexNeedsFix % itemCount;
if (value < 0) {
value += itemCount;
}
return value;
}
}
class _TinderSwiper extends _SubSwiper {
_TinderSwiper({
Key key,
Curve curve,
int duration,
SwiperController controller,
ValueChanged<int> onIndexChanged,
double itemHeight,
double itemWidth,
IndexedWidgetBuilder itemBuilder,
int index,
bool loop,
int itemCount,
Axis scrollDirection,
}) : assert(itemWidth != null && itemHeight != null),
super(
loop: loop,
key: key,
itemWidth: itemWidth,
itemHeight: itemHeight,
itemBuilder: itemBuilder,
curve: curve,
duration: duration,
controller: controller,
index: index,
onIndexChanged: onIndexChanged,
itemCount: itemCount,
scrollDirection: scrollDirection);
@override
State<StatefulWidget> createState() {
return new _TinderState();
}
}
class _StackSwiper extends _SubSwiper {
_StackSwiper({
Key key,
Curve curve,
int duration,
SwiperController controller,
ValueChanged<int> onIndexChanged,
double itemHeight,
double itemWidth,
IndexedWidgetBuilder itemBuilder,
int index,
bool loop,
int itemCount,
Axis scrollDirection,
}) : super(
loop: loop,
key: key,
itemWidth: itemWidth,
itemHeight: itemHeight,
itemBuilder: itemBuilder,
curve: curve,
duration: duration,
controller: controller,
index: index,
onIndexChanged: onIndexChanged,
itemCount: itemCount,
scrollDirection: scrollDirection);
@override
State<StatefulWidget> createState() {
return new _StackViewState();
}
}
class _TinderState extends _CustomLayoutStateBase<_TinderSwiper> {
List<double> scales;
List<double> offsetsX;
List<double> offsetsY;
List<double> opacity;
List<double> rotates;
double getOffsetY(double scale) {
return widget.itemHeight - widget.itemHeight * scale;
}
@override
void didChangeDependencies() {
super.didChangeDependencies();
}
@override
void didUpdateWidget(_TinderSwiper oldWidget) {
_updateValues();
super.didUpdateWidget(oldWidget);
}
@override
void afterRender() {
super.afterRender();
_startIndex = -3;
_animationCount = 5;
opacity = [0.0, 0.9, 0.9, 1.0, 0.0, 0.0];
scales = [0.80, 0.80, 0.85, 0.90, 1.0, 1.0, 1.0];
rotates = [0.0, 0.0, 0.0, 0.0, 20.0, 25.0];
_updateValues();
}
void _updateValues() {
if (widget.scrollDirection == Axis.horizontal) {
offsetsX = [0.0, 0.0, 0.0, 0.0, _swiperWidth, _swiperWidth];
offsetsY = [
0.0,
0.0,
-5.0,
-10.0,
-15.0,
-20.0,
];
} else {
offsetsX = [
0.0,
0.0,
5.0,
10.0,
15.0,
20.0,
];
offsetsY = [0.0, 0.0, 0.0, 0.0, _swiperHeight, _swiperHeight];
}
}
@override
Widget _buildItem(int i, int realIndex, double animationValue) {
double s = _getValue(scales, animationValue, i);
double f = _getValue(offsetsX, animationValue, i);
double fy = _getValue(offsetsY, animationValue, i);
double o = _getValue(opacity, animationValue, i);
double a = _getValue(rotates, animationValue, i);
Alignment alignment = widget.scrollDirection == Axis.horizontal
? Alignment.bottomCenter
: Alignment.centerLeft;
return new Opacity(
opacity: o,
child: new Transform.rotate(
angle: a / 180.0,
child: new Transform.translate(
key: new ValueKey<int>(_currentIndex + i),
offset: new Offset(f, fy),
child: new Transform.scale(
scale: s,
alignment: alignment,
child: new SizedBox(
width: widget.itemWidth ?? double.infinity,
height: widget.itemHeight ?? double.infinity,
child: widget.itemBuilder(context, realIndex),
),
),
),
),
);
}
}
class _StackViewState extends _CustomLayoutStateBase<_StackSwiper> {
List<double> scales;
List<double> offsets;
List<double> opacity;
@override
void didChangeDependencies() {
super.didChangeDependencies();
}
void _updateValues() {
if (widget.scrollDirection == Axis.horizontal) {
double space = (_swiperWidth - widget.itemWidth) / 2;
offsets = [-space, -space / 3 * 2, -space / 3, 0.0, _swiperWidth];
} else {
double space = (_swiperHeight - widget.itemHeight) / 2;
offsets = [-space, -space / 3 * 2, -space / 3, 0.0, _swiperHeight];
}
}
@override
void didUpdateWidget(_StackSwiper oldWidget) {
_updateValues();
super.didUpdateWidget(oldWidget);
}
@override
void afterRender() {
super.afterRender();
//length of the values array below
_animationCount = 5;
//Array below this line, '0' index is 1.0 ,witch is the first item show in swiper.
_startIndex = -3;
scales = [0.7, 0.8, 0.9, 1.0, 1.0];
opacity = [0.0, 0.5, 1.0, 1.0, 1.0];
_updateValues();
}
@override
Widget _buildItem(int i, int realIndex, double animationValue) {
double s = _getValue(scales, animationValue, i);
double f = _getValue(offsets, animationValue, i);
double o = _getValue(opacity, animationValue, i);
Offset offset = widget.scrollDirection == Axis.horizontal
? new Offset(f, 0.0)
: new Offset(0.0, f);
Alignment alignment = widget.scrollDirection == Axis.horizontal
? Alignment.centerLeft
: Alignment.topCenter;
return new Opacity(
opacity: o,
child: new Transform.translate(
key: new ValueKey<int>(_currentIndex + i),
offset: offset,
child: new Transform.scale(
scale: s,
alignment: alignment,
child: new SizedBox(
width: widget.itemWidth ?? double.infinity,
height: widget.itemHeight ?? double.infinity,
child: widget.itemBuilder(context, realIndex),
),
),
),
);
}
}
class ScaleAndFadeTransformer extends PageTransformer {
final double _scale;
final double _fade;
ScaleAndFadeTransformer({double fade: 0.3, double scale: 0.8})
: _fade = fade,
_scale = scale;
@override
Widget transform(Widget item, TransformInfo info) {
double position = info.position;
Widget child = item;
if (_scale != null) {
double scaleFactor = (1 - position.abs()) * (1 - _scale);
double scale = _scale + scaleFactor;
child = new Transform.scale(
scale: scale,
child: item,
);
}
if (_fade != null) {
double fadeFactor = (1 - position.abs()) * (1 - _fade);
double opacity = _fade + fadeFactor;
child = new Opacity(
opacity: opacity,
child: child,
);
}
return child;
}
}