• phangkosal.oait99@gmail.com
flutter_carousel_slider

Flutter carousel slider using carousel_slider package step by step

This article we will use flutter carousel_slider package to create amazing carousel slider with easy step.

flutter carousel slider

Source Code

  1. pubspec.yaml
environment:
  sdk: ">=2.16.2 <3.0.0"

dependencies:
  flutter:
    sdk: flutter

  cupertino_icons: ^1.0.2
  carousel_slider: ^4.1.1
  get:

2. SliderPage3.dart

import 'package:carousel_slider/carousel_slider.dart';
import 'package:flutter/material.dart';
final List<String> imgList = [
  'https://d2dgtayfmpkokn.cloudfront.net/wp-content/uploads/sites/461/2021/03/19103450/Flash-Sale-Mar-2021-Website-Banner-v3.png',
  'https://avdgraph.com/wp-content/uploads/car-rental-banner.jpg',
  'https://graphicsfamily.com/wp-content/uploads/edd/2021/07/Car-Dealer-or-Showroom-Editable-Banner-Design-Template-1180x664.jpg',
  'https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcQ0PeCRpARYSmM1BRkzN4yhYocaEQRKLKVJvw&usqp=CAU',
];
class SliderPage3 extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    return _SliderPage3();
  }
}

class _SliderPage3 extends State<SliderPage3> {
  int _current = 0;
  final CarouselController _controller = CarouselController();
  final List<Widget> imageSliders = imgList
      .map((item) => Container(
    child: Container(
      margin: EdgeInsets.all(5.0),
      child: ClipRRect(
          borderRadius: BorderRadius.all(Radius.circular(5.0)),
          child: Stack(
            children: <Widget>[
              Image.network(item, fit: BoxFit.cover, width: 1000.0),
              Positioned(
                bottom: 0.0,
                left: 0.0,
                right: 0.0,
                child: Container(
                  decoration: BoxDecoration(
                    gradient: LinearGradient(
                      colors: [
                        Color.fromARGB(200, 0, 0, 0),
                        Color.fromARGB(0, 0, 0, 0)
                      ],
                      begin: Alignment.bottomCenter,
                      end: Alignment.topCenter,
                    ),
                  ),
                  padding: EdgeInsets.symmetric(
                      vertical: 10.0, horizontal: 20.0),
                  child: Text(
                    'No. ${imgList.indexOf(item)} image',
                    style: TextStyle(
                      color: Colors.white,
                      fontSize: 20.0,
                      fontWeight: FontWeight.bold,
                    ),
                  ),
                ),
              ),
            ],
          )),
    ),
  ))
      .toList();
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: Text('Flutter Carousel')),
      body: Column(children: [
        Container(
          child: CarouselSlider(
            items: imageSliders,
            carouselController: _controller,
            options: CarouselOptions(
                autoPlay: true,
                aspectRatio: 2.0,
                onPageChanged: (index, reason) {
                  setState(() {
                    _current = index;
                  });
                }),
          ),
        ),
        Row(
          mainAxisAlignment: MainAxisAlignment.center,
          children: imgList.asMap().entries.map((entry) {
            return GestureDetector(
              onTap: () => _controller.animateToPage(entry.key),
              child: Container(
                width: 12.0,
                height: 12.0,
                margin: EdgeInsets.symmetric(vertical: 8.0, horizontal: 4.0),
                decoration: BoxDecoration(
                    shape: BoxShape.circle,
                    color: (Theme.of(context).brightness == Brightness.dark
                        ? Colors.white
                        : Colors.black)
                        .withOpacity(_current == entry.key ? 0.9 : 0.4)),
              ),
            );
          }).toList(),
        ),
        CarouselSlider(
          items: imageSliders,
          carouselController: _controller,
          options: CarouselOptions(
              autoPlay: true,
              aspectRatio: 2.0,
              onPageChanged: (index, reason) {
                setState(() {
                  _current = index;
                });
              }),
        ),
      ]),
    );
  }
}
Spread the love

0 thoughts on “Flutter carousel slider using carousel_slider package step by step

  1. I was recommended this blog through my cousin. I’m not certain whether this publish is written by him as nobody else recognise such targeted approximately my trouble. You are incredible! Thank you!

Leave a Reply

Your email address will not be published. Required fields are marked *