What does shrinkWrap do in the ListView.builder? - flutter

In the following example, which is making a horizontal list of product cards, a shrinkWrap property was used. Newetherless I did not notice any difference wheither it used or not. So what's actualy the purpose of the shrinkWrap here?
ListView.builder(
shrinkWrap: true,
padding: EdgeInsets.symmetric(horizontal: 20, vertical: 10),
scrollDirection: Axis.horizontal,
itemCount: products.length,
itemBuilder: (context, index) {
return ProductCard(product: products[index]);
},
)

ListView usually fills all the available space of its own parent, without any attention to the minimum size that each item needs. with using shrinkWrap and setting it to true you can change this scenario so that the ListView only occupies the space that each item needs in general. Obviously, if the parent widget does not have the minimum height, it would scroll even if you set shrinkWrap to true.

This constructor is appropriate for list views with a large (or infinite) number of children because the builder is called only for those children that are actually visible.
If you do not set the shrinkWrap property, your ListView will be as big as its parent.
If you set it to true, the list will wrap its content and be as big as it children allows it to be.

Related

Flutter : Display data in listview with static string and scrollable array

My data is like this:
I want to display it like this:
In the example, the title and subtitle are static. But in my case, it is dynamic. How do I achieve this UI? I tried like putting it in a ListView.builder with horizontal scrolling. But again do I need another ListView.builder to show restaurant list from rests array? Or any other ways to do this?
That can depend on your layout.
You can look at the use case below.
Using ListView + ListView:
If you want to give your second ListView full height.
ListView(
children: [
// Other widgets ...
ListView.builder(
shrinkWrap: true, // <-- Set this to true
physics: ScrollPhysics(), // <-- Also change physics
itemBuilder: ...,
),
],
)
YES; You can use listview to implement this functionality.
demo(){
List<Map> restaurantArray = [
{"name":"breakfase 1","cover":"http://......."},
{"name":"breakfase 2","cover":"http://......."},
{"name":"breakfase 3","cover":"http://......."},
{"name":"breakfase 4","cover":"http://......."},
{"name":"breakfase 5","cover":"http://......."},
];
return ListView.builder(
itemCount: restaurantArray.length,
itemBuilder: (ctx,index){
// return your cell with data
return Container(
child: Text(restaurantArray[index]["name"].toString()),
);
}
);
}

Making parent adjust to size of child widget

I am using a ListView Builder, and that has to be wrapped inside a Container. If i don't give a height parameter then i get Vertical viewport was given unbounded height. Now, because of this if i only have 1 item in the list, any content comes after the container, which wastes a lot of screen real estate and doesn't look aesthetically pleasing.
How can i make the parent widget i.e Container() to adjust to the size of child widget ListView.builder() ?
Add this shrinkWrap: true to your ListView and Remove Height from container.
snippet code:
return Container(
child: ListView.builder(itemBuilder: (context, index) {
return listItem(itemArray[index]),
},
itemCount: itemArray.length,
shrinkWrap: true),
);

Idea in order to address lack of scrollbar in Flutter web

I am new to flutter and using it for web. I see that there is no built in scrollbar when the page overflows the viewport
Would it be possible to somehow have a javascript script in the html that would check the height of the content periodically and add/remove scroll bar as necessary?
Does this make any sense?
Thank you
I'm not sure if its what you want but if you wrap your Listview with Scrollbar then you get the result you want:
Scrollbar(
child: ListView.builder(
itemCount: 20,
itemBuilder: (context, index) => ListTile(title: Text("Item= ${index + 1}"),),),
)
You can find more here

How do i monitor which ListView item is selected in Flutter?

In my listView i have the itemBuilder value set to a custom widget
Widget programsRowWidget(BuildContext context, List<MembershipPrograms> programs) {
return Expanded(
child: ListView.builder(
scrollDirection: Axis.horizontal,
shrinkWrap: true,
itemCount: programs.length,
itemBuilder: (context, index) {
return UsersProgramListItem(program: programs[index]);
}
),
);
In that widget the root element is a Card, whose child is an InkWell. I want to use it's onTap property to control if the item is selected or not. How do i monitor which item in the listView is selected from the listView in the parent widget shown above, and how do i allow only one item to be selected?
How do i monitor which item in the listView is selected from the listView in the parent widget shown above
You can use bool flags such as isSelected and isSelectable, which is passed to a widget in a builder function. The main aspect there is to separate logic of selection from ListView.builder to upper widget or to object of your element.
How do i allow only one item to be selected?
Use flags. When one item is selected - the flag of selectable for others can be set to false.

Height of listview.builder depend on items count/context

Height of listview.builder depend on items count.I have different count of items and in all case i have different height of my listvuew.builder.Is it posible in flutter?
If you mean that you want to have different listview with different size, Yeah! it is possible. ListView automatically create the size for you. But you shared very little information and no code, so i persume you want to have a listView on different cases, then you can do it like this:
return Scaffold(
appBar: AppBar(
title:Text("ListView.builder")
),
body: ListView.builder(
itemCount: count, //the variable which holds the count.
itemBuilder: (BuildContext context,int index){
return ListTile(
//Your code
);
}
),
);
I hope it answers your question, if not try expanding your question or letting me know it in the comment. Happy coding:)