Skip to content

Commit

Permalink
bumped to version 2.1.0
Browse files Browse the repository at this point in the history
  • Loading branch information
nixrajput committed Aug 12, 2023
1 parent 160dd55 commit c99fd70
Show file tree
Hide file tree
Showing 4 changed files with 117 additions and 35 deletions.
9 changes: 9 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
# Changelog

## 2.1.0

* **Fix**: `issue #27` LICENSE updated to MIT.
* **Fix**: `issue #24` fixed.
* **Fix**: `issue #22` fixed.
* **Fix**: `issue #20` fixed.
* **Fix**: `issue #17` fixed.
* **Improvement**: Performance improvements.

## 2.0.4

* **Optimization**: Removed unnecessary codes.
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,15 @@ A customizable carousel slider widget in Flutter which supports infinite scrolli

## Demo

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

## Installation

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

```dart
dependencies:
flutter_carousel_widget: latest_version
flutter_carousel_widget: "latest_version"
```

And import it:
Expand Down
78 changes: 77 additions & 1 deletion example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ class FlutterCarouselWidgetDemo extends StatelessWidget {
'/indicator': (ctx) => const CarouselWithIndicatorDemo(),
'/multiple': (ctx) => const MultipleItemDemo(),
'/expandable': (ctx) => const ExpandableCarouselDemo(),
'/page_changed_reason': (ctx) => const PageChangedReason(),
},
theme: AppThemes.lightTheme,
darkTheme: AppThemes.darkTheme,
Expand Down Expand Up @@ -150,6 +151,7 @@ class CarouselDemoHome extends StatelessWidget {
DemoItem('Carousel with Custom Indicator Demo', '/indicator'),
DemoItem('Multiple Item in One Screen Demo', '/multiple'),
DemoItem('Expandable Carousel Demo', '/expandable'),
DemoItem('Page Changed Reason Demo', "/page_changed_reason"),
],
),
),
Expand Down Expand Up @@ -178,7 +180,7 @@ class ComplicatedImageDemo extends StatelessWidget {
autoPlay: true,
autoPlayInterval: const Duration(seconds: 3),
disableCenter: true,
viewportFraction: 0.8,
viewportFraction: deviceSize.width > 800.0 ? 0.8 : 1.0,
height: deviceSize.height * 0.45,
indicatorMargin: 12.0,
enableInfiniteScroll: true,
Expand Down Expand Up @@ -217,6 +219,80 @@ class EnlargeStrategyDemo extends StatelessWidget {
}
}

class PageChangedReason extends StatefulWidget {
const PageChangedReason({Key? key}) : super(key: key);

@override
State<PageChangedReason> createState() => _PageChangedReasonState();
}

class _PageChangedReasonState extends State<PageChangedReason> {
CarouselController? controller = CarouselController();
bool autoplay = false;
CarouselPageChangedReason? lastReason;

@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
elevation: 10,
title: const Text('Page Changed Reason'),
actions: [
IconButton(
onPressed: () => setState(() {
autoplay = !autoplay;
print('autoplay toggled');
}),
icon: Icon(autoplay ? Icons.pause : Icons.play_arrow),
),
IconButton(
onPressed: () => setState(() {
controller = controller == null ? CarouselController() : null;
print('controller toggled');
}),
icon: Icon(
controller == null ? Icons.gamepad_outlined : Icons.gamepad,
),
)
],
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text(
'Last changed reason:\n${lastReason != null ? lastReason!.toString() : 'None'}'),
Expanded(
child: FlutterCarousel(
items: const [
Padding(
padding: EdgeInsets.all(8.0),
child: Placeholder(
fallbackHeight: 200.0,
fallbackWidth: 200.0,
),
)
],
options: CarouselOptions(
onPageChanged: (_, reason) => setState(
() {
lastReason = reason;
print(reason);
},
),
controller: controller,
enableInfiniteScroll: true,
autoPlay: autoplay,
),
),
),
],
),
),
);
}
}

class ManuallyControlledSlider extends StatefulWidget {
const ManuallyControlledSlider({Key? key}) : super(key: key);

Expand Down
61 changes: 29 additions & 32 deletions lib/src/_flutter_carousel_widget.dart
Original file line number Diff line number Diff line change
Expand Up @@ -55,14 +55,28 @@ class FlutterCarousel extends StatefulWidget {
class FlutterCarouselState extends State<FlutterCarousel>
with TickerProviderStateMixin {
/// mode is related to why the page is being changed
CarouselPageChangedReason mode = CarouselPageChangedReason.controller;
CarouselPageChangedReason changeReasonMode =
CarouselPageChangedReason.controller;

CarouselState? _carouselState;
int _currentPage = 0;
PageController? _pageController;
double _pageDelta = 0.0;
Timer? _timer;

void changeIndexPageDelta() {
var realIndex = getRealIndex(
_carouselState!.pageController!.page!.floor(),
_carouselState!.realPage,
widget.itemCount ?? widget.items?.length,
);

WidgetsBinding.instance.addPostFrameCallback((_) => setState(() {
_currentPage = realIndex;
_pageDelta = _pageController!.page! - _pageController!.page!.floor();
}));
}

@override
void didUpdateWidget(covariant FlutterCarousel oldWidget) {
_carouselState!.options = options;
Expand All @@ -81,17 +95,7 @@ class FlutterCarouselState extends State<FlutterCarousel>
/// handle autoplay when state changes
_handleAutoPlay();

_pageController!.addListener(() {
var realIndex = getRealIndex(
_carouselState!.pageController!.page!.floor(),
_carouselState!.realPage,
widget.itemCount ?? widget.items?.length,
);
setState(() {
_currentPage = realIndex;
_pageDelta = _pageController!.page! - _pageController!.page!.floor();
});
});
_pageController!.addListener(changeIndexPageDelta);

super.didUpdateWidget(oldWidget);
}
Expand All @@ -110,7 +114,7 @@ class FlutterCarouselState extends State<FlutterCarousel>
options,
_clearTimer,
_resumeTimer,
_changeMode,
changeMode,
);

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

_carouselState!.pageController = _pageController;

_pageController!.addListener(() {
var realIndex = getRealIndex(
_carouselState!.pageController!.page!.floor(),
_carouselState!.realPage,
widget.itemCount ?? widget.items?.length,
);
setState(() {
_currentPage = realIndex;
_pageDelta = _pageController!.page! - _pageController!.page!.floor();
});
});
_pageController!.addListener(changeIndexPageDelta);
}

CarouselControllerImpl get carouselController =>
Expand All @@ -162,8 +156,8 @@ class FlutterCarouselState extends State<FlutterCarousel>
return;
}

var previousReason = mode;
_changeMode(CarouselPageChangedReason.timed);
var previousReason = changeReasonMode;
changeMode(CarouselPageChangedReason.timed);

var itemCount = widget.itemCount ?? widget.items!.length;
var nextPage = _carouselState!.pageController!.page!.round() + 1;
Expand All @@ -183,12 +177,12 @@ class FlutterCarouselState extends State<FlutterCarousel>
duration: widget.options.autoPlayAnimationDuration,
curve: widget.options.autoPlayCurve,
)
.then((_) => _changeMode(previousReason));
.then((_) => changeMode(previousReason));
});
}

void _changeMode(CarouselPageChangedReason mode) {
mode = mode;
void changeMode(CarouselPageChangedReason _mode) {
changeReasonMode = _mode;
}

/// Clear Timer
Expand Down Expand Up @@ -218,7 +212,7 @@ class FlutterCarouselState extends State<FlutterCarousel>

/// onStart
void _onStart() {
_changeMode(CarouselPageChangedReason.manual);
changeMode(CarouselPageChangedReason.manual);
}

/// onPanDown
Expand All @@ -227,7 +221,7 @@ class FlutterCarouselState extends State<FlutterCarousel>
_clearTimer();
}

_changeMode(CarouselPageChangedReason.manual);
changeMode(CarouselPageChangedReason.manual);
}

/// onPanEnd
Expand Down Expand Up @@ -342,7 +336,10 @@ class FlutterCarouselState extends State<FlutterCarousel>
);

if (widget.options.onPageChanged != null) {
widget.options.onPageChanged!(currentPage, mode);
widget.options.onPageChanged!(
currentPage,
changeReasonMode,
);
}
},
itemCount: widget.options.enableInfiniteScroll ? null : widget.itemCount,
Expand Down

0 comments on commit c99fd70

Please sign in to comment.