I want to loop some part of function base on widget.menuItem.category.length, I am new in flutter.
Here is my current code :
_buildContentView({BuildContext context}) {
return Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: <Widget>[
_buildMenuItemImage(),
Container(
child: Text(widget.menuItem.name,
style: TextStyle(fontSize: 18, fontWeight: FontWeight.bold)),
padding: EdgeInsets.only(left: 16, right: 16, top: 12, bottom: 8),
),
Container(
child: Text(widget.menuItem.itemDescription,
style: TextStyle(fontSize: 18, fontWeight: FontWeight.normal)),
padding: EdgeInsets.only(left: 16, right: 16, top: 12, bottom: 8),
),
Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: <Widget>[
Container(
child: Text('Select ' + widget.menuItem.category[0]),
padding: EdgeInsets.only(left: 16, right: 16, top: 8, bottom: 6),
),
Container(
child: Text('Select ' +
_qty.toString() +
' item (' +
_qty.toString() +
'/' +
widget.menuItem.setMenuItems.length.toString() +
')'),
padding: EdgeInsets.only(left: 16, right: 16, top: 8, bottom: 6),
),
],
),
_buildSetMenusView(setMenuItems: widget.menuItem.setMenuItems),
Padding(
padding: EdgeInsets.only(left: 12, right: 12, top: 8, bottom: 12),
child: _buildActionButtons(context: context),
)
],
);
}
I want to loop this part :
Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: <Widget>[
Container(
child: Text('Select ' + widget.menuItem.category[0]),
padding: EdgeInsets.only(left: 16, right: 16, top: 8, bottom: 6),
),
Container(
child: Text('Select ' +
_qty.toString() +
' item (' +
_qty.toString() +
'/' +
widget.menuItem.setMenuItems.length.toString() +
')'),
padding: EdgeInsets.only(left: 16, right: 16, top: 8, bottom: 6),
),
],
),
_buildSetMenusView(setMenuItems: widget.menuItem.setMenuItems),
I tried with this code but failed, it did not show on simulator (blank):
for(var i = 0; i < widget.menuItem.category.length; i++){
Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: <Widget>[
Container(
child: Text('Select ' + widget.menuItem.category[i]),
padding: EdgeInsets.only(left: 16, right: 16, top: 8, bottom: 6),
),
Container(
child: Text('Select ' +
_qty.toString() +
' item (' +
_qty.toString() +
'/' +
widget.menuItem.setMenuItems.length.toString() +
')'),
padding: EdgeInsets.only(left: 16, right: 16, top: 8, bottom: 6),
),
],
),
_buildSetMenusView(setMenuItems: widget.menuItem.setMenuItems),
}
What is the correct code to loop this part in flutter? please help me repair this flutter loop code
3
Answers
If i get you right then you have to wrap a widget inside a
ListView.builder
in Flutter, you need to follow a few steps.ListView.builder
is a great choice when you have a large number of children widgets and you want to build them on-demand, as it helps with performance optimization. Here’s how you can wrap a widget inside aListView.builder
:ListView.builder
widget. This widget takes a builder function that generates the list items dynamically based on the index.Replace ‘itemCount’ and ‘YourListItemWidget’ with Your Data: In the
ListView.builder
, replaceitemCount
with the actual number of items you have in your list, and replaceYourListItemWidget
with your custom list item widget.Display the ListView.builder: Finally, add your
ListView.builder
to your widget tree in the appropriate part of your Flutter app.Here’s a complete example:
Replace the
itemCount
and the content ofYourListItemWidget
with your actual data and custom widget as needed. This will allow you to create a scrollable list of items efficiently usingListView.builder
in Flutter.While you are looking inside Column and like to return multiple widget, you can use spreed operator for this.
Also you can return another Column.
To loop through a part of your function based on the widget.menuItem.category length, you can use a ListView.builder or a Column with a loop. Here’s how you can do it using a Column: