Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

YaruCarousel: add navigation controls #139

Merged
merged 2 commits into from
May 28, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions example/lib/pages/carousel_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ class _CarouselPageState extends State<CarouselPage> {
YaruCarousel(
children: _getCarouselChildren(),
height: 400,
navigationControls: true,
previousIcon: Icon(YaruIcons.go_previous),
nextIcon: Icon(YaruIcons.go_next),
),
]),
YaruSection(headline: 'Auto scroll: on', width: 700, children: [
Expand Down
107 changes: 90 additions & 17 deletions lib/src/yaru_carousel.dart
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ class YaruCarousel extends StatefulWidget {
this.placeIndicator = true,
this.placeIndicatorMarginTop = 12.0,
this.viewportFraction = 0.8,
this.navigationControls = false,
this.previousIcon,
this.nextIcon,
}) : super(key: key);

/// The height of the children, defaults to 500.0.
Expand Down Expand Up @@ -48,6 +51,17 @@ class YaruCarousel extends StatefulWidget {
/// The fraction of the viewport that each page should occupy.
final double viewportFraction;

/// Display previous and next navigation buttons
final bool navigationControls;

/// Icon used for the previous button
/// Require [navigationControls] to be true
final Widget? previousIcon;

/// Icon used for the next button
/// Require [navigationControls] to be true
final Widget? nextIcon;

@override
State<YaruCarousel> createState() => _YaruCarouselState();
}
Expand All @@ -67,9 +81,7 @@ class _YaruCarouselState extends State<YaruCarousel> {
initialPage: _index,
);

if (widget.autoScroll) {
_startTimer();
}
_startTimer();
}

@override
Expand All @@ -84,10 +96,7 @@ class _YaruCarouselState extends State<YaruCarousel> {
@override
void dispose() {
super.dispose();

if (widget.autoScroll) {
_cancelTimer();
}
_cancelTimer();
}

@override
Expand All @@ -111,8 +120,7 @@ class _YaruCarouselState extends State<YaruCarousel> {
}

Widget _buildCarousel() {
return Expanded(
child: PageView.builder(
final carousel = PageView.builder(
itemCount: widget.children.length,
pageSnapping: true,
controller: _pageController,
Expand All @@ -137,6 +145,39 @@ class _YaruCarouselState extends State<YaruCarousel> {
: widget.children[index],
),
),
);

if (widget.navigationControls) {
return Expanded(
child: Stack(
children: [
carousel,
_buildNavigationButton(
Alignment.centerLeft,
_isFirstPage() ? null : () => _animateToPreviousPage(),
widget.previousIcon ?? const Icon(Icons.arrow_back)),
_buildNavigationButton(
Alignment.centerRight,
_isLastPage() ? null : () => _animateToNextPage(),
widget.nextIcon ?? const Icon(Icons.arrow_forward)),
],
));
}

return Expanded(child: carousel);
}

Widget _buildNavigationButton(
AlignmentGeometry alignement, VoidCallback? onPressed, Widget icon) {
return Positioned.fill(
child: Align(
alignment: alignement,
child: OutlinedButton(
style: OutlinedButton.styleFrom(
shape: const CircleBorder(),
backgroundColor: Theme.of(context).colorScheme.background),
onPressed: onPressed,
child: icon),
));
}

Expand Down Expand Up @@ -198,26 +239,58 @@ class _YaruCarouselState extends State<YaruCarousel> {
);
}

bool _isFirstPage() {
return _index == 0;
}

bool _isLastPage() {
return _index == widget.children.length - 1;
}

void _animateToPage(int pageIndex) {
_pageController.animateToPage(
pageIndex,
duration: _kAnimationDuration,
curve: _kAnimationCurve,
);

if (widget.autoScroll) {
_cancelTimer();
_startTimer();
}
_restartTimer();
}

void _animateToPreviousPage() {
_pageController.previousPage(
duration: _kAnimationDuration,
curve: _kAnimationCurve,
);

_restartTimer();
}

void _animateToNextPage() {
_pageController.nextPage(
duration: _kAnimationDuration,
curve: _kAnimationCurve,
);

_restartTimer();
}

void _startTimer() {
_timer = Timer.periodic(widget.autoScrollDuration, (timer) {
_animateToPage(_index >= widget.children.length ? 0 : _index++);
});
if (widget.autoScroll) {
_timer = Timer.periodic(widget.autoScrollDuration, (timer) {
_animateToPage(_index >= widget.children.length ? 0 : _index++);
});
}
}

void _cancelTimer() {
_timer.cancel();
if (widget.autoScroll) {
_timer.cancel();
}
}

void _restartTimer() {
_cancelTimer();
_startTimer();
}
}