How do I use a loop inside a widget?
I want to be able to:
RichText(
text: TextSpan(
text: 'Itemlist\n',
children: <TextSpan>[
for(i = 0; i< items.length; i++){
TextSpan( text: 'item' +i +': ',),
TextSpan( text: 'item[i].value' +i +'\n',),
}
]
)
it is importaint that i could have several lines of code inside the loop.
You can achieve by spread operator which unfold the Iterable<Widget> as follows:
RichText(
text: TextSpan(
text: 'Items\n',
children: <TextSpan>[
for(var i = 0; i< items.length; i++)
...[
TextSpan( text: 'Item ${i.toString()}: '),
TextSpan( text: '${items[i]} \n',),
],
],
),
)
You can check it out in DartPad also.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With