@@ -3,7 +3,7 @@ import 'package:flutter/material.dart';
33
44class GFCarousel extends StatefulWidget {
55 /// Creates slide show of Images and [Widget] with animation for sliding.
6- GFCarousel ({
6+ const GFCarousel ({
77 @required this .items,
88 this .pagerSize,
99 this .passiveIndicator,
@@ -13,7 +13,6 @@ class GFCarousel extends StatefulWidget {
1313 this .aspectRatio = 16 / 9 ,
1414 this .viewportFraction = 0.8 ,
1515 this .initialPage = 0 ,
16- int realPage = 10000 ,
1716 this .enableInfiniteScroll = true ,
1817 this .reverse = false ,
1918 this .autoPlay = false ,
@@ -25,12 +24,7 @@ class GFCarousel extends StatefulWidget {
2524 this .onPageChanged,
2625 this .scrollPhysics,
2726 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+ });
3428
3529 /// The pagination dots size can be defined using [double] .
3630 final double pagerSize;
@@ -59,9 +53,6 @@ class GFCarousel extends StatefulWidget {
5953 /// The initial page to show when first creating the [GFCarousel] . Defaults to 0.
6054 final num initialPage;
6155
62- /// The actual index of the [PageView] .
63- final num realPage;
64-
6556 /// Determines if slides should loop infinitely or be limited to item length. Defaults to true, i.e. infinite loop.
6657 final bool enableInfiniteScroll;
6758
@@ -107,48 +98,44 @@ class GFCarousel extends StatefulWidget {
10798 /// Defaults to matching platform conventions.
10899 final ScrollPhysics scrollPhysics;
109100
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-
114101 /// Animates the controlled [GFCarousel] to the next page.
115102 ///
116103 /// The animation lasts for the given duration and follows the given curve.
117104 /// 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+ // }
152139
153140 List <T > map <T >(List list, Function handler) {
154141 List <T > result;
@@ -173,15 +160,31 @@ class _GFCarouselState extends State<GFCarousel> with TickerProviderStateMixin {
173160 /// Width of cells container
174161 double width = 0 ;
175162
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+
176170 @override
177171 void initState () {
178172 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+ );
179182 timer = getPlayTimer ();
180183 }
181184
182185 Timer getPlayTimer () => Timer .periodic (widget.autoPlayInterval, (_) {
183186 if (widget.autoPlay && widget.items.length > 1 ) {
184- widget. pageController.nextPage (
187+ pageController.nextPage (
185188 duration: widget.autoPlayAnimationDuration,
186189 curve: widget.autoPlayCurve);
187190 }
@@ -230,7 +233,7 @@ class _GFCarouselState extends State<GFCarousel> with TickerProviderStateMixin {
230233 getPageWrapper (PageView .builder (
231234 physics: widget.scrollPhysics,
232235 scrollDirection: widget.scrollDirection,
233- controller: widget. pageController,
236+ controller: pageController,
234237 reverse: widget.reverse,
235238 itemCount: widget.items.length == 1
236239 ? widget.items.length
@@ -239,8 +242,8 @@ class _GFCarouselState extends State<GFCarousel> with TickerProviderStateMixin {
239242 : widget.items.length,
240243 onPageChanged: (int index) {
241244 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);
244247 if (widget.onPageChanged != null ) {
245248 widget.onPageChanged (currentPage);
246249 }
@@ -253,29 +256,47 @@ class _GFCarouselState extends State<GFCarousel> with TickerProviderStateMixin {
253256 itemBuilder: (BuildContext context, int i) {
254257 final int index = _getRealIndex (
255258 i + widget.initialPage,
256- widget. realPage,
259+ realPage,
257260 widget.items.length,
258261 );
262+
259263 currentSlide = index;
260264 return AnimatedBuilder (
261- animation: widget. pageController,
265+ animation: pageController,
262266 child: widget.items[index],
263267 builder: (BuildContext context, child) {
264268 // on the first render, the pageController.page is null,
265269 // 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+ }
272293 }
273- double value = widget.pageController.page - i;
274294 value = (1 - (value.abs () * 0.3 )).clamp (0.0 , 1.0 );
275295
276296 final double height = widget.height ??
277297 MediaQuery .of (context).size.width *
278298 (1 / widget.aspectRatio);
299+
279300 final double distortionValue = widget.enlargeMainPage
280301 ? Curves .easeOut.transform (value)
281302 : 1.0 ;
0 commit comments