Skip to content

Commit c99fd70

Browse files
committed
bumped to version 2.1.0
1 parent 160dd55 commit c99fd70

File tree

4 files changed

+117
-35
lines changed

4 files changed

+117
-35
lines changed

CHANGELOG.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,14 @@
11
# Changelog
22

3+
## 2.1.0
4+
5+
* **Fix**: `issue #27` LICENSE updated to MIT.
6+
* **Fix**: `issue #24` fixed.
7+
* **Fix**: `issue #22` fixed.
8+
* **Fix**: `issue #20` fixed.
9+
* **Fix**: `issue #17` fixed.
10+
* **Improvement**: Performance improvements.
11+
312
## 2.0.4
413

514
* **Optimization**: Removed unnecessary codes.

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,15 +30,15 @@ A customizable carousel slider widget in Flutter which supports infinite scrolli
3030

3131
## Demo
3232

33-
[View Demo](https://nixrajput.github.io/flutter_carousel_widget)
33+
<a href="https://nixrajput.github.io/flutter_carousel_widget" target="_blank">View Demo</a>
3434

3535
## Installation
3636

3737
Add `flutter_carousel_widget` as a dependency in your `pubspec.yaml` file:
3838

3939
```dart
4040
dependencies:
41-
flutter_carousel_widget: latest_version
41+
flutter_carousel_widget: "latest_version"
4242
```
4343

4444
And import it:

example/lib/main.dart

Lines changed: 77 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ class FlutterCarouselWidgetDemo extends StatelessWidget {
6969
'/indicator': (ctx) => const CarouselWithIndicatorDemo(),
7070
'/multiple': (ctx) => const MultipleItemDemo(),
7171
'/expandable': (ctx) => const ExpandableCarouselDemo(),
72+
'/page_changed_reason': (ctx) => const PageChangedReason(),
7273
},
7374
theme: AppThemes.lightTheme,
7475
darkTheme: AppThemes.darkTheme,
@@ -150,6 +151,7 @@ class CarouselDemoHome extends StatelessWidget {
150151
DemoItem('Carousel with Custom Indicator Demo', '/indicator'),
151152
DemoItem('Multiple Item in One Screen Demo', '/multiple'),
152153
DemoItem('Expandable Carousel Demo', '/expandable'),
154+
DemoItem('Page Changed Reason Demo', "/page_changed_reason"),
153155
],
154156
),
155157
),
@@ -178,7 +180,7 @@ class ComplicatedImageDemo extends StatelessWidget {
178180
autoPlay: true,
179181
autoPlayInterval: const Duration(seconds: 3),
180182
disableCenter: true,
181-
viewportFraction: 0.8,
183+
viewportFraction: deviceSize.width > 800.0 ? 0.8 : 1.0,
182184
height: deviceSize.height * 0.45,
183185
indicatorMargin: 12.0,
184186
enableInfiniteScroll: true,
@@ -217,6 +219,80 @@ class EnlargeStrategyDemo extends StatelessWidget {
217219
}
218220
}
219221

