-
-
Save master-stm/be49ad07b52526df6443c197bd51ecac to your computer and use it in GitHub Desktop.
This is a code snippet to elaborate how to use image carousel pro package with dot indicator. You can find the package here: https://pub.dartlang.org/packages/carousel_pro. Note: there are so many image carousel packages that you can try, I have chosen this because I dont want to separately add dot indicator by the time I'm creating this snippet.
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
//First go to pubspec.yaml file and add this => carousel_pro: ^0.0.1 under dependencies. | |
import 'package:carousel_pro/carousel_pro.dart'; | |
import 'package:flutter/material.dart'; | |
void main() => runApp(new MyApp()); | |
class MyApp extends StatelessWidget { | |
@override | |
Widget build(BuildContext context) { | |
return new MaterialApp( | |
debugShowCheckedModeBanner: false, | |
title: 'Flutter Carousel', | |
home: ImageCarousel(), | |
); | |
} | |
} | |
class ImageCarousel extends StatefulWidget { | |
_ImageCarouselState createState() => new _ImageCarouselState(); | |
} | |
class _ImageCarouselState extends State<ImageCarousel> with SingleTickerProviderStateMixin { | |
Animation<double> animation; | |
AnimationController controller; | |
initState() { | |
super.initState(); | |
controller = new AnimationController( | |
duration: const Duration(milliseconds: 2000), vsync: this); | |
animation = new Tween(begin: 0.0, end: 18.0).animate(controller) | |
..addListener(() { | |
setState(() { | |
// the state that has changed here is the animation object’s value | |
}); | |
}); | |
controller.forward(); | |
} | |
@override | |
Widget build(BuildContext context) { | |
double screenHeight = MediaQuery.of(context).size.height; | |
Widget carousel = new Carousel( | |
boxFit: BoxFit.cover, | |
images: [ | |
new AssetImage('assets/images/1.jpg'), | |
new AssetImage('assets/images/2.jpg'), | |
new AssetImage('assets/images/3.jpg'), | |
new AssetImage('assets/images/4.jpg'), | |
], | |
animationCurve: Curves.fastOutSlowIn, | |
animationDuration: Duration(seconds: 1), | |
); | |
Widget banner = new Padding( | |
padding: const EdgeInsets.only(top: 20.0, left: 20.0), | |
child: new Container( | |
decoration: BoxDecoration( | |
borderRadius: BorderRadius.only( | |
topLeft: Radius.circular(15.0), | |
bottomRight: Radius.circular(15.0)), | |
color: Colors.amber.withOpacity(0.5), | |
), | |
padding: const EdgeInsets.all(10.0), | |
child: new Text( | |
'Banner on top of carousel', | |
style: TextStyle( | |
fontFamily: 'fira', | |
fontSize: animation.value,//18.0, | |
//color: Colors.white, | |
), | |
), | |
), | |
// ), | |
// ), | |
); | |
return new Scaffold( | |
backgroundColor: Colors.black, | |
body: new Center( | |
child: new Container( | |
padding: const EdgeInsets.all(20.0), | |
height: screenHeight / 2, | |
child: new ClipRRect( | |
borderRadius: BorderRadius.circular(30.0), | |
child: new Stack( | |
children: [ | |
carousel, | |
banner, | |
], | |
), | |
), | |
), | |
), | |
); | |
} | |
dispose() { | |
controller.dispose(); | |
super.dispose(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment