Differences between SliverList vs ListView in Flutter - flutter

What are the differences between SliverList and ListView in Flutter?

There's almost no difference.
ListView is a SliverList. Same with GridView, which is a SliverGrid.
They are doing exactly the same thing. The only difference between them is that SliverList is a sliver, not a widget. Which means it's used inside a ScrollView, usually CustomScrollView.
ListView is nothing else but a biding of SliverList to transform it into a Widget to make it usable alongside other widgets such as Row/Container.
Most of the time, use ListView.
But if you want advanced scroll behavior such as appbar animations with scroll ; you'll need to use a CustomScrollView. Which will force you to use SliverList instead of ListView.

According this article,
All of the scrollable views you use, like ListView and GridView,
are actually implemented using Slivers. You can kind of think of
Slivers as a lower-level interface, providing finer-grained control on
implementing scrollable area. Because slivers can lazily build each
item just as it scrolls into view, slivers are particularly useful for
efficiently scrolling through large numbers of children.

Related

PageView scroll under SliverAppBar

In material 3 the AppBar changes elevation and color when there is something scrolled under it.
It works fine using CustomScrollView with a SliverList or ListView scrolling under a SliverAppBar.
The issue is when I try to use PageView instead. It works fine using a SliverFillRemaining over the PageView but the appBar won’t notice when the page is scrolled.
I tried many ways, using NestedScrollView and CustomScrollView with no success.
The only way I could achieve that was using the PageView onPageChanged to check the page and calling back the setState of my homePage to change the color and elevation programmatically but at the cost of rebuilding almost the whole tree when it reaches or leaves the 1st page.
Any insights would be appreciated.

Present an empty view in a Flutter widget to be used in the slivers of a CustomScrollView

Related question:
How to present an empty view in flutter?
Like that question, I need to return an empty view from Widget.build to indicate that there is nothing to render, but of course cannot return null. The widget will be used in the slivers of a CustomScrollView.
However, the suggestions from the answers to that question give errors when trying to use them in a Sliver (for example SizedBox.shrink()).
It seems that an empty const SliverToBoxAdapter() works.
Is this the best way?
The CustomScrollView needs Sliver type widgets.
SizedBox, Container, Row, Column, etc are not Sliver types at all.
So you need to convert them using SliverToBoxAdapter / SliverList / etc
SliverToBoxAdapter(child: SizedBox.shrink()) seems the more explicit to me yeah

DraggableScrollableSheet but not scrollable

I need a DraggableSheet but not scrollable, so it does not require any scrollable widget inside it.
What I need is a DraggableSheet that only scrollable until it expands and not the content inside.
is there any native widget or 3rd party library for it?
Since the builder for the DraggableScrollableSheet requires you to return a Scrollable widget, you could return a ListView with NeverScrollableScrollPhysics assigned to the physics argument. Or any other widget you require that allows you to set the physics to NeverScrollableScrollPhysics
More info and code samples on the Flutter docs
More info and code samples on the NeverScrollPhysics docs
Have you try to just using a Container ?

which class or widget to use a better responsive layout in the flutter?

This class or widget is useful to create a responsive layout.
AspectRatio
CustomSingleChildLayout,
CustomMultiChildLayout,
FittedBox,
FractionallySizedBox,
LayoutBuilder,
MediaQuery,
MediaQueryData,
OrientationBuilder.
Which is to use the better responsive layout.
iDecode already told you that it all depends on what you need. here take a look at official documentation, https://flutter.dev/docs/development/ui/layout.
And here is my advice
Use row when you need Horizontal layout
Use column when you need Vertical layout
Use singleChildScrollView when you need scrolling layout
Use either GridView or listView when you need repetitive view
Use Expanded, Flexible, Limitedbox, flexiblebox for adjusting the size of the widget
Mine favourite : Rows for Horizontal, Column for vertical, Scrollview for Extra data exist in the page or UI not fitted in the screen, ListView for TableView UI etc
For best fitting layout: IS Expanded, AspectRatio

In which circumstances should you use listview instead of listview.builder?

Like the topic I was wondering of there are any circumstances one should use the regular ListView instead of the ListView.builder in flutter, like if there are few items in a list could the ListView give better performance?
ListView has actually four different ways to use it , But let discuss ListView and ListView.builder
ListView : It has a children
property that takes a collection of static widgets. A ListView takes a small number of other widgets and makes it scrollable. Why a “small number”? Because this is designed to be a static list, one that you, the developer, simply types into the build() method
by hand.
ListView.builder : ListView’s alternative constructor, ListView.builder receives two
parameters, an itemCount and an ItemBuilder property that is a
function. This makes the ListView lazy-loaded. The itemBuilder function
dynamically creates children widgets on demand. As the user scrolls close
to the bottom of the list, itemBuilder creates new items to be scrolled into
view. And when we scroll something far enough off the screen, it is paged
out of memory and disposed of. Pretty cool.
Reference : taken from Rap Payne's Beginning App Development with Flutter (Great Book for beginners! , not an affiliate link).
official documentation for ListView .
ListView is the most commonly used scrolling widget. It displays its children one after another in the scroll direction. So if you just want to show some widgets below earch other and you need to scroll them you use ListView.
ListView.builder is a way of constructing the list where children’s (Widgets) are built on demand. However, instead of returning a static widget, it calls a function which can be called multiple times (based on itemCount ) and it’s possible to return different widget at each call.
I guess the simple answer you were looking for is:
Use ListView.builder whenever you iterate over an array of 'similar' elements.
Use ListView when items in your list are completely different from one another.
Think of ListView as scrollable Column