Flutter Slivers

 

Flutter Slivers

Sliver is a portion of the scrollable area which is used to achieve a custom scrolling effect. In other words, the sliver widget is a slice of the viewport. We can implement all of the scrollable views, such as ListView, GridView using slivers. It is a lower-level interface that provides excellent control over implementing a scrollable area. It is useful while scrolling large numbers of children widgets in a scrollable area. As they are based on the viewport, it can change their shape, size, and extent based on several events and offset.

Flutter provides several kinds of slivers, some of them are given below:

  • SliverAppBar
  • SliverList
  • SliverGrid

How to use slivers?

It is to note that all of the sliver's components should always be placed inside a CustomScrollView. After that, we can combine the list of slivers to make the custom scrollable area.

What is CustomScrollView?

CustomScrollView in Flutter is a scroll view that allows us to create various scrolling effects such as grids, lists, and expanding headers. It has a sliver property where we can pass a list of widgets that include SliverAppBar, SliverToBoxAdapter, SliverList, and SliverGrid, etc.

Let us discuss each sliver in detail.

SliverAppBar

SliverAppBar is a material design app bar in Flutter that integrates with a CustomScrollView. Generally, we used it as the first child of CustomScrollView. It can vary in height and float above the other widget of the scroll view. It allows us to create an app bar with various scrolling effects.

Properties of SliverAppBar

The following are the essential properties of the SliverAppBar:

actions: This property is used to create widgets right of the appBar title. Generally, it is an iconButton that represents common operations.

title: This property is used to define the title to the SliverAppBar. It is similar to the AppBar title to give the name of the application.

leading: This property is used to define a widget left to the title. Generally, it is used to place the Menu Drawer widget.

backgroundColor: This property is used to define a background color to the sliver app bar.

bottom: This property is used to create space to the bottom of the title, where we can define any widget according to our needs.

expandedHeight: This property is used to specify the maximum height to the app bar that can be expanded while scrolling. It must be defined in a double value.

floating: This property determines whether the app bar will be visible or not when the user scrolls towards the app bar.

flexibleSpace: This property is used to define a widget which is stacked behind the toolbar and the tab bar. Its height is the same as the app bar's overall height.

Example

In the below example, we will see how to use the SliverAppBar with CustomScrollView.

  1. import 'package:flutter/material.dart';  
  2.   
  3. void main() => runApp(MyApp());  
  4.   
  5. class MyApp extends StatelessWidget {  
  6.   @override  
  7.   Widget build(BuildContext context) {  
  8.     return MaterialApp(  
  9.       home: Scaffold(  
  10.         body: CustomScrollView(  
  11.           slivers: <Widget>[  
  12.             SliverAppBar(  
  13.               actions: <Widget>[  
  14.                 Icon(Icons.person, size: 40,)  
  15.             ],  
  16.               title: Text("SliverAppBar Example"),  
  17.               leading: Icon(Icons.menu),  
  18.               backgroundColor: Colors.green,  
  19.               expandedHeight: 100.0,  
  20.               floating: true,  
  21.               pinned: true  
  22.             )  
  23.             // Place sliver widgets here  
  24.           ],  
  25.         ),  
  26.       ),  
  27.     );  
  28.   }  
  29. }  

Output

When we run the app, we should get the UI of the screen similar to the below screenshot:

Flutter Slivers

SliverList

SliverList is a sliver that places the children in a linear array or one-dimensional array. It takes a delegate parameter to provide the items in the list in a way they will scroll into view. We can specify the children's list using a SliverChildListDelegate or build them with a SliverChildBuilderDelegate.

Example

In the below example, we will see how to use the SliverList with CustomScrollView.

  1. import 'package:flutter/material.dart';  
  2.   
  3. void main() => runApp(MyApp());  
  4.   
  5. class MyApp extends StatelessWidget {  
  6.   @override  
  7.   Widget build(BuildContext context) {  
  8.     return MaterialApp(  
  9.       home: Scaffold(  
  10.         body: CustomScrollView(  
  11.           slivers: <Widget>[  
  12.             SliverAppBar(  
  13.               actions: <Widget>[  
  14.                 Icon(Icons.person, size: 40,)  
  15.             ],  
  16.               title: Text("SliverList Example"),  
  17.               leading: Icon(Icons.menu),  
  18.               backgroundColor: Colors.green,  
  19.               expandedHeight: 100.0,  
  20.               floating: true,  
  21.               pinned: true  
  22.             ),  
  23.             SliverList(  
  24.               delegate: new SliverChildListDelegate(_buildList(20)),  
  25.             ),// Place sliver widgets here  
  26.           ],  
  27.         ),  
  28.       ),  
  29.     );  
  30.   }  
  31. }  
  32. List _buildList(int count) {  
  33.   List<Widget> listItems = List();  
  34.   for (int i = 0; i < count; i++) {  
  35.     listItems.add(new Padding(padding: new EdgeInsets.all(16.0),  
  36.         child: new Text(  
  37.             'Sliver Item ${i.toString()}',  
  38.             style: new TextStyle(fontSize: 22.0)  
  39.         )  
  40.     ));  
  41.   }  
  42.   return listItems;  
  43. }  

Output

When we run the app, we should get the UI of the screen similar to the below screenshot:

Flutter Slivers

SliverGrid

SliverGrids places the children in a two-dimensional arrangement. It also uses a delegate to specify the children or an explicit list similar to the SliverList. But it has additional formatting to the cross-axis dimension on a grid. It allows the three ways to specify the grid layout:

