Skip to content
Readerstacks logo Readerstacks
  • Home
  • Softwares
  • Angular
  • Php
  • Laravel
  • Flutter
Readerstacks logo
Readerstacks
Create a slider in flutter

How to create custom slider in flutter ?

Aman Jain, January 8, 2022January 15, 2022

In this tutorial i will show you to create you simple slider in flutter. Flutter has many inbuilt widgets to create beautiful UI and manage them with state and stateless widgets. There is many packages to create the sliders in flutter application but today here i am going to share siple logic to create slider using PageView in flutter

We will use PageView to create the Slider and Timer to automatically change the page. PageView accepts multiple parameter to change te direction or initial page etc. You can also read our last article for vertical scroll in flutter.

So in this article i will cover PageView to create create slider in flutter by step by step

Step 1 : Create flutter project

Very first step is to create the fluter application using command line tool or in Android studio.

flutter create example_app

This will create a new project with name my_app then go in to the folder.

Step 2 : Create a widgets for slider

Now, Create a widget to slide the content.

File : lib/SliderWidget.dart

import 'package:flutter/material.dart';

class SliderWidget extends StatelessWidget {
  final List<String> list = <String>['1', '2', '3', '4', '5'];
  final PageController pageController = PageController(initialPage: 0);

  SliderWidget({Key? key}) : super(key: key) {
    Timer.periodic(const Duration(seconds: 2), (timer) {
      int page = pageController.page!.ceil();
      if (list.length - 1 > page) {
        pageController.animateToPage(page + 1,
            duration: const Duration(seconds: 1), curve: Curves.ease);
      } else {
        pageController.animateToPage(0,
            duration: const Duration(seconds: 1), curve: Curves.ease);
      }
    });
  }

  List<Widget> buildContainers() {
    return list
        .map((e) => Container(
            decoration: const BoxDecoration(
              image: DecorationImage(
                  image: NetworkImage("https://picsum.photos/200/200"),
                  fit: BoxFit.fill),
            ),
            child: Container(
                alignment: Alignment.bottomLeft,
                margin: const EdgeInsets.only(bottom: 40, left: 20),
                child: Column(
                  mainAxisAlignment: MainAxisAlignment.end,
                  children: [
                    Text(" Name of user  " + e,
                        style: const TextStyle(
                            color: Colors.white, fontWeight: FontWeight.bold)),
                    Text(" subtitle  " + e,
                        style:const TextStyle(color: Colors.white))
                  ],
                ))))
        .toList();
  }

  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: Scaffold(
        appBar: AppBar(title: const Text("Readerstack")),
        body: Container(
          height: 200,
          child:
              PageView(controller: pageController, children: buildContainers()),
        ),
      ),
    );
  }
}

Here as you can see we have used PageView to build the slider view and also created a PageController to set initial page and TImer to slide it automatically.

Step 3 : Add Page to main file

Add you new pages to main.dart and start navigating.

import 'package:flutter/material.dart';
import 'package:example_app/SliderWidget.dart';

void main() => runApp(SliderWidget());

Step 4: Run the project

Simply run the project using command line or in android studio to check the implementation.

flutter run lib/main.dart

Live Preview:

How to change page in pageview in flutter ?

As you can see we have used Timer.periodic to run a timee and jumpToPage to animate the page with ease. jumpTOPage method change the current index of PageView as below

 Timer.periodic(const Duration(seconds: 2), (timer) {
      int page = pageController.page!.ceil();
      if (list.length - 1 > page) {
        pageController.animateToPage(page + 1,
            duration: const Duration(seconds: 1), curve: Curves.ease);
      } else {
        pageController.animateToPage(0,
            duration: const Duration(seconds: 1), curve: Curves.ease);
      }
    });

Related

Dart Flutter Flutterscrollslider

Post navigation

Previous post
Next post

Related Posts

Dart How to create a scroll view in flutter

How to create a scroll view in flutter ?

January 8, 2022November 5, 2023

Flutter provides multiple way to scroll the view and one of the most efficient way is ListView.builder. Using the ListView class we can configure multiple options. Using these widgets we can create horizontal as well vertical scroll. ListView GridView CustomScrollView DraggableScrollableSheet NestedScrollView PageView SingleChildScrollView Here are the few scroll widgets…

Read More
Flutter Display background image in Flutter

How to display background image in flutter ?

January 15, 2022August 29, 2022

In our recent article i showed you to display local or remote image in flutter application but in some cases we need to show our image in background so we can put additional content on it. Background image is useful to show when we wanted to show full image in…

Read More
Dart Navigating between pages in flutter

How to navigate between one page to another in flutter ?

January 4, 2022January 15, 2022

Flutter provides its inbuilt navigator to navigate between pages. In our application it must have multiple screens and we wanted to navigate on all screens so in flutter we use Navigator class to navigate between pages. In android we use Activity and in iOS we use ViewController to navigate between…

Read More

Aman Jain
Aman Jain

With years of hands-on experience in the realm of web and mobile development, they have honed their skills in various technologies, including Laravel, PHP CodeIgniter, mobile app development, web app development, Flutter, React, JavaScript, Angular, Devops and so much more. Their proficiency extends to building robust REST APIs, AWS Code scaling, and optimization, ensuring that your applications run seamlessly on the cloud.

Categories

  • Angular
  • CSS
  • Dart
  • Devops
  • Flutter
  • HTML
  • Javascript
  • jQuery
  • Laravel
  • Laravel 10
  • Laravel 11
  • Laravel 9
  • Mysql
  • Php
  • Softwares
  • Ubuntu
  • Uncategorized

Archives

  • July 2025
  • June 2025
  • May 2025
  • April 2025
  • October 2024
  • July 2024
  • February 2024
  • January 2024
  • December 2023
  • November 2023
  • October 2023
  • July 2023
  • March 2023
  • November 2022
  • October 2022
  • September 2022
  • August 2022
  • July 2022
  • June 2022
  • May 2022
  • April 2022
  • March 2022
  • February 2022
  • January 2022
  • December 2021
  • November 2021
  • October 2021
  • September 2021
  • August 2021
  • July 2021
  • June 2021

Recent Posts

  • Mapping Together: The Vibrant Spirit of OpenStreetMap Japan
  • Understanding High Vulnerabilities: A Deep Dive into the Weekly Summary
  • Building a Million-Dollar Brand: The Journey of Justin Jackson
  • Mastering Schedule Management with Laravel Zap
  • The Resilience of Nature: How Forests Recover After Fires
©2023 Readerstacks | Design and Developed by Readerstacks
Go to mobile version