Featured Image
Software Development

Create an Onboarding Page Indicator in 3 Minutes in Flutter

Onboarding is the process of introducing your app to new users and guiding them through its features and benefits. This is a very important feature when you have a bunch of cool features in your app. It’s a way of helping your users get more familiar with the app and encouraging them to use it regularly.

A common method to implement onboarding is to use a series of pages with some text and images that explain the main functionality of your app. To make the experience more engaging and interactive, you can also add a page indicator that shows the current page and the number of pages in the onboarding sequence. In this blog post, we will show you how to create a simple and elegant onboarding page indicator in Flutter in just 3 minutes.

Let’s see the final output first.

A screenshot of an onboarding page indicator

Pretty cool! Right? So let’s get started.

The UI Structure of the Onboarding Screen

A diagram of the flutter widgets structure of the onboarding page indicator

Stack(
  alignment: AlignmentDirectional.bottomCenter,
  children: <Widget>[
    PageView.builder(...),
    Stack(...),
    Visibility(...)
  ],
)

The Stack Widget is the best fit when we need to overlap one widget over other. So in our case, Stack has:

  • PageView.builder() to slide through different screens.
  • Stack() to contain a number of page indicators.
  • Visibility() to hide and show ‘Arrow’ button.

Now let’s see each widget in detail.

1) PageView.builder();

Creates horizontal/vertical slidable widget.

A screenshot of a PageView.builder widget

First of all, we will need to create a list of widgets. This list may contain different screens that will show up when sliding. It can be created like this

final List<Widget> introWidgetsList = <Widget>[
  Screen1(...),
  Screen2(...),
  Screen3(...),
];

We can also create one common widget/screen and change the images based on passed params. This is optional but good to have.

PageView.builder(
  physics: ClampingScrollPhysics(),
  itemCount: introWidgetsList.length,
  onPageChanged: (int page) {
    getChangedPageAndMoveBar(page);
  },
  controller: controller,
  itemBuilder: (context, index) {
    return introWidgetsList[index];
  },
)

Now we can provide the list details to itemCount and itemBuilder property of PageView.builder. onPageChanged property is used to notify when pages change so that we can show the appropriate page indicator.

2) Stack();

I have used stack but you can use any widget to show page indicators.

Using stack widget to show page indicators with Animation

Stack(
  alignment: AlignmentDirectional.topStart,
  children: <Widget>[
    Container(
      margin: EdgeInsets.only(bottom: 35),
      child: Row(
        mainAxisSize: MainAxisSize.min,
        mainAxisAlignment: MainAxisAlignment.center,
        children: <Widget>[
          for (int i = 0; i < introWidgetsList.length; i++)
            if (i == currentPageValue) ...[circleBar(true)] else
              circleBar(false),
        ],
      ),
    ),
  ],
)

Stack further contains the Row() that aligns the page indicators besides each other and circleBar(true/false) is the actual widget that shows Page indicator or Dot. Using Dart’s 2.3 new feature Collection for and Collection If we can show a number of circleBar() as the number of screens and pass True or False to notify whether isActive.

Let’s have a deeper look inside it.

Using AnimatedContainer widget to show Animation

Widget circleBar(bool isActive) {
  return AnimatedContainer(
    duration: Duration(milliseconds: 150),
    margin: EdgeInsets.symmetric(horizontal: 8),
    height: isActive ? 12 : 8,
    width: isActive ? 12 : 8,
    decoration: BoxDecoration(
        color: isActive ? kOrange : klightGrey,
        borderRadius: BorderRadius.all(Radius.circular(12))),
  );
}

As you can see it’s just AnimatedContainer that changes its height and width and color based on the isActive parameter.

3) Visibility;

This widget controls the visibility of its child, and in our case its Floating action button.

floating action button on the last onboarding screen

Visibility(
  visible: currentPageValue == introWidgetsList.length - 1
      ? true
      : false,
  child: FloatingActionButton(
    onPressed: () {
    },
    shape: BeveledRectangleBorder(
        borderRadius: BorderRadius.all(Radius.circular(26))),
    child: Icon(Icons.arrow_forward),
  ),
)

FloatingActionButton gets visible only when the last screen is showing up.

Finally here comes the magic.

Here is the implementation for the getChangedPageAndMoveBar that we are calling from onPageChange of PageView.

void getChangedPageAndMoveBar(int page) {
  currentPageValue = page;
  setState(() {});
}

Setting currentPageValue as of currently displayed screen on PageView and setState(() {}); will do rest of the magic. I would suggest you please try it from here.

That’s it.

We hope this blog post has helped you learn how to create an onboarding page indicator quickly and easily. If you want to learn more about our Flutter development services or need help with your projects, feel free to contact us at Aubergine Solutions. We are a team of skilled and passionate developers who can help you build beautiful and responsive apps.

If you enjoyed this post, don’t miss our other amazing blogs:
How to Build a Superhero Interaction App in Flutter with Awesome Animations

How to Create a Text Reader Animation in Flutter in Easy Steps

author
Pinkesh Darji
I love to solve problems using technology that improves user’s life on a major scale. Over the last several years, I have been developing and leading various mobile apps in different areas. More than just programming, I love to write technical articles. I have written many high-quality technical articles. I have worked with various startups to build their dream app. I have been involved in product development since my early days and know insights into it. I have provided my valuable input while taking some crucial decisions of the app by brainstorming with a design, QA team. Over the last 3 years, I have been developing mobile apps and libraries using Google’s Flutter framework. I mentor junior Flutter developers and review their code. You will also find me talking on various Flutter topics in local meetups.