Created
December 28, 2018 13:14
-
-
Save miguelpruivo/a99bae919703d3ad6099f04579c84967 to your computer and use it in GitHub Desktop.
A carousel widget
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Carousel extends StatefulWidget { | |
_CarouselState createState() => _CarouselState(); | |
} | |
class _CarouselState extends State<Carousel> with SingleTickerProviderStateMixin { | |
final PageController controller = PageController(); | |
AnimationController animationController; | |
List<Widget> list = [ | |
SliderBox( | |
child: FlutterLogo( | |
colors: Colors.red, | |
)), | |
SliderBox( | |
child: FlutterLogo( | |
colors: Colors.green, | |
)), | |
SliderBox( | |
child: FlutterLogo( | |
colors: Colors.blue, | |
)) | |
]; | |
@override | |
void initState() { | |
super.initState(); | |
WidgetsBinding.instance.addPostFrameCallback((_) => _animateSlider()); | |
} | |
void _animateSlider() { | |
Future.delayed(Duration(seconds: 2)).then((_) { | |
int nextPage = controller.page.round() + 1; | |
if (nextPage == list.length) { | |
nextPage = 0; | |
} | |
controller | |
.animateToPage(nextPage, duration: Duration(seconds: 1), curve: Curves.linear) | |
.then((_) => _animateSlider()); | |
}); | |
} | |
@override | |
Widget build(BuildContext context) { | |
PageIndicatorContainer container = new PageIndicatorContainer( | |
pageView: new PageView( | |
children: list, | |
controller: controller, | |
), | |
length: list.length, | |
padding: EdgeInsets.fromLTRB(10, 40, 10, 10), | |
indicatorSpace: 10, | |
indicatorColor: Colors.grey[350], | |
indicatorSelectorColor: Colors.grey, | |
); | |
return Stack( | |
children: <Widget>[ | |
Container(color: Colors.grey[100], height: double.infinity), | |
Container(color: Colors.white, child: container, margin: EdgeInsets.only(bottom: 50)), | |
], | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment