diff --git a/example/lib/pokemon_types.dart b/example/lib/pokemon_types.dart index 1504291..5b108f5 100644 --- a/example/lib/pokemon_types.dart +++ b/example/lib/pokemon_types.dart @@ -17,4 +17,4 @@ enum PokemonType { dragon, steel, fairy, -} \ No newline at end of file +} diff --git a/lib/src/carousel.dart b/lib/src/carousel.dart index 6337b17..cd08f97 100644 --- a/lib/src/carousel.dart +++ b/lib/src/carousel.dart @@ -21,6 +21,8 @@ class Carousel extends StatefulWidget { this.onPageChanged, this.alignment = AlignmentDirectional.topStart, this.onCardClick, + this.initialPage = 0, + this.allowInfiniteScrollingBackwards = false, Key? key, }) : super(key: key); @@ -46,16 +48,29 @@ class Carousel extends StatefulWidget { /// Size of the pageview used to capture swipe gestures. final double pageViewHeight; + /// The page to show when first creating the [Carousel]. + final int initialPage; + + /// Whether to allow infinite scrolling backwards. Defaults to false. If true, this works by using a very large number of pages (10000). Works in conjunction with [initialPage]. + final bool allowInfiniteScrollingBackwards; + @override State createState() => _CarouselState(); } class _CarouselState extends State { - final PageController _pageController = PageController(initialPage: 0); + late PageController _pageController; double _currentPage = 0; @override void initState() { + _pageController = PageController( + initialPage: widget.allowInfiniteScrollingBackwards + ? 10000 + widget.initialPage + : widget.initialPage, + ); + _currentPage = _pageController.initialPage.toDouble(); + _pageController.addListener(() { _currentPage = _pageController.page!; });