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 behavior. In this article, we are going to implement the CustomScrollView widget.
A sample video is given below to get an idea of what we are going to do in this article.
Demo Video
Basic Syntax of CustomScrollView Widget
CustomScrollView(
slivers: <Widget>[
// List of sliver widgets
SliverWidget1(
// Sliver widget properties
),
SliverWidget2(
// Sliver widget properties
),
// ... More sliver widgets ...
],
)
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.
- Flutter Installed
- Flutter plugin for VS Code / Android Studio.
Step-by-Step Implementation
Step 1: Create a new Flutter Application
Create a new Flutter application using the command Prompt. To create a new app, type the following command and run it.
flutter create app_nameTo know more about it refer this article: 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.
void main() {
runApp(MyApp());
}
Step 4: Create MyApp Class
In this class, we are going to implement the MaterialApp, Here, we are also setting the Theme of our App.
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,
title: 'CustomScrollView Example',
home: CustomScrollViewExample(),
);
}
}
Step 5: Create CustomScrollViewExample Class
In this class, we are going to implement the CustomScrollView with a SliverAppBar that has an expandable image background and a SliverList containing a list of items. Comments are added for better understanding.
CustomScrollView(
slivers: <Widget>[
// SliverAppBar
SliverAppBar(
expandedHeight: 200.0,
floating: false,
pinned: true,
flexibleSpace: FlexibleSpaceBar(
title: Text(
'CustomScrollView', // Title text for the SliverAppBar
style: TextStyle(color: Colors.black, fontSize: 15),
),
background: Image.network(
'https://static.startuptalky.com/2021/06/GeeksforGeeks-StartupTalky.jpg', // Replace with your image URL
fit: BoxFit.cover, // Ensure the image covers the entire space
),
),
),
// SliverList
SliverList(
delegate: SliverChildBuilderDelegate(
(BuildContext context, int index) {
return ListTile(
title: Text('Item $index'), // Display list item with index
);
},
childCount: 50, // Number of list items
),
),
],
),
class CustomScrollViewExample extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('CustomScrollView Example'),
),
body: CustomScrollView(
slivers: <Widget>[
// SliverAppBar
SliverAppBar(
expandedHeight: 200.0,
floating: false,
pinned: true,
flexibleSpace: FlexibleSpaceBar(
title: Text(
'CustomScrollView', // Title text for the SliverAppBar
style: TextStyle(color: Colors.black, fontSize: 15),
),
background: Image.network(
'https://static.startuptalky.com/2021/06/GeeksforGeeks-StartupTalky.jpg', // Replace with your image URL
fit: BoxFit.cover, // Ensure the image covers the entire space
),
),
),
// SliverList
SliverList(
delegate: SliverChildBuilderDelegate(
(BuildContext context, int index) {
return ListTile(
title: Text('Item $index'), // Display list item with index
);
},
childCount: 50, // Number of list items
),
),
],
),
);
}
}
Complete Source Code
main.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,
title: 'CustomScrollView Example',
home: CustomScrollViewExample(),
);
}
}
class CustomScrollViewExample extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('CustomScrollView Example'),
),
body: CustomScrollView(
slivers: <Widget>[
// SliverAppBar
SliverAppBar(
expandedHeight: 200.0,
floating: false,
pinned: true,
flexibleSpace: FlexibleSpaceBar(
title: Text(
'CustomScrollView', // Title text for the SliverAppBar
style: TextStyle(color: Colors.black, fontSize: 15),
),
background: Image.network(
'https://static.startuptalky.com/2021/06/GeeksforGeeks-StartupTalky.jpg', // Replace with your image URL
fit: BoxFit.cover, // Ensure the image covers the entire space
),
),
),
// SliverList
SliverList(
delegate: SliverChildBuilderDelegate(
(BuildContext context, int index) {
return ListTile(
title: Text('Item $index'), // Display list item with index
);
},
childCount: 50, // Number of list items
),
),
],
),
);
}
}