@@ -891,3 +891,124 @@ class SnackBarPage extends StatelessWidget {
891891 }
892892}
893893```
894+ ## Carousel
895+ ``` dart
896+ import 'package:flutter/material.dart';
897+
898+ void main() => runApp(MyApp());
899+
900+ class MyApp extends StatelessWidget {
901+ @override
902+ Widget build(BuildContext context) {
903+ return MaterialApp(
904+ title: 'Material App',
905+ debugShowCheckedModeBanner: false,
906+ home: Scaffold(
907+ appBar: AppBar(
908+ title: Text('Image carousel'),
909+ ),
910+ body: Carousel(),
911+ ),
912+ );
913+ }
914+ }
915+
916+ class Carousel extends StatefulWidget {
917+ const Carousel({
918+ Key? key,
919+ }) : super(key: key);
920+
921+ @override
922+ State<Carousel> createState() => _CarouselState();
923+ }
924+
925+ class _CarouselState extends State<Carousel> {
926+ late PageController _pageController;
927+
928+ List<String> images = [
929+ "https://images.wallpapersden.com/image/download/purple-sunrise-4k-vaporwave_bGplZmiUmZqaraWkpJRmbmdlrWZlbWU.jpg",
930+ "https://wallpaperaccess.com/full/2637581.jpg",
931+ "https://uhdwallpapers.org/uploads/converted/20/01/14/the-mandalorian-5k-1920x1080_477555-mm-90.jpg"
932+ ];
933+
934+ int activePage = 1;
935+
936+ @override
937+ void initState() {
938+ super.initState();
939+ _pageController = PageController(viewportFraction: 0.8, initialPage: 1);
940+ }
941+
942+ @override
943+ Widget build(BuildContext context) {
944+ return Column(
945+ children: [
946+ SizedBox(
947+ width: MediaQuery.of(context).size.width,
948+ height: 200,
949+ child: PageView.builder(
950+ itemCount: images.length,
951+ pageSnapping: true,
952+ controller: _pageController,
953+ onPageChanged: (page) {
954+ setState(() {
955+ activePage = page;
956+ });
957+ },
958+ itemBuilder: (context, pagePosition) {
959+ bool active = pagePosition == activePage;
960+ return slider(images,pagePosition,active);
961+ }),
962+ ),
963+ Row(
964+ mainAxisAlignment: MainAxisAlignment.center,
965+ children: indicators(images.length,activePage))
966+ ],
967+ );
968+ }
969+ }
970+
971+ AnimatedContainer slider(images, pagePosition, active) {
972+ double margin = active ? 10 : 20;
973+
974+ return AnimatedContainer(
975+ duration: Duration(milliseconds: 500),
976+ curve: Curves.easeInOutCubic,
977+ margin: EdgeInsets.all(margin),
978+ decoration: BoxDecoration(
979+ image: DecorationImage(image: NetworkImage(images[pagePosition]))),
980+ );
981+ }
982+
983+ imageAnimation(PageController animation, images, pagePosition) {
984+ return AnimatedBuilder(
985+ animation: animation,
986+ builder: (context, widget) {
987+ print(pagePosition);
988+
989+ return SizedBox(
990+ width: 200,
991+ height: 200,
992+ child: widget,
993+ );
994+ },
995+ child: Container(
996+ margin: EdgeInsets.all(10),
997+ child: Image.network(images[pagePosition]),
998+ ),
999+ );
1000+ }
1001+
1002+ List<Widget> indicators(imagesLength, currentIndex) {
1003+ return List<Widget>.generate(imagesLength, (index) {
1004+ return Container(
1005+ margin: EdgeInsets.all(3),
1006+ width: 10,
1007+ height: 10,
1008+ decoration: BoxDecoration(
1009+ color: currentIndex == index ? Colors.black : Colors.black26,
1010+ shape: BoxShape.circle),
1011+ );
1012+ });
1013+ }
1014+ ```
0 commit comments