Updated 27 April 2023
Every UI element is widget in flutter.For designing UI, we have multiple widgets like Container,listview,gridview.There exists a special kind of widget known as slivers.
A sliver is a section of a scrollable zone that you can mark as acting differently. Slivers can be used to create bespoke scrolling over effects, such as flexible scrolling over. Making scrollable content in Flutter is simple, whether you’re trying to pin widgets or create parallax effects.Slivers are a fantastic way to keep track of how various widgets in a format behave as the user scrolls up and down.Slivers cannot be directly used in Scaffold. We will have to add a CustomScrollView which can hold an array of slivers.
Check out more about our Flutter app development services
Following are the famous kinds of slivers that are in use:
1.SliverList
2.SliverGrid
When we have to render a list of items in CustomScrollView which can be scrolled vertically, a SliverList is used. It has a property named delegate which accepts SliverChildBuilderDelegate. SliverList is especially used when the number of items in the list is not known – such as in cases where you need to render the list containing responses from the server API.
SliverChildBuilderDelegate has a property childCount of type integer which takes in the length of an array. This works similarly to Listview.builder.
For e.g.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
void main() { runApp(MyApp()); } class MyApp extends StatelessWidget { @override Widget build(BuildContext context) { return MaterialApp( debugShowCheckedModeBanner: false, home: HomePage(), ); } } class HomePage extends StatefulWidget { @override _HomePageState createState() => _HomePageState(); } class _HomePageState extends State<HomePage> { @override Widget build(BuildContext context) { return Scaffold( body: CustomScrollView( slivers: [ SliverAppBar( backgroundColor: Colors.green, title: Text('Silver AppBar'), ), SliverList( delegate: SliverChildBuilderDelegate( (BuildContext context, int index) { return Card( margin: EdgeInsets.all(15), child: Container( color: Colors.orange[100 * (index % 12+1)], height: 60, alignment: Alignment.center, child: Text( "List Item $index", style: TextStyle(fontSize: 30), ), ), ); }, childCount: 10, ), ), ], )); } } |
When we have to render a list of items in CustomScrollView which can be scrolled horizontally, a SliverGrid is used. It has a property named delegate which accepts SliverChildBuilderDelegate. Like SliverList, SliverGrid is also used when the number of items on the list is not known such as in cases when there is a need to render a list containing responses from a server API.
SliverChildBuilderDelegate has a property childCount of type integer, which takes the length of an array. It is equivalent to Gridview.builder.
We have two more Grid properties in Slivers:-
SliverGrid.extent
In CustomScrollView, when the number of items in the list is already known and can be scrolled horizontally, SliverGrid.extent is used. It has a property – children – which takes in a list of widgets. SliverGrid.extent is especially used when the number of items is fixed.
SliverGrid.count
Like SliverGrid.extent, SliverGrid.count also renders a list horizontally, but it differs from the former in one way: it allows us to set the number of widgets to be rendered in SliverGrid.count. Like SliverGrid.extent, it too has the same property – children – which takes in a list of widgets. SliverGrid.count is used when the number of items is fixed.
For e.g.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 |
void main() { runApp(MyApp()); } class MyApp extends StatelessWidget { @override Widget build(BuildContext context) { return MaterialApp( debugShowCheckedModeBanner: false, home: HomePage(), ); } } class HomePage extends StatefulWidget { @override _HomePageState createState() => _HomePageState(); } class _HomePageState extends State<HomePage> { @override Widget build(BuildContext context) { return Scaffold( body: CustomScrollView( slivers: [ SliverAppBar( floating: false, expandedHeight: 120.0, flexibleSpace: FlexibleSpaceBar( title: Text('SilverGrid AppBar'), ), ), SliverGrid( delegate: SliverChildBuilderDelegate( (context, index) { return Container( alignment: Alignment.center, color: Colors.teal[100 * (index % 9)], child: Text('grid item $index'), ); }, childCount: 10, ), gridDelegate: SliverGridDelegateWithFixedCrossAxisCount( crossAxisCount: 3, mainAxisSpacing: 15, crossAxisSpacing: 15, childAspectRatio: 2.0, ), ), SliverToBoxAdapter( child: Container( color: Colors.yellow, padding: const EdgeInsets.all(8.0), child: Text('Grid Header', style: TextStyle(fontSize: 24)), ), ), SliverGrid.count( crossAxisCount: 3, mainAxisSpacing: 10.0, crossAxisSpacing: 10.0, childAspectRatio: 4.0, children: <Widget>[ Container(color: Colors.red), Container(color: Colors.green), Container(color: Colors.blue), Container(color: Colors.red), Container(color: Colors.green), Container(color: Colors.blue), ], ), SliverGrid.extent( maxCrossAxisExtent: 200, mainAxisSpacing: 10.0, crossAxisSpacing: 10.0, childAspectRatio: 4.0, children: <Widget>[ Container(color: Colors.pink), Container(color: Colors.indigo), Container(color: Colors.orange), Container(color: Colors.pink), Container(color: Colors.indigo), Container(color: Colors.orange), ], ), ], ) ); } } |
I hope this blog will help in achieving fancy scrolling using slivers.
Reference Links:
https://api.flutter.dev/flutter/widgets/SliverList-class.html
https://api.flutter.dev/flutter/widgets/SliverGrid-class.html
If you have more details or questions, you can reply to the received confirmation email.
Back to Home
Be the first to comment.