Using PageStorage and Keeping offset of Scrollable Widgets in flutter

TheBlacktaler
3 min readSep 29, 2021

So Now I’m going to show how to use PageStorage

and I also did a lot of mistakes until I learned how to use it

If you wish to use it without any mistakes follow my Examples with attention

For a start We have to create Our class

So PageStorage is used to store offset of scrollableWidget for example: ListView,GridView,PageView and many others.

I am going to show two of the Widgets above.

Before that we should create some variables and functions to give to the PageStorage:

  1. create PageStorageKey and PageStorageBucket;

It should be created in each page detached if you want to use more than one scrollable widget For Example:

2. create variables and list for BottomNavigationBar(By The Way I will show BottomNavigationBar too:).

3.We will get instance with initState to increase performance from Classes below

P/S: if you want to use more than one scrollable widgets in one page

you do not need to give key to the page as I did below

here is my ListClass:

The SecondClass is also the same with this, only it’s named ListClass2.

I gave key to each Scrollable widget separatedly

because there are 2 Scrollable Widgets

and if I do not give keys one by one Scroll offset of both Widgets will be stored the same like this:

So now just give pages with _activeIndex to the body

body: pages[_activeIndex],

the _activeIndex is used in BottomNavigationBar and You can find bottomNavigationBar property in Scaffold before or after the appBar/body:

As you are seeing it’s quite easy to make BottomNavigationBar(it has many other features too, If you don’t understand it well there are a lot of posts about it.)

So Now It should 100% work or if you use 2.5.1 or later version of Flutter it has a little difference and in that case it should work 99% 😜

Here is my full Code Source

Happy Coding

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

TheBlacktaler
TheBlacktaler

Written by TheBlacktaler

I'm going to publish more posts. In real life I'm a happy, and simultaneously serious person and I'm a junior professional) :

Responses (1)

Write a response

Excellent article on how to keep scrollable state for widgets. One enhancement for the article would be to post code in text format instead of images to help make it more legible, faster loading, and save people time with copy and paste to test out your idea.