1
0
mirror of https://github.com/flutter/samples.git synced 2025-11-09 22:38:42 +00:00

[samples_index] Fix carousel edge cases and make mobile responsive (#608)

* Fix carousel ui issues

* Update carousel to be mobile responsive
This commit is contained in:
Sashika Nawarathne
2020-12-08 00:09:50 +05:30
committed by GitHub
parent 6b561158b1
commit d1b054cb02
2 changed files with 67 additions and 33 deletions

View File

@@ -14,13 +14,30 @@ class Carousel {
Element prevSlide, currentSlide, nextSlide;
num x0;
bool touched = false;
Carousel.init({this.withArrowKeyControl = false}) {
lastSlideIndex = slides.length - 1;
currentSlideIndex = -1;
// Remove container empty space when no images are available
if (lastSlideIndex == -1) {
container.classes.clear();
return;
}
// Skip carousel decoration when only one image is available
if (lastSlideIndex == 0) {
currentSlide = slides[currentSlideIndex + 1];
currentSlide.classes.add('active');
return;
}
_hideSlides();
_initBullets();
_initArrows();
_initGestureListener();
if (withArrowKeyControl) {
_initArrowKeyControl();
@@ -71,6 +88,30 @@ class Carousel {
container.append(nextArrow);
}
void _touchStartListener(TouchEvent e) {
x0 = e.changedTouches.first.client.x;
touched = true;
}
void _touchEndListener(TouchEvent e) {
if (touched) {
int dx = e.changedTouches.first.client.x - x0;
// dx==0 case is ignored
if (dx > 0 && currentSlideIndex > 0) {
_slideLeft();
} else if (dx < 0 && currentSlideIndex < lastSlideIndex) {
_slideRight();
}
touched = false;
}
}
void _initGestureListener() {
container.onTouchStart.listen(_touchStartListener);
container.onTouchEnd.listen(_touchEndListener);
}
void _updateBullets() {
final bullets =
querySelector('.bullet-container').querySelectorAll('.bullet');