42

I have seen few examples using ListView as the main body element of Scaffold and in few tutorials, it's SingleChildScrollView. 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

3 Answers 3

118

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.

Sign up to request clarification or add additional context in comments.

Thanks @Remi for the answer, it was helpful. Just wondering how you gained so much knowledge on Flutter ? Any resources you recommend for learning.
Curiosity. I've asked myself all these questions before, and searched for the answer autonomously.
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.
@Badamchi no ListView does the lazy rendering too. The only difference is the memory allocation for the children parameter.
@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?
12

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. 

Can 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 ?
When 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.
Thanks @jitsm555 for the answer, it was helpful.
@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.
I had a very complex layout under the ListView default constructor. Some children were rebuilt each time I scroll up and down. On the other hand, when using SingleChildScrollView+Column, all children were built just once.
6

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.

Thanks @Kumar for the answer, it was helpful.

Your Answer

Draft saved
Draft discarded

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.