In my Flutter application I need to display a list with one or two item cards per line accordingly to the property singleLine. If it’s true, the item must be displayed in the whole line, if it’s false then I need to check the next item. If both have singleLine as false, then I must display both in the same line, otherwise both will be displayed in separate lines using full width. For example, consider the following list:
final cards = [
{"title": "Item 1", "singleLine": true},
{"title": "Item 2", "singleLine": false},
{"title": "Item 3", "singleLine": false},
{"title": "Item 4", "singleLine": true},
{"title": "Item 5", "singleLine": true},
{"title": "Item 6", "singleLine": false},
{"title": "Item 7", "singleLine": true},
{"title": "Item 8", "singleLine": false},
{"title": "Item 9", "singleLine": false},
];
In this case I want to display this in the screen:
Item 1
Item 2 Item 3
Item 4
Item 5
Item 6
Item 7
Item 8 Item 9
How can I do this?
2
Answers
I think there’s no specific widget that does this, but it can be fulfilled by using some logic.
Look at the following ListView’s builder:
and here’s the result:
Hope it helps you.
A faster way to implement this would be to just create a 2 dimensional Widget List using a either Text widgets or Row widgets, then setting that as the "children" property of a Column widget as follows:
It just traverses the list one time and creates the page using the simplest and fastest built widgets.