Making parent adjust to size of child widget - flutter

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),
);

Related

Flutter How To Size Horizontal Scrollable In Vertical Layout

I'm having trouble working out how to size a horizontal scrollable (ListView) within a vertical layout (a vertical ListView in this case).
I want the ListView to be as tall as its contents but am unsure how to achieve this with a horizontal scrollable.
For example:
ListView(
children: [
//How do I size this ListView to the height of its child?
ListView(
scrollDirection: Axis.horizontal,
children: [for (int i = 0; i < 2; i++) Widget()],
),
],
),
I don't think that would be possible. It would require the ListView to build every child to use the maximum height and this would go again the lazy loading behaviour of the ListView.
Try wrapping the inner ListView with a Container of height: double.infinity
You could wrap the outer ListView in a Container with a height you would like to set, this would set the height of the outer ListView and the inner ListView inside the container should expand this outer ListView's height.
Try using shrinkWrap: true. It's explained in details in official docs and in another answer on stackoverflow

What does shrinkWrap do in the ListView.builder?

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.

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.

Is it possible to use ReorderableListView in a "wrapping" mode?

I would like the ReorderableListView not to take up the whole height which is given to it, but to be as tall as the item views that it contains.
I could make the list either flex and fill the whole height it is given, or put it in a SizedBox and have a fixed height.
I'd like to add an + Add list tile directly under the list items (similar to Google Keep on Android)
Thank you
You can try this:
Wrap your List with an Expanded widget.
Fetch the number of items in your list and return the Add List tile widget at the length-1 position of your list.
You can alter the size of your ListView in correlation to the number of items fetched from the list.
Expanded will help you alter the length dynamically.
eg:
Expanded(
child: new ListView.builder
(
itemCount: litems.length,
itemBuilder: (BuildContext ctxt, int Index) {
return new Text(litems[Index]);
if(length==length-1){
//**call your Add List tile widget here**
}
}
)
)

How to add wrap future builder inside column

This is my simple outline code to generate ListView inside Future builder.
What I wanted to achieve is that I want some widget below of the FutureBuilder widget so I wrapped it with column but the content of future builder got just vanished after adding, but before adding it was fine.
Widget build(BuildContext context) {
child:FutureBuilder(
future: msDB.getListOfMoviesSeries(widget.type,widget.isWatched),
builder: (BuildContextcontext, AsyncSnapshot<List<MovieSeries>> snapshot) {
return ListView.builder(itemBuilder: (context, index) {
return ListTile(
........
)
}
);
}
);
}
Since you are using ListView in Column, The Column has an unbounded height in the vertical axis and your ListView will try to expand to maxHeight and the flutter framework will throw an error. By setting shrinkWrap to true, the extent of the scroll view in the scroll direction is determined by the contents being viewed
So to fix the issue you add following line in your ListView.builder
shrinkWrap:true
You can read more about shrinkWrap here
Hope this helps!