Flutter - Nested Scroll View
Last Updated :
24 Apr, 2025
In Flutter, you can use a NestedScrollView widget to create a scrollable view with multiple scrolling sections that can scroll independently of each other. This is commonly used when you have a header that should remain visible while the content below it scrolls. In this article, we are going to implement the NestedScrollView widget. A sample video is given below to get an idea about what we are going to do in this article.
Basic Syntax of NestedScrollView Widget
NestedScrollView(
headerSliverBuilder: (BuildContext context, bool innerBoxIsScrolled) {
// SliverAppBar and/or other sliver widgets go here
return <Widget>[
// List of Sliver widgets
];
},
body: ListView(
// Body content goes here
),
)
Required Tools
To build this app, you need the following items installed on your machine:
- Visual Studio Code / Android Studio
- Android Emulator / iOS Simulator / Physical Device device.
- Flutter Installed
- Flutter plugin for VS Code / Android Studio.
Step By Step Implementation
Step 1: Create a New Project in Android Studio
To set up Flutter Development on Android Studio please refer to Android Studio Setup for Flutter Development, and then create a new project in Android Studio please refer to Creating a Simple Application in Flutter.
Step 2: Import the Package
First of all import material.dart file.
import 'package:flutter/material.dart';
Step 3: Execute the main Method
Here the execution of our app starts.
Dart
void main() {
runApp(MyApp());
}
Step 4: Create MyApp Class
In this class we are going to implement the MaterialApp , here we are also set the Theme of our App.
Dart
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
theme: ThemeData(
primarySwatch: Colors.green, // Set the app's primary theme color
),
debugShowCheckedModeBanner: false,
home: MyNestedScrollView(),
);
}
}
Step 5: Create MyNestedScrollView Class
In this class we are going to Implement the NestedScrollView widget that help to create scrollable view with a sticky header that remains visible while you scroll through the list of items. Comments are added for better understanding.
NestedScrollView(
// The headerSliverBuilder callback defines the sliver widgets in the header
headerSliverBuilder: (BuildContext context, bool innerBoxIsScrolled) {
return <Widget>[
// SliverAppBar is the header that remains visible while scrolling
SliverAppBar(
expandedHeight: 200.0,
floating: false,
pinned: true,
flexibleSpace: FlexibleSpaceBar(
title: Text(
'Nested Scroll View Example',
style: TextStyle(fontSize: 16), // Customize the title's style
),
background: Image.network(
'https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcSf9fG0XSXlw5HHtdVIhc1_gl4vzcKeCOAkoBD07BHiTAyvsVoKqvRbLkwuNSTheOd3Kk4&usqp=CAU',
fit: BoxFit.cover, // Ensure the image covers the entire area
),
),
),
];
},
// The body contains the scrollable content
body: ListView.builder(
itemCount: 20,
itemBuilder: (BuildContext context, int index) {
return ListTile(
title: Text('Item $index'), // Display a list of items
);
},
),
),
Dart
class MyNestedScrollView extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
body: NestedScrollView(
// The headerSliverBuilder callback defines the sliver widgets in the header
headerSliverBuilder: (BuildContext context, bool innerBoxIsScrolled) {
return <Widget>[
// SliverAppBar is the header that remains visible while scrolling
SliverAppBar(
expandedHeight: 200.0,
floating: false,
pinned: true,
flexibleSpace: FlexibleSpaceBar(
title: Text(
'Nested Scroll View Example',
style: TextStyle(fontSize: 16), // Customize the title's style
),
background: Image.network(
'https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcSf9fG0XSXlw5HHtdVIhc1_gl4vzcKeCOAkoBD07BHiTAyvsVoKqvRbLkwuNSTheOd3Kk4&usqp=CAU',
fit: BoxFit.cover, // Ensure the image covers the entire area
),
),
),
];
},
// The body contains the scrollable content
body: ListView.builder(
itemCount: 20,
itemBuilder: (BuildContext context, int index) {
return ListTile(
title: Text('Item $index'), // Display a list of items
);
},
),
),
);
}
}
Here is the full Code of main.dart file
Dart
import 'package:flutter/material.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
theme: ThemeData(
primarySwatch: Colors.green, // Set the app's primary theme color
),
debugShowCheckedModeBanner: false,
home: MyNestedScrollView(),
);
}
}
class MyNestedScrollView extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
body: NestedScrollView(
// The headerSliverBuilder callback defines the sliver widgets in the header
headerSliverBuilder: (BuildContext context, bool innerBoxIsScrolled) {
return <Widget>[
// SliverAppBar is the header that remains visible while scrolling
SliverAppBar(
expandedHeight: 200.0,
floating: false,
pinned: true,
flexibleSpace: FlexibleSpaceBar(
title: Text(
'Nested Scroll View Example',
style: TextStyle(fontSize: 16), // Customize the title's style
),
background: Image.network(
'https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcSf9fG0XSXlw5HHtdVIhc1_gl4vzcKeCOAkoBD07BHiTAyvsVoKqvRbLkwuNSTheOd3Kk4&usqp=CAU',
fit: BoxFit.cover, // Ensure the image covers the entire area
),
),
),
];
},
// The body contains the scrollable content
body: ListView.builder(
itemCount: 20,
itemBuilder: (BuildContext context, int index) {
return ListTile(
title: Text('Item $index'), // Display a list of items
);
},
),
),
);
}
}
Output:
Similar Reads
Flutter - Custom Scroll View
A CustomScrollView in Flutter is a highly customizable scrolling widget that allows you to create complex scrolling effects and layouts. You can use it to create scrollable views with multiple slivers, each having its own behavior. In this article, we are going to implement the CustomScrollView widg
4 min read
Flutter - Scroll Snap List
The scroll_snap_list package provides a wrapper that wraps around the ListView.builder widget to enable snapping event on List items. It can be modified to horizontal or vertical snapping based on the requirements. It is also important to note that it doesn't make use of less or no animation. In thi
4 min read
ListWheelScrollView Widget in Flutter
ListWheelScrollView is a flutter widget that is used to build ListView with a 3D effect. We can also use ListView to create a list of items but we can't add a 3D effect to it plus it also comes with a constraint that all the children inside this widget must be of the same size along the strolling ax
7 min read
Flutter - Animated ScrollView
Creating an animated scroll view in Flutter involves using a combination of widgets and animation controllers to achieve the desired scrolling effect. Here we use Tween Animation to implement animation. In this article we are going to add an animation effect to a ScrollView A sample video is given b
5 min read
Flutter - Scrollable Text
In this article, we are going to make a Flutter application in which we will add a Text Widget that can be scrolled horizontally or vertically. These can have a wide range of applications depending upon the needs of the users. Here we will be implementing the simplest form of scrollable text. We can
3 min read
Flutter - Named Routes
An app has to display multiple screens depending upon the user's needs. A user needs to back and forth from the multiple screens to the home screen. In, Flutter this is done with the help of Navigator.Note: In Flutter, screens and pages are called routes. Steps to Implement Named Routes in FlutterSt
3 min read
Flutter - Staggered Grid View
Staggered Grid View is a type of layout that is used to display images and posts. As you see in various social platforms such as Pinterest, Instagram and many more. The main feature of Staggered Grid View is that it makes the layout beautiful and develop a great User Experience. Staggered Grid View
4 min read
Flutter - Inherited Widget
If you are a flutter developer then you know how easy is to create Flutter UI. But when you have lots of widgets in your app and you want to pass data from one widget to another widget, this will be a pain for many developers,s especially for the newbie, this will be really hard for them to pass the
6 min read
Flutter - GestureDetector Widget
A GestureDetector is a very useful Flutter widget that allows you to recognize and respond to various touch gestures, such as taps, swipe, double taps, drags, and more. It provides a way to make your Flutter app interactive and responsive to user input. In this article, we are going to implement and
4 min read
Flutter - GridView
Flutter's GridView is a widget similar to a 2-D array found in many programming languages. As its name indicates, the GridView widget is used to display content in a grid format. This allows us to showcase images, text, icons, and more within the GridView. There are several ways to implement GridVie
6 min read