@@ -6,26 +6,37 @@ import '../carousel_state/expandable_carousel_state.dart';
66import '../enums/carousel_page_changed_reason.dart' ;
77import '../utils/flutter_carousel_utils.dart' ;
88
9+ /// Abstract controller class to handle carousel interactions like navigation and auto-play.
910abstract class ExpandableCarouselController {
11+ /// Factory constructor that returns an implementation of the controller.
1012 factory ExpandableCarouselController () => ExpandableCarouselControllerImpl ();
1113
14+ /// Indicates if the controller is ready.
1215 bool get ready;
1316
17+ /// Future that completes when the controller is ready.
1418 Future <void > get onReady;
1519
20+ /// Navigates to the next page in the carousel.
1621 void nextPage ({Duration ? duration, Curve ? curve});
1722
23+ /// Navigates to the previous page in the carousel.
1824 void previousPage ({Duration ? duration, Curve ? curve});
1925
26+ /// Jumps to a specific page in the carousel without animation.
2027 void jumpToPage (int page);
2128
29+ /// Animates the carousel to a specific page.
2230 void animateToPage (int page, {Duration ? duration, Curve ? curve});
2331
32+ /// Starts the auto-play functionality of the carousel.
2433 void startAutoPlay ();
2534
35+ /// Stops the auto-play functionality of the carousel.
2636 void stopAutoPlay ();
2737}
2838
39+ /// Implementation of the ExpandableCarouselController interface.
2940class ExpandableCarouselControllerImpl implements ExpandableCarouselController {
3041 final Completer <void > _readyCompleter = Completer <void >();
3142 ExpandableCarouselState ? _state;
@@ -34,19 +45,23 @@ class ExpandableCarouselControllerImpl implements ExpandableCarouselController {
3445 /// The animation lasts for the given duration and follows the given curve.
3546 /// The returned [Future] resolves when the animation completes.
3647 @override
37- void animateToPage (int page,
38- {Duration ? duration = const Duration (milliseconds: 300 ),
39- Curve ? curve = Curves .linear}) async {
48+ void animateToPage (
49+ int page, {
50+ Duration ? duration = const Duration (milliseconds: 300 ),
51+ Curve ? curve = Curves .linear,
52+ }) async {
4053 final isNeedResetTimer = _state! .options.pauseAutoPlayOnManualNavigate;
4154
4255 if (isNeedResetTimer) {
4356 _state! .onResetTimer ();
4457 }
4558
59+ // Calculate the real index to animate to.
4660 final index = getRealIndex (_state! .pageController? .page! .toInt () ?? 0 ,
4761 _state! .realPage - _state! .initialPage, _state! .itemCount);
4862 _setModeController ();
4963
64+ // Animate to the target page.
5065 await _state! .pageController! .animateToPage (
5166 (_state! .pageController? .page! .toInt () ?? 0 ) + page - index,
5267 duration: duration! ,
@@ -67,6 +82,7 @@ class ExpandableCarouselControllerImpl implements ExpandableCarouselController {
6782
6883 _setModeController ();
6984
85+ // Jump directly to the target page.
7086 return _state! .pageController? .jumpToPage (
7187 (_state! .pageController? .page! .toInt () ?? 0 ) + page - index);
7288 }
@@ -75,9 +91,10 @@ class ExpandableCarouselControllerImpl implements ExpandableCarouselController {
7591 /// The animation lasts for the given duration and follows the given curve.
7692 /// The returned [Future] resolves when the animation completes.
7793 @override
78- void nextPage (
79- {Duration ? duration = const Duration (milliseconds: 300 ),
80- Curve ? curve = Curves .linear}) async {
94+ void nextPage ({
95+ Duration ? duration = const Duration (milliseconds: 300 ),
96+ Curve ? curve = Curves .linear,
97+ }) async {
8198 final isNeedResetTimer = _state! .options.pauseAutoPlayOnManualNavigate;
8299
83100 if (isNeedResetTimer) {
@@ -86,23 +103,26 @@ class ExpandableCarouselControllerImpl implements ExpandableCarouselController {
86103
87104 _setModeController ();
88105
106+ // Navigate to the next page.
89107 await _state! .pageController? .nextPage (duration: duration! , curve: curve! );
90108
91109 if (isNeedResetTimer) {
92110 _state! .onResumeTimer ();
93111 }
94112 }
95113
114+ /// Future that completes when the controller is ready.
96115 @override
97116 Future <void > get onReady => _readyCompleter.future;
98117
99118 /// Animates the controlled [ExpandableCarousel] to the previous page.
100119 /// The animation lasts for the given duration and follows the given curve.
101120 /// The returned [Future] resolves when the animation completes.
102121 @override
103- void previousPage (
104- {Duration ? duration = const Duration (milliseconds: 300 ),
105- Curve ? curve = Curves .linear}) async {
122+ void previousPage ({
123+ Duration ? duration = const Duration (milliseconds: 300 ),
124+ Curve ? curve = Curves .linear,
125+ }) async {
106126 final isNeedResetTimer = _state! .options.pauseAutoPlayOnManualNavigate;
107127
108128 if (isNeedResetTimer) {
@@ -111,6 +131,7 @@ class ExpandableCarouselControllerImpl implements ExpandableCarouselController {
111131
112132 _setModeController ();
113133
134+ // Navigate to the previous page.
114135 await _state! .pageController
115136 ? .previousPage (duration: duration! , curve: curve! );
116137
@@ -119,6 +140,7 @@ class ExpandableCarouselControllerImpl implements ExpandableCarouselController {
119140 }
120141 }
121142
143+ /// Checks if the controller is ready to interact with the carousel.
122144 @override
123145 bool get ready => _state != null ;
124146
@@ -138,13 +160,15 @@ class ExpandableCarouselControllerImpl implements ExpandableCarouselController {
138160 _state! .onResetTimer ();
139161 }
140162
163+ /// Sets the current state of the carousel.
141164 set state (ExpandableCarouselState ? state) {
142165 _state = state;
143166 if (! _readyCompleter.isCompleted) {
144167 _readyCompleter.complete ();
145168 }
146169 }
147170
171+ /// Sets the carousel change mode to controller-based.
148172 void _setModeController () =>
149173 _state! .changeMode (CarouselPageChangedReason .controller);
150174}
0 commit comments