Flutter, a popular open-source UI toolkit from Google, provides a wide range of widgets to create complex UIs for your applications. Among these widgets, the NestedScrollView class plays a crucial role in creating scrollable layouts where multiple scrolling views act as one coherent scroll view.
This feature is particularly useful when you have an app bar followed by a tab bar with multiple tabs, each representing different pages. These pages can be swiped horizontally, and each page might have its own scrollable view. The Flutter NestedScrollView widget helps us achieve this effect with ease, adhering to the Material Design specifications.
In this blog post, we will delve into the basics of the NestedScrollView class, its structure, and how to implement it in your Flutter applications. We will also explore how to use it in conjunction with other Flutter widgets like ListView and CustomScrollView. Furthermore, we'll discuss the role of ScrollController in NestedScrollView and how to implement it. Lastly, we will explore the SliverOverlapAbsorber and SliverOverlapInjector widgets and their role in NestedScrollView.
NestedScrollView is a class in Flutter that enables multiple scrolling views to act as one coherent scroll view. This widget is particularly useful when you want to create a scrolling view inside another scrolling view, hence the use of the term "nested".
The most common use case for NestedScrollView is when you have an app bar followed by a tab bar with multiple tabs, each representing different pages. These pages can be swiped horizontally, and each page might have its own scrollable view.
In such scenarios, the scroll positions of the outer scroll view and the inner scroll view are intrinsically linked. When the user scrolls up, the app bar scrolls off the screen, and the tab bar sticks to the top. As the user continues to scroll, the inner scroll view starts to scroll. When the user scrolls down, the opposite happens: the inner scroll view scrolls until it hits the top, then the app bar starts coming back.
This overlapping behavior of scroll views is according to the Material Design spec by Google. Significantly, the NestedScrollView widget helps us achieve this effect with ease.
The NestedScrollView widget takes a required headerSliverBuilder property, which is expected to return a list of widgets that will be slivered and placed in the outer scroll view. The body of the NestedScrollView typically contains the tab views, which are the inner scrolling views.
In the above example, we have a floating app bar with a tab bar in the headerSliverBuilder. The body contains a TabBarView with multiple tabs. Each tab has its own CustomScrollView, which is the inner scrolling view. The SliverOverlapInjector widget is used to synchronize the scroll positions of the outer and inner scroll views.
The headerSliverBuilder property takes a callback function that provides the current BuildContext and a boolean indicating whether the inner box is scrolled. This callback is expected to return a list of slivers, which are the widgets that will be hosted in the outer scroll view.
In the body of the NestedScrollView, we create a TabBarView that contains the tab views. Each tab view is a separate scrolling view inside the NestedScrollView. The SliverOverlapInjector is used to handle the overlapping of the outer and inner scroll views, ensuring they act as one coherent scroll view.
To start using the NestedScrollView widget in your Flutter application, you need to have Flutter SDK installed and set up in your development environment. If you're new to Flutter, you can follow the official Flutter installation guide.
Once you have Flutter set up, you can start creating a new Flutter project or open an existing one. In your project, make sure to import the flutter/material.dart package, which provides the NestedScrollView widget along with many other essential widgets.
Let's create a simple example where we have a floating app bar and a list that scrolls vertically. When the list scrolls, the app bar should scroll off the screen, and when the list reaches the top, the app bar should start coming back.
In this example, we have a NestedScrollView with a SliverAppBar in the headerSliverBuilder. The body of the NestedScrollView contains a ListView.builder that creates a list of ListTile widgets. The floating and pinned properties of the SliverAppBar are set to true, which means the app bar can scroll off the screen and can remain visible at the start of the scroll view.
This example demonstrates how to create a simple NestedScrollView with a floating app bar and a scrolling view inside. When the list scrolls, the app bar scrolls off the screen, creating a seamless user experience.
The headerSliverBuilder is a key property of the NestedScrollView widget. It is a function that should return a list of widgets that will be slivered and placed in the outer scroll view. The headerSliverBuilder takes two parameters: a BuildContext and a boolean value indicating whether the inner box is scrolled.
The BuildContext provides information about the location of a widget in the widget tree, and it can be used to access data that can influence how widgets are built. The boolean value innerBoxIsScrolled indicates whether the inner scroll view or the outer scroll view should react to the user's scroll.
Let's look at a simple example of using headerSliverBuilder in a NestedScrollView.
In this example, the headerSliverBuilder returns a SliverAppBar with a TabBar at the bottom. The TabBar contains ten tabs, each represented by a Tab widget. The forceElevated property of the SliverAppBar is set to the value of innerBoxIsScrolled, which means the app bar will show an elevation shadow when the inner scroll view is scrolled.
NestedScrollView can be combined with ListView to create a coherent scroll view where the app bar and the list scroll together. This is useful when you want to create a screen where the app bar scrolls off the screen as the user scrolls through a list.
Here's a simple example of using NestedScrollView with ListView:
In this example, the NestedScrollView contains a SliverAppBar in the headerSliverBuilder and a ListView.builder in the body. The ListView.builder creates a list of ListTile widgets. As the list scrolls, the app bar scrolls off the screen, creating a seamless user experience.
NestedScrollView can also be used with CustomScrollView to create more complex scrollable layouts. CustomScrollView allows you to create a scroll view with slivers, which are portions of a scrollable area that can change their layout.
Here's a simple example of using NestedScrollView with CustomScrollView:
In this example, the NestedScrollView contains a SliverAppBar in the headerSliverBuilder and a CustomScrollView in the body. The CustomScrollView contains a SliverList, which creates a list of ListTile widgets. As the list scrolls, the app bar scrolls off the screen, creating a seamless user experience.
In Flutter, a ScrollController is used to control the position to which a scroll view scrolls. In the context of a NestedScrollView, the ScrollController can be used to control the scroll position of the outer scroll view or the inner scroll views.
The ScrollController is typically used when you want to animate the scroll position, or when you want to know when the scroll position changes, so you can implement some custom behavior.
It's important to note that in a NestedScrollView, the ScrollController you provide is used for the outer scroll view. The inner scroll views are controlled by their own separate ScrollControllers.
Let's look at a simple example where we use a ScrollController with a NestedScrollView.
In this example, we create a ScrollController and assign it to the controller property of the NestedScrollView. This allows us to control the scroll position of the outer scroll view. The ListView.builder in the body of the NestedScrollView has its own ScrollController.
In this blog post, we've explored the NestedScrollView class in Flutter, a powerful tool for creating complex scrolling views. We've learned about its basic structure, how to implement it, and how to combine it with other Flutter widgets like ListView and CustomScrollView. We've also delved into the role of the ScrollController and how it's used in NestedScrollView, along with the SliverOverlapAbsorber and SliverOverlapInjector widgets.
Understanding and effectively using the NestedScrollView class can significantly enhance the user experience of your Flutter applications, especially when dealing with multiple scrolling views.
We hope this guide has provided you with valuable insights and practical knowledge about the NestedScrollView class.