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 widget. A sample video is given below to get an idea about what we are going to do in this article.
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 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 , 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/w3wiki-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
),
),
],
),
Dart
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/w3wiki-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 ), ), ], ), ); } } |
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 , 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/w3wiki-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 ), ), ], ), ); } } |