What is the difference between ListView and SingleChildScrollView in Flutter?
Solution 1
You could consider ListView as an optimisation to the combination of SingleChildScrollView + Column.
By using ListView, only the items that are visible are mounted and painted.
On the other hand, by using SingleChildScrollView+Column, the entire item list is mounted+painted, even if only a few items are visible.
The tradeoff is that ListView is less flexible. So for complex layouts with a small number of items, the performance gain may not be worth the trouble, in which case we can use SingleChildScrollView.
Solution 2
ListView, you would use, if you have a list of UI widgets to render, and the number of such widgets is dynamic.
For example: You need to only show a list of contacts on your screen and the list items are more or less similar (UI wise and category wise). Then you would use a Listview.
A SingleChildScrollView can be compared to "ScrollView" in Android. Suppose you have a lot of UI widgets (Buttons, InputFields, Cards, etc), and they start going out of the screen. In such cases, your widgets will overflow and you will start seeing warnings. To fix this, you wrap your UI widgets with a SingleChildScrollView.
For your usecase, "Recommended" section, ListView would be a good choice.
Solution 3
ListView: is used when we have to render the same kind of widgets for n elements
SingleChildScrollView: is used when we have different widgets for n elements.
Ideally, both the cases we required scrolling, Listview have default behavior, but column and other widgets don't have so required to use SingleChildScrollView
Updated: One important point you should take care while choosing ListView
ListView() -- Render all the items in the list, even if it is not visible.
`ListView.Builder()` -- Render only visible items on the screen.
BeingSuman
Updated on June 07, 2022Comments
-
BeingSuman almost 2 years
I have seen few examples using
ListView
as the mainbody
element ofScaffold
and in few tutorials, it'sSingleChildScrollView
. All that I understood is both allow some axis of scrolling based on the direction that is configured but cannot figure out when to use one over the other?ListView:
Scaffold body : ListView children : Padding/Container
SingleChildScrollView:
Scaffold body : SingleChildScrollView child : Column
-
BeingSuman almost 4 yearsCan you elaborate different widgets for n elements ? Consider i have to display few menu items with Recommended section as show here. Which one would you prefer, ListView / SingleChildScrollView ? And why ?
-
Jitesh Mohite almost 4 yearsWhen you only ListView widget in your screen then Listview, but for the different widgets, SingleChildScrollView as it has the ability to scroll all child widgets declare inside it.
-
BeingSuman almost 4 yearsThanks @Kumar for the answer, it was helpful.
-
BeingSuman almost 4 yearsThanks @Remi for the answer, it was helpful. Just wondering how you gained so much knowledge on Flutter ? Any resources you recommend for learning.
-
BeingSuman almost 4 yearsThanks @jitsm555 for the answer, it was helpful.
-
Rémi Rousselet almost 4 yearsCuriosity. I've asked myself all these questions before, and searched for the answer autonomously.
-
D B over 3 years
Listview
itself isn't efficient or what you called optimized, because it also renders and paints al the items in its list.Listview.builder()
is the one which use lazy loading and uses resources efficiently. -
Rémi Rousselet over 3 years@Badamchi no
ListView
does the lazy rendering too. The only difference is the memory allocation for thechildren
parameter. -
D B over 3 yearsQuote "In contrast to the default ListView constructor, which requires creating all items at once, the ListView.builder() constructor creates items as they’re scrolled onto the screen." Reference: flutter.dev/docs/cookbook/lists/long-lists
-
Rémi Rousselet over 3 years@Badamchi yes, but this has nothing to do with rendering. "creating an item (aka widget)" doesn't mean that it will be rendered.
-
CoderUni over 3 years@RémiRousselet Thanks for your answer. What's the difference of creating vs rendering a widget?
-
cofirazak almost 3 years@RémiRousselet Hi, Rémi, you say "The tradeoff is that ListView is less flexible." - could you please explain what exactly you mean by this?
-
ig-dev about 2 yearsThere is nothing wrong with using ListView for different types of widgets.
-
chemturion about 2 years@ig-dev can you please substantiate your claim? Why is there nothing wrong using ListView for different types of widgets?
-
ig-dev about 2 years@chemturion The other way: why is there anything wrong with non-homogeneous children? There is no such constraint. Documentation also mentions no such thing, and it works just as expected. You can use it to lay out your application. The claim is made up by OP out of nowhere.