@@ -3,7 +3,7 @@ import 'package:flutter/material.dart';
3
3
4
4
class GFCarousel extends StatefulWidget {
5
5
/// Creates slide show of Images and [Widget] with animation for sliding.
6
- GFCarousel ({
6
+ const GFCarousel ({
7
7
@required this .items,
8
8
this .pagerSize,
9
9
this .passiveIndicator,
@@ -13,7 +13,6 @@ class GFCarousel extends StatefulWidget {
13
13
this .aspectRatio = 16 / 9 ,
14
14
this .viewportFraction = 0.8 ,
15
15
this .initialPage = 0 ,
16
- int realPage = 10000 ,
17
16
this .enableInfiniteScroll = true ,
18
17
this .reverse = false ,
19
18
this .autoPlay = false ,
@@ -25,12 +24,7 @@ class GFCarousel extends StatefulWidget {
25
24
this .onPageChanged,
26
25
this .scrollPhysics,
27
26
this .scrollDirection = Axis .horizontal,
28
- }) : realPage = enableInfiniteScroll ? realPage + initialPage : initialPage,
29
- pageController = PageController (
30
- viewportFraction: viewportFraction,
31
- initialPage:
32
- enableInfiniteScroll ? realPage + initialPage : initialPage,
33
- );
27
+ });
34
28
35
29
/// The pagination dots size can be defined using [double] .
36
30
final double pagerSize;
@@ -59,9 +53,6 @@ class GFCarousel extends StatefulWidget {
59
53
/// The initial page to show when first creating the [GFCarousel] . Defaults to 0.
60
54
final num initialPage;
61
55
62
- /// The actual index of the [PageView] .
63
- final num realPage;
64
-
65
56
/// Determines if slides should loop infinitely or be limited to item length. Defaults to true, i.e. infinite loop.
66
57
final bool enableInfiniteScroll;
67
58
@@ -107,48 +98,44 @@ class GFCarousel extends StatefulWidget {
107
98
/// Defaults to matching platform conventions.
108
99
final ScrollPhysics scrollPhysics;
109
100
110
- /// [pageController] is created using the properties passed to the constructor
111
- /// and can be used to control the [PageView] it is passed to.
112
- final PageController pageController;
113
-
114
101
/// Animates the controlled [GFCarousel] to the next page.
115
102
///
116
103
/// The animation lasts for the given duration and follows the given curve.
117
104
/// The returned [Future] resolves when the animation completes.
118
- Future <void > nextPage ({Duration duration, Curve curve}) =>
119
- pageController.nextPage (duration: duration, curve: curve);
120
-
121
- /// Animates the controlled [GFCarousel] to the previous page.
122
- ///
123
- /// The animation lasts for the given duration and follows the given curve.
124
- /// The returned [Future] resolves when the animation completes.
125
- Future <void > previousPage ({Duration duration, Curve curve}) =>
126
- pageController.previousPage (duration: duration, curve: curve);
127
-
128
- /// Changes which page is displayed in the controlled [GFCarousel] .
129
- ///
130
- /// Jumps the page position from its current value to the given value,
131
- /// without animation, and without checking if the new value is in range.
132
- void jumpToPage (int page) {
133
- final index =
134
- _getRealIndex (pageController.page.toInt (), realPage, items.length);
135
- return pageController
136
- .jumpToPage (pageController.page.toInt () + page - index);
137
- }
138
-
139
- /// Animates the controlled [GFCarousel] from the current page to the given page.
140
- ///
141
- /// The animation lasts for the given duration and follows the given curve.
142
- /// The returned [Future] resolves when the animation completes.
143
- Future <void > animateToPage (int page, {Duration duration, Curve curve}) {
144
- final index =
145
- _getRealIndex (pageController.page.toInt (), realPage, items.length);
146
- return pageController.animateToPage (
147
- pageController.page.toInt () + page - index,
148
- duration: duration,
149
- curve: curve,
150
- );
151
- }
105
+ // Future<void> nextPage({Duration duration, Curve curve}) =>
106
+ // pageController.nextPage(duration: duration, curve: curve);
107
+ //
108
+ // // / Animates the controlled [GFCarousel] to the previous page.
109
+ // // /
110
+ // // / The animation lasts for the given duration and follows the given curve.
111
+ // // / The returned [Future] resolves when the animation completes.
112
+ // Future<void> previousPage({Duration duration, Curve curve}) =>
113
+ // pageController.previousPage(duration: duration, curve: curve);
114
+ //
115
+ // // / Changes which page is displayed in the controlled [GFCarousel].
116
+ // // /
117
+ // // / Jumps the page position from its current value to the given value,
118
+ // // / without animation, and without checking if the new value is in range.
119
+ // void jumpToPage(int page) {
120
+ // final index =
121
+ // _getRealIndex(pageController.page.toInt(), realPage, items.length);
122
+ // return pageController
123
+ // .jumpToPage(pageController.page.toInt() + page - index);
124
+ // }
125
+ //
126
+ // // / Animates the controlled [GFCarousel] from the current page to the given page.
127
+ // // /
128
+ // // / The animation lasts for the given duration and follows the given curve.
129
+ // // / The returned [Future] resolves when the animation completes.
130
+ // Future<void> animateToPage(int page, {Duration duration, Curve curve}) {
131
+ // final index =
132
+ // _getRealIndex(pageController.page.toInt(), realPage, items.length);
133
+ // return pageController.animateToPage(
134
+ // pageController.page.toInt() + page - index,
135
+ // duration: duration,
136
+ // curve: curve,
137
+ // );
138
+ // }
152
139
153
140
List <T > map <T >(List list, Function handler) {
154
141
List <T > result;
@@ -173,15 +160,31 @@ class _GFCarouselState extends State<GFCarousel> with TickerProviderStateMixin {
173
160
/// Width of cells container
174
161
double width = 0 ;
175
162
163
+ /// [pageController] is created using the properties passed to the constructor
164
+ /// and can be used to control the [PageView] it is passed to.
165
+ PageController pageController;
166
+
167
+ /// The actual index of the [PageView] .
168
+ int realPage = 10000 ;
169
+
176
170
@override
177
171
void initState () {
178
172
super .initState ();
173
+ realPage = widget.enableInfiniteScroll
174
+ ? realPage + widget.initialPage
175
+ : widget.initialPage;
176
+ pageController = PageController (
177
+ viewportFraction: widget.viewportFraction,
178
+ initialPage: widget.enableInfiniteScroll
179
+ ? realPage + widget.initialPage
180
+ : widget.initialPage,
181
+ );
179
182
timer = getPlayTimer ();
180
183
}
181
184
182
185
Timer getPlayTimer () => Timer .periodic (widget.autoPlayInterval, (_) {
183
186
if (widget.autoPlay && widget.items.length > 1 ) {
184
- widget. pageController.nextPage (
187
+ pageController.nextPage (
185
188
duration: widget.autoPlayAnimationDuration,
186
189
curve: widget.autoPlayCurve);
187
190
}
@@ -230,7 +233,7 @@ class _GFCarouselState extends State<GFCarousel> with TickerProviderStateMixin {
230
233
getPageWrapper (PageView .builder (
231
234
physics: widget.scrollPhysics,
232
235
scrollDirection: widget.scrollDirection,
233
- controller: widget. pageController,
236
+ controller: pageController,
234
237
reverse: widget.reverse,
235
238
itemCount: widget.items.length == 1
236
239
? widget.items.length
@@ -239,8 +242,8 @@ class _GFCarouselState extends State<GFCarousel> with TickerProviderStateMixin {
239
242
: widget.items.length,
240
243
onPageChanged: (int index) {
241
244
int currentPage;
242
- currentPage = _getRealIndex (index + widget.initialPage,
243
- widget.realPage, widget.items.length);
245
+ currentPage = _getRealIndex (
246
+ index + widget.initialPage, realPage, widget.items.length);
244
247
if (widget.onPageChanged != null ) {
245
248
widget.onPageChanged (currentPage);
246
249
}
@@ -253,29 +256,47 @@ class _GFCarouselState extends State<GFCarousel> with TickerProviderStateMixin {
253
256
itemBuilder: (BuildContext context, int i) {
254
257
final int index = _getRealIndex (
255
258
i + widget.initialPage,
256
- widget. realPage,
259
+ realPage,
257
260
widget.items.length,
258
261
);
262
+
259
263
currentSlide = index;
260
264
return AnimatedBuilder (
261
- animation: widget. pageController,
265
+ animation: pageController,
262
266
child: widget.items[index],
263
267
builder: (BuildContext context, child) {
264
268
// on the first render, the pageController.page is null,
265
269
// this is a dirty hack
266
- if (widget.pageController.position.minScrollExtent == null ||
267
- widget.pageController.position.maxScrollExtent == null ) {
268
- Future .delayed (const Duration (microseconds: 1 ), () {
269
- setState (() {});
270
- });
271
- return Container ();
270
+
271
+ // if (pageController.position.minScrollExtent == null ||
272
+ // pageController.position.maxScrollExtent == null) {
273
+ // Future.delayed(const Duration(microseconds: 1), () {});
274
+ // return Container();
275
+ // }
276
+
277
+ double value;
278
+ try {
279
+ value = pageController.page - i;
280
+ // ignore: avoid_catches_without_on_clauses
281
+ } catch (e) {
282
+ // value = 1;
283
+ final BuildContext storageContext =
284
+ pageController.position.context.storageContext;
285
+ final double previousSavedPosition =
286
+ PageStorage .of (storageContext)
287
+ ? .readState (storageContext);
288
+ if (previousSavedPosition != null ) {
289
+ value = previousSavedPosition - i.toDouble ();
290
+ } else {
291
+ value = realPage.toDouble () - i.toDouble ();
292
+ }
272
293
}
273
- double value = widget.pageController.page - i;
274
294
value = (1 - (value.abs () * 0.3 )).clamp (0.0 , 1.0 );
275
295
276
296
final double height = widget.height ??
277
297
MediaQuery .of (context).size.width *
278
298
(1 / widget.aspectRatio);
299
+
279
300
final double distortionValue = widget.enlargeMainPage
280
301
? Curves .easeOut.transform (value)
281
302
: 1.0 ;
0 commit comments