Difference between ListView.builder vs manual list? - flutter

I can also create lists by this code that I can with ListView.builder so what differentiate both?
for(Item in List) View(Item);
I am new to development and trying to understand this.
Is it good if I use above approach?

When items are limited(not more but few) you can use ListView widget, but for many items (like as thousands of items, also created on demand) - You should use ListView.builder.
The difference between of them is: When you use ListView, all the items are rendered on screen. But when you use ListView.builder, items are rendered on demand. Only the items are visible on the screen and few of above & bottom are rendered.
For more:
ListView
ListView.builder

Related

Flutter how to implement multiple scrolling items is same screen

I have a screen with a column that contains 2 items and a list below.
i want to make the first item (1) always scroll up below the appbar or down, doesn't care if the list is empty or full.
the second item (2) should always be visible while scrolling the list items.
see image how can i implement this in flutter
What I suggest you do is to look at Sliver* series of built-in Flutter classes. They give you a great amount of flexibility when it comes to scrolling behavior. Here is a great documentation that demonstrates what I mean: https://docs.flutter.dev/development/ui/advanced/slivers

Is building one large better than building small ~75-100 widgets

Consider the case that I have 3 adjacent list view with each list view having a sized widget of some height. Each list view has around 24 child items. I'm trying to scale the widget using onScaleUpdate in GestureDetector.
onScaleUpdate I want to change the height of each child item of all the 3 listviews.
Is rebuilding all child better or should I rebuild the whole widget?
As #Yeasin Sheikh pointed out, using ListView.builder is good because it builds only the needed children (the ones inside the screen and just a few ones outside as precaution). And as to actually answering your question, I'm no Flutter expert, but using ListView.builder I don't think it makes that much difference, Flutter is intelligent enough to solve this by itself.

Now which widgets are right now shown in List

Hello I have a ListView with different Widgets of different height. Now I want to know which of those Widgets are at the moment visible in the ListView. Does anyone has an idea how to do it. I can't use to scroll_to_widget package because it interferes other implementations.
Try using https://pub.dev/packages/visibility_detector maybe you can create a separate list and store true according to index, based on their visibility?

Scroll Parent ListView when nested ListView.builder is updated

I have a ListView with, say, PostItems being generated with ListView.builder.
Each PostItem MAY have comments on it. So each PostItem has a nested ListView.builder to show comments for that PostItem.
User can add a Comment to the nested ListView. However, UX would like to scroll to that newly added Comment in the nested ListView. Nested Listview shrinkwrap is true, with NeverScrollable Physics. When nested Comment listview expandeds with new Comment, it's off screen and I want to scroll down to it.
ListView.builder -> PostItem -> Nested ListView.builder
However, my assumption is that I need to make the parent ListView scroll down but how do I know where to scroll to when it's the Nested listview that has grown in height.
I can't share code due to the project but any theological thoughts or ideas would be a great help!
Thanks
I can see that, based on your description, we can access the index of that particular ListView. Thus, I would suggest you to use this package to achieve what you want:
scroll_to_index

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