1. It uses Count Constructor for counting the number of items in the horizontal axis. See the below code:

  1. SliverGrid.count(  
  2.   crossAxisCount: 3,  
  3.   mainAxisSpacing: 20.0,  
  4.   crossAxisSpacing: 20.0,  
  5.   childAspectRatio: 3.0,  
  6.   children: <Widget>[  
  7.     Container(color: Colors.red),  
  8.     Container(color: Colors.blue),  
  9.     Container(color: Colors.green),  
  10.     Container(color: Colors.red),  
  11.     Container(color: Colors.blue),  
  12.     Container(color: Colors.green),  
  13.   ],  
  14. ),  

2. It uses Extent Constructor, which specifies the maximum width of the items. It is most useful when the grid items vary in size. It means we can limit how large space they should take up. See the below code:

  1. SliverGrid.extent(  
  2.   maxCrossAxisExtent: 200,  
  3.   mainAxisSpacing: 10.0,  
  4.   crossAxisSpacing: 10.0,  
  5.   childAspectRatio: 4.0,  
  6.   children: <Widget>[  
  7.     Container(color: Colors.orange),  
  8.     Container(color: Colors.yellow),  
  9.     Container(color: Colors.purple),  
  10.     Container(color: Colors.pink),  
  11.     Container(color: Colors.green),  
  12.     Container(color: Colors.indigo),  
  13.   ],  
  14. ),  

3. It uses Default constructor which is pass in an explicit gridDelegate parameter:

Example

In the below example, we will see how to use the SliverGrid with CustomScrollView.

  1. import 'package:flutter/material.dart';  
  2.   
  3. void main() => runApp(MyApp());  
  4.   
  5. class MyApp extends StatelessWidget {  
  6.   @override  
  7.   Widget build(BuildContext context) {  
  8.     return MaterialApp(  
  9.       home: Scaffold(  
  10.         body: CustomScrollView(  
  11.           slivers: <Widget>[  
  12.             SliverAppBar(  
  13.               actions: <Widget>[  
  14.                 Icon(Icons.camera_front, size: 40,)  
  15.             ],  
  16.               title: Text("SliverGrid Example"),  
  17.               leading: Icon(Icons.menu),  
  18.               backgroundColor: Colors.green,  
  19.               expandedHeight: 100.0,  
  20.               floating: true,  
  21.               pinned: true  
  22.             ),  
  23.             SliverGrid(  
  24.               gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(  
  25.                 crossAxisCount: 4,  
  26.               ) ,  
  27.               delegate: SliverChildBuilderDelegate((BuildContext context, int index) {  
  28.                 return new Container(  
  29.                     color: _randomPaint(index),  
  30.                     height: 150.0  
  31.                 );  
  32.               }),  
  33.             ),  
  34.           ],  
  35.         ),  
  36.       ),  
  37.     );  
  38.   }  
  39. }  
  40. Color _randomPaint(int index) {  
  41.   if (index % 3 == 0) {  
  42.     return Colors.orange;  
  43.   } else if (index % 3 == 1) {  
  44.     return Colors.blue;  
  45.   }  
  46.   return Colors.red;  
  47. }  

Output

When we run the app, we should get the UI of the screen similar to the following screenshot:

Flutter Slivers

SliverFixedExtentList and SliverToBoxAdapter

A SliverFixedExtentList is a sliver that holds multiple children with the same main-axis extent in a one-dimensional array or linear array. It is more efficient than SliverList because there is no need to perform layouts on its children to obtain the main-axis extent. It does not allow a gap between its children. It requires each child to have the SliverConstraints.crossAxisExtent in the cross axis and the itemExtent property on the main-axis.

A SliverToBoxAdapter is a sliver that can hold only a single box widget. It is useful when we want to display only a single child widget in CustomScrollView to create a custom scroll effect. If we need to display multiple box widgets in a CustomScrollView, we must use the SliverList, SliverFixedExtentList, or SliverGrid.

Example

In the below example, we will see how to use the SliverFixedExtentList and SliverToBoxAdapter with CustomScrollView.

  1. import 'package:flutter/material.dart';  
  2.   
  3. void main() => runApp(MyApp());  
  4.   
  5. class MyApp extends StatelessWidget {  
  6.   @override  
  7.   Widget build(BuildContext context) {  
  8.     return MaterialApp(  
  9.       home: Scaffold(  
  10.         body: CustomScrollView(  
  11.           slivers: <Widget>[  
  12.             SliverAppBar(  
  13.               actions: <Widget>[  
  14.                 Icon(Icons.camera_front, size: 40,)  
  15.             ],  
  16.               title: Text("Sliver Example"),  
  17.               leading: Icon(Icons.menu),  
  18.               backgroundColor: Colors.green,  
  19.               expandedHeight: 100.0,  
  20.               floating: true,  
  21.               pinned: true  
  22.             ),  
  23.             SliverFixedExtentList(  
  24.               itemExtent: 75,  
  25.               delegate: SliverChildListDelegate([  
  26.                 Container(color: Colors.blue),  
  27.                 Container(color: Colors.pink),  
  28.                 Container(color: Colors.yellow),  
  29.               ]),  
  30.             ),  
  31.             SliverToBoxAdapter(  
  32.               child: Container(  
  33.                 color: Colors.orange,  
  34.                 padding: const EdgeInsets.all(16.0),  
  35.                 child: Text('Sliver Grid Header', style: TextStyle(fontSize: 28)),  
  36.               ),  
  37.             ),  
  38.           ],  
  39.         ),  
  40.       ),  
  41.     );  
  42.   }  
  43. }  

Output

When we run the app, we should get the UI of the screen similar to the below screenshot:

Flutter Slivers


Post a Comment

Previous Post Next Post