How to iterate over list to incorporate it in a wrap widget? - flutter

I am trying to restrict the items(Containers) in the Row to 8 and if there are more items, they should be wrapped. I have a list of Containers but I can not use listView as children of Wrap since listView scrolls. Is there any way to get this layout fixed?
I have tried using for loop but as it hits return for the first time, it gets out of loop.
I have tried gridView instead of wrap but it doesn't give me the result as gridView is scrollable.
Expanded(
flex: 4,
child: Wrap(
direction: Axis.horizontal,
spacing: 0.5,
runSpacing: 0.5,
crossAxisAlignment: WrapCrossAlignment.center,
children: <Widget>[
// I want something that works like following line
//Container(child: kids1)
//currently I can get results with following code
kids1[1],
kids1[2],
kids1[3], kids1[4], kids1[5], kids1[6],
kids1[7], kids1[8], kids1[9], kids1[10],
kids1[11]
],
),
),
kids is a list of Containers

Why don't you create the list of children before the return of the build function :
#override
Widget build(BuildContext context) {
List<Widget> children = List.generate(myContainerList.length, (e) => myContainerList[e]);
return Wrap(
children: children,
);
}

Related

How to animate items inside Wrap widget on add and delete?

Wrap(
direction: Axis.vertical,
runAlignment: WrapAlignment.center,
alignment: WrapAlignment.start,
children: listItems.map((e) => CustomWidget(data: e)).toList(),
),
The children inside Wrap widget are mapped to a custom widget. These items should animate and fill existing space on adding and removing items.
How can we achieve this in Flutter?

Any alternative to Expanded widgets using with ListView.builder

I need to design a screen that shows a different widget (selectedWidget) depending on which button is selected. Problem is that the content is dynamic and I have to use ListView.builder for all the APIs. And ListView.builder only works when the Row containing listview is wrapped in Expanded widget. I want to have two buttons just below my ListView, which I'm not able to achieve due to the Expanded widget which covers up the entire Column in which the ListView is present.
The code gives the basic structure which I've been using. selectedWidget is the ListView.builder widget which will be selected according to the FlatButton selected. Also the Column which contains the selectedWidget has dynamic height. It should adjust according to the size of the listview, so I cannot define a fixed height or even a maxHeight. I can only give minHeight here. And the two buttons below my ListView should be right below my ListView and take the width of the Column with flex 3.
I've also tried disabling the scroll for ListView and using SingleChildScrollView, but then my two buttons below the ListView also start scrolling, which I don't want. I need them to be static on the screen and only the ListView should scroll.
I need a solution for the above problem. I need a workaround for this Expanded widget which doesn't let me align my buttons right below my listview and gives white space. Please share if there is any alternative to ListView.builder because I don't think I can use ListView.builder without the Expanded widget. I've tried every possible structure with ListView and Expanded widget.
class MyScreen extends StatefulWidget {
#override
_MyScreenState createState() => _MyScreenState();
}
class _MyScreenState extends State<MyScreen> {
#override
Widget build(BuildContext context) {
return Column(
children:[
Expanded(
child: Row(
Expanded(flex: 2,
child: Column(
children: [
FlatButton(),
FlatButton(),
FlatButton(),
FlatButton(),
]
)
),
Expanded(flex: 3,
child: Column(
children: [
Expanded(selectedWidget),
Row(children: [FlatButton(), FlatButton()], )
]
)
),
)
)
],);
}
}
Maybe you can use Flexible and set shrinkWrap: true, inside ListView. The following 2 buttons may not need Expand widget after that.
child: Column(
children: [
Flexible(
child: ListView.builder(
shrinkWrap: true,
itemBuilder: (BuildContext context, int index) => Text(index.toString()),
itemCount: 6,
),
),
Row(
... // 2 Buttons

Make a column scrollable in flutter

How can i make a column with many other things in it scrollable
Widget build(BuildContext context) {
return Scaffold(
body: Column(
children: <Widget>[
Expanded(
flex: 10,
child: Stack(
children: <Widget>[..
Wrap the Column in a SingleChildScrollView widget.
If you want to have many other things scrollable you can look at slivers in flutter.
I believe they are a good fit for this.

Card and ListView on single screen

I have a dropdown box and a listview below it on a screen.
On selection of a item in dropdownbox, the list view gets populated with multiple cards. As such the logic is working, but somehow having issues with the scroll configuration.
I have the main parent widget like this:
Widget build(BuildContext context) {
return Column(
children: <Widget>[locationDropdown(), showPackages(plansLoading)],
);
}
The locationDropDown() function returns a card widget with dropdown in it.
showPackages(plansLoading) returns a ListView widget whose code is like
return ListView.builder(
scrollDirection: Axis.vertical,
shrinkWrap: true,
return Card(
color: Colors.transparent,
child: Column(
children: <Widget>[
///...
],
),
);
)
Requirement is to keep the card in the loationDropdown() widget fixed at its place and the cards generated in showPackages's listview be scrolled.
This is the output I am getting, I am unable to scroll.
This functionality is not working. Where am I going wrong?
you can used SafeArea Widget or use Expanded widget before Listview.
ex.
Column (
children: <Widget>[
new TextField(
decoration: new InputDecoration(
labelText: "Search something"
),
Expanded(
ListView(),
),
]
)

When I Wrap a chips list, how can I limit the area (the wrap area) to 2 lines max?

I have a list that I want to show in chips and organize in a Wrap widget.
However, no matter what the list.length is (or the length of a single string inside the list), I want to build only 2 lines of the Wrap area.
Here is my code:
class TagsView extends StatelessWidget {
#override
Widget build(BuildContext context) {
List<Widget> list = List<Widget>.from(
{"bla1", "bla2", "bla3", "bla4", "bla5", "bla6", "bla7","bla8", "bla9", "bla10", "bla11", "bla12", "bla13", "bla14"}
.map((tag) => Chip(
label: Text(
tag,
),
))
.toList());
return Wrap(
verticalDirection: VerticalDirection.down,
spacing: 5.0,
runSpacing: 10.0,
children: list,
);
}
}
and here are Examples for the unwanted code (the first screenshot is the result of the code above)
A good result will look like this:
You can put it in a Container and limit the height.
It will always show the first two lines.
return Container(
height: 100,
child: Wrap(
verticalDirection: VerticalDirection.down,
spacing: 5.0,
runSpacing: 10.0,
children: list,
),
);
Probably too late, but you can use the take method.
List<Widget> list = List<Widget>.from({
"bla1",
"bla2",
"bla3",
"bla4",
"bla5",
"bla6",
"bla7",
"bla8",
"bla9",
"bla10",
"bla11",
"bla12",
"bla13",
"bla14"
}
.take(2)
.map((tag) => Chip(
label: Text(
tag,
),
))
.toList());