222+
class PageChangedReason extends StatefulWidget {
223+
const PageChangedReason({Key? key}) : super(key: key);
224+
225+
@override
226+
State<PageChangedReason> createState() => _PageChangedReasonState();
227+
}
228+
229+
class _PageChangedReasonState extends State<PageChangedReason> {
230+
CarouselController? controller = CarouselController();
231+
bool autoplay = false;
232+
CarouselPageChangedReason? lastReason;
233+
234+
@override
235+
Widget build(BuildContext context) {
236+
return Scaffold(
237+
appBar: AppBar(
238+
elevation: 10,
239+
title: const Text('Page Changed Reason'),
240+
actions: [
241+
IconButton(
242+
onPressed: () => setState(() {
243+
autoplay = !autoplay;
244+
print('autoplay toggled');
245+
}),
246+
icon: Icon(autoplay ? Icons.pause : Icons.play_arrow),
247+
),
248+
IconButton(
249+
onPressed: () => setState(() {
250+
controller = controller == null ? CarouselController() : null;
251+
print('controller toggled');
252+
}),
253+
icon: Icon(
254+
controller == null ? Icons.gamepad_outlined : Icons.gamepad,
255+
),
256+
)
257+
],
258+
),
259+
body: Center(
260+
child: Column(
261+
mainAxisAlignment: MainAxisAlignment.center,
262+
children: <Widget>[
263+
Text(
264+
'Last changed reason:\n${lastReason != null ? lastReason!.toString() : 'None'}'),
265+
Expanded(
266+
child: FlutterCarousel(
267+
items: const [
268+
Padding(
269+
padding: EdgeInsets.all(8.0),
270+
child: Placeholder(
271+
fallbackHeight: 200.0,
272+
fallbackWidth: 200.0,
273+
),
274+
)
275+
],
276+
options: CarouselOptions(
277+
onPageChanged: (_, reason) => setState(
278+
() {
279+
lastReason = reason;
280+
print(reason);
281+
},
282+
),
283+
controller: controller,
284+
enableInfiniteScroll: true,
285+
autoPlay: autoplay,
286+
),
287+
),
288+
),
289+
],
290+
),
291+
),
292+
);
293+
}
294+
}
295+
220296
class ManuallyControlledSlider extends StatefulWidget {
221297
const ManuallyControlledSlider({Key? key}) : super(key: key);
222298

lib/src/_flutter_carousel_widget.dart

Lines changed: 29 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -55,14 +55,28 @@ class FlutterCarousel extends StatefulWidget {
5555
class FlutterCarouselState extends State<FlutterCarousel>
5656
with TickerProviderStateMixin {
5757
/// mode is related to why the page is being changed
58-
CarouselPageChangedReason mode = CarouselPageChangedReason.controller;
58+
CarouselPageChangedReason changeReasonMode =
59+
CarouselPageChangedReason.controller;
5960

6061
CarouselState? _carouselState;
6162
int _currentPage = 0;
6263
PageController? _pageController;
6364
double _pageDelta = 0.0;
6465
Timer? _timer;
6566

67+
void changeIndexPageDelta() {
68+
var realIndex = getRealIndex(
69+
_carouselState!.pageController!.page!.floor(),
70+
_carouselState!.realPage,
71+
widget.itemCount ?? widget.items?.length,
72+
);
73+
74+
WidgetsBinding.instance.addPostFrameCallback((_) => setState(() {
75+
_currentPage = realIndex;
76+
_pageDelta = _pageController!.page! - _pageController!.page!.floor();
77+
}));
78+
}
79+
6680
@override
6781
void didUpdateWidget(covariant FlutterCarousel oldWidget) {
6882
_carouselState!.options = options;
@@ -81,17 +95,7 @@ class FlutterCarouselState extends State<FlutterCarousel>
8195
/// handle autoplay when state changes
8296
_handleAutoPlay();
8397

84-
_pageController!.addListener(() {
85-
var realIndex = getRealIndex(
86-
_carouselState!.pageController!.page!.floor(),
87-
_carouselState!.realPage,
88-
widget.itemCount ?? widget.items?.length,
89-
);
90-
setState(() {
91-
_currentPage = realIndex;
92-
_pageDelta = _pageController!.page! - _pageController!.page!.floor();
93-
});
94-
});
98+
_pageController!.addListener(changeIndexPageDelta);
9599

96100
super.didUpdateWidget(oldWidget);
97101
}
@@ -110,7 +114,7 @@ class FlutterCarouselState extends State<FlutterCarousel>
110114
options,
111115
_clearTimer,
112116
_resumeTimer,
113-
_changeMode,
117+
changeMode,
114118
);
115119

116120
_carouselState!.itemCount = widget.itemCount;
@@ -130,17 +134,7 @@ class FlutterCarouselState extends State<FlutterCarousel>
130134

131135
_carouselState!.pageController = _pageController;
132136

133-
_pageController!.addListener(() {
134-
var realIndex = getRealIndex(
135-
_carouselState!.pageController!.page!.floor(),
136-
_carouselState!.realPage,
137-
widget.itemCount ?? widget.items?.length,
138-
);
139-
setState(() {
140-
_currentPage = realIndex;
141-
_pageDelta = _pageController!.page! - _pageController!.page!.floor();
142-
});
143-
});
137+
_pageController!.addListener(changeIndexPageDelta);
144138
}
145139

146140
CarouselControllerImpl get carouselController =>
@@ -162,8 +156,8 @@ class FlutterCarouselState extends State<FlutterCarousel>
162156
return;
163157
}
164158

165-
var previousReason = mode;
166-
_changeMode(CarouselPageChangedReason.timed);
159+
var previousReason = changeReasonMode;
160+
changeMode(CarouselPageChangedReason.timed);
167161

168162
var itemCount = widget.itemCount ?? widget.items!.length;
169163
var nextPage = _carouselState!.pageController!.page!.round() + 1;
@@ -183,12 +177,12 @@ class FlutterCarouselState extends State<FlutterCarousel>
183177
duration: widget.options.autoPlayAnimationDuration,
184178
curve: widget.options.autoPlayCurve,
185179
)
186-
.then((_) => _changeMode(previousReason));
180+
.then((_) => changeMode(previousReason));
187181
});
188182
}
189183

190-
void _changeMode(CarouselPageChangedReason mode) {
191-
mode = mode;
184+
void changeMode(CarouselPageChangedReason _mode) {
185+
changeReasonMode = _mode;
192186
}
193187

194188
/// Clear Timer
@@ -218,7 +212,7 @@ class FlutterCarouselState extends State<FlutterCarousel>
218212

219213
/// onStart
220214
void _onStart() {
221-
_changeMode(CarouselPageChangedReason.manual);
215+
changeMode(CarouselPageChangedReason.manual);
222216
}
223217

224218
/// onPanDown
@@ -227,7 +221,7 @@ class FlutterCarouselState extends State<FlutterCarousel>
227221
_clearTimer();
228222
}
229223

230-
_changeMode(CarouselPageChangedReason.manual);
224+
changeMode(CarouselPageChangedReason.manual);
231225
}
232226

233227
/// onPanEnd
@@ -342,7 +336,10 @@ class FlutterCarouselState extends State<FlutterCarousel>
342336
);
343337

344338
if (widget.options.onPageChanged != null) {
345-
widget.options.onPageChanged!(currentPage, mode);
339+
widget.options.onPageChanged!(
340+
currentPage,
341+
changeReasonMode,
342+
);
346343
}
347344
},
348345
itemCount: widget.options.enableInfiniteScroll ? null : widget.itemCount,

0 commit comments

Comments
 (0)