What is Scrollable in flutter

When a widget scrolls, we call it Scrollable. The StatefulWidget is the direct ancestor of a Scrollable class. Therefore when we scroll down or up, as location changes, state of position also changes.

Although on the whole we use the Scrollable class to implement the interaction model for a scrollable widget, but it’s rare to construct a Scrollable directly.

Actually, there are reasons for that.

A Scrollable class doesn’t have an opinion about how the children will be displayed using the viewport.

Therefore, as a result, we use different scrollable widgets, such as ListView, or GridView that are built for different purposes.

To tell the truth, a Scrollable class does have an interaction model including gesture recognition that includes different type of semantic actions, such as tap, drag, and scale. Consequently they also involve state.

But, a Scrollable class doesn’t have any idea about the viewport that actually displays the children.

What is Viewport?

As the name suggests, we cannot view the materials without viewport. Most importantly, we don’t have an idea on what is going to be displayed.

The scrolling machinery would stop if viewport didn’t do the hard work. Since viewport widget toils hard, a subset of its children is being displayed.

Consider the following screenshot, where we use CustomScrollView.

CustomScrollView with collapsing toolbar
CustomScrollView with collapsing toolbar
https://sanjibsinha.com/customscrollview-flutter/

The viewport widget manages the dimension and the given offset. The offset is nothing but a property that decides which part of the content inside the viewport should be visible.

How does it happen?

Here the ViewportOffset.pixels plays the important role, determining the scroll offset. Using this scroll offset viewport selects which part of its content to display.

Because we know that as the user scrolls the viewport, the value changes, and the changing value again changes the content, which is displayed.

It also happens in case of other widgets that combines Scrollable and Viewport into widgets that are easier to use. Consider the case of PageView.

PageView in flutter page one
PageView in flutter page one
https://sanjibsinha.com/pageview-flutter/

At the same time, Viewport widget also hosts a list of Slivers.

You may have interest in reading a few articles on Slivers as well; because viewport cannot contain box children directly it uses different type of slivers.

https://sanjibsinha.com/slivergrid-flutter/
https://sanjibsinha.com/silver-app-bar-flutter/
https://sanjibsinha.com/sliverpersistentheader-flutter/
https://sanjibsinha.com/slivers-flutter/

Why don’t we construct Scrollable directly?

The reason is simple. We treat a Scrollable as an implementation model.

Subsequently, we choose ListView or GridView that combines many facets at the same time. It scrolls, uses viewport, and a layout model.

On the other hand, the CustomScrollView combines layout models.

By the way, you may want to take a look at the other Scrolling Widgets, before we close down further reading on this topic. We have a list below.

What is ListView Flutter? How do I use ListView in Flutter?

How do I use SingleChildScrollView in flutter?

What is GridView and how do you centre a GridView item in Flutter?

What is GridView builder in Flutter?

What is the grid view in Flutter?

What is GridView count in flutter?

What is GridView.extent in Flutter?

How do I make my collapsing toolbar flutter?

What is SliverGrid in flutter?

How to use CustomScrollView in Flutter?

How to use NestedScrollView in flutter?

How to use PageView in Flutter

What is PageView builder in flutter?

What is PageView custom in flutter?

How to use DraggableScrollableSheet

Summing it up, without the Scrollable, in most cases, we cannot think of building a flutter app that perform well. We always expect that a user will choose a product out of many other products. And in a static page that doesn’t scroll or interact with gestures, we cannot provide such rich experiences. Besides scrolling, gesture also plays a key role.

https://sanjibsinha.com/difference-between-inkwell-gesturedetector/

What Next?

Books at Leanpub

Books in Apress

My books at Amazon

GitHub repository

Technical blog

Twitter

Comments

4 responses to “What is Scrollable in flutter”

  1. […] ListView is a widget that arranges a scrollable list of widgets linearly. Moreover, we can construct ListView using four ways. We’ll see each construction […]

  2. […] already learned that a ListView is a widget that arranges a scrollable list of widgets linearly. Moreover, we can construct ListView using four ways. We’ve seen two ListView […]

  3. […] ListView is a widget that arranges a scrollable list of widgets linearly. Moreover, we can construct ListView using four ways. We’ll see each construction one by […]

  4. […] already learned that a ListView is a widget that arranges a scrollable list of widgets linearly. Moreover, we can construct ListView using four ways. We’ve seen two ListView […]

Leave a Reply