DEV Community

Cover image for How to implement pageview in Flutter.
Samuel Adekunle
Samuel Adekunle

Posted on

How to implement pageview in Flutter.

Hello everyone and welcome to a brand new tutorial on Flutter. Today, we’re going to learn how to implement a customized PageView in our Flutter app.

If you want to know more about some cogent flutter Tricks and Tips and How to achieve tasks easily in Flutter, consider following me and sign up for the newsletter so you don’t miss any updates and also subscribe to my YouTube Channel. Thanks

Let's get started

If you haven’t already seen the Flutter Widget of the Week video about how to implement PageView in Flutter, kindly watch it first. The rest of this article is based on it.

This video showed you the essence of what PageView does. This article will fill out the code for you to play with. Scroll down to the end to get the full code demo.

A PageView widget allows the user to swipe/transition between different screens in your app. All you need to set it up are a PageViewController and a PageView.

You can use PageController to control which page is visible in the view. In addition to being able to control the pixel offset of the content inside the PageView, PageController lets you control the offset in terms of pages, which are increments of the viewport size.

PageController can also be used to control the PageController.initialPage, which determines which page is shown when the PageView is first constructed, and the PageController.viewportFraction, which determines the size of the pages as a fraction of the viewport size.

Exploring PageView

A PageView is a widget that generates scrollable pages on the screen. This can either be a fixed list of pages or a builder function that builds repeating pages. PageView acts similarly to a Listview in the sense of constructing elements.

Three types/Constructor of PageView

we have three types of PageView just like ListView.

  1. PageView
  2. PageView.builder
  3. PageView.custom

PageView (Default constructor)

This type takes a fixed list of children (pages) and makes them scrollable.



       PageView(
          children: <Widget>[
            Container(
              color: Colors.orange,
              child: Center(
                  child: Text(
                'Page 1',
                style: TextStyle(color: Colors.white),
              )),
            ),
            Container(
              color: Colors.blue,
              child: Center(
                  child: Text(
                'Page 2',
                style: TextStyle(color: Colors.white),
              )),
            ),
            Container(
              color: Colors.green,
              child: Center(
                  child: Text(
                'Page 3',
                style: TextStyle(color: Colors.white),
              )),
            ),
          ],
        ),


Enter fullscreen mode Exit fullscreen mode

The above code produces the below screen 👇

image

PageView.builder

This constructor takes an item builder function and an itemCount similar to ListView.builder.



PageView.builder(
  itemBuilder: (context, index) {
    return _buildPage();
  },
  itemCount: listItemCount, // Can be null
)


Enter fullscreen mode Exit fullscreen mode

Like a ListView.builder, this builds children on demand. If the itemCount is set to null (not set), an infinite list of pages can be generated.

Example: The below code produce the below screen 👇



      PageView.builder(
        // itemCount: 3,
        itemBuilder: (context, index) {
        return Container(
          color: index % 2 == 0 ? Colors.green : Colors.blue[700],
        );
      }),


Enter fullscreen mode Exit fullscreen mode

Gives an infinite list of pages with alternating pink and cyan colors:

image

PageView.custom Constructor



PageView.custom(
   {Key key,
   Axis scrollDirection: Axis.horizontal,
   bool reverse: false,
   PageController controller,
   ScrollPhysics physics,
   bool pageSnapping: true,
   ValueChanged<int> onPageChanged,
   @required SliverChildDelegate childrenDelegate,
   DragStartBehavior dragStartBehavior: DragStartBehavior.start,
   bool allowImplicitScrolling: false,
   String restorationId,
   Clip clipBehavior: Clip.hardEdge}
)


Enter fullscreen mode Exit fullscreen mode

PageView.custom works the same way as ListView.custom. It takes a childrenDelegate function similar to ListView.builder.
PageView.custom is more explained with examples in the source code.

Customized PageView

image

And that's all the major stuff you need 👌, I hope you have learned something, kindly appreciate this article by sharing it, and feel free to ask a question. Thanks for reading

Full Source Code 👇 - Show some ❤️ by starring ⭐ the repo and do follow me 😄!

GitHub logo techwithsam / PageView-Flutter

How to implement PageView in Flutter.

pageview

A new Flutter project.

Getting Started

This project is a starting point for a Flutter application.

A few resources to get you started if this is your first Flutter project:

For help getting started with Flutter, view our online documentation, which offers tutorials, samples, guidance on mobile development, and a full API reference. "# PageView-Flutter"




I hope you have learned one thing or the other, kindly give this article much appreciation you want if you enjoy it, feel free to ask a question and leave a comment if you feel like it 🤭. Thanks for reading and see you in the next series.

🔗 Let's Connect 🔗 ==> Github | Twitter | Youtube | WhatsApp | LinkedIn | Patreon | Facebook.

Join the Flutter Dev Community 👨‍💻👨‍💻 ==> Facebook | Telegram | WhatsApp | Signal.

Subscribe to my Telegram channel | Youtube channel. Thanks

Happy Fluttering 🥰👨‍💻

Top comments (0)