6

I want to display a list of widgets with variable sizes.

Flutter's Wrap is a great fit for my layout needs.

final List<dynamic> someItems; 

return SingleChildScrollView(
  child: Wrap(
    children: someItems.map((item) => _createTile(context, item)).toList(),
  ),
  scrollDirection: Axis.vertical,
);

enter image description here

But a fatal problem, Wrap can't create widgets lazily. For example, if there are less than 100 Tile widgets displayed in the image above, but the number of data list is more, Wrap creates even invisible widgets.

// The result of taking a log once for each item when configuring Wrap with a list of 1000 data.
I/flutter ( 4437): create Widget 0
I/flutter ( 4437): create Widget 1
I/flutter ( 4437): create Widget 2
I/flutter ( 4437): create Widget 3
I/flutter ( 4437): create Widget 4
I/flutter ( 4437): create Widget 5
I/flutter ( 4437): create Widget 6
I/flutter ( 4437): create Widget 7
I/flutter ( 4437): create Widget 8
...
I/flutter ( 4437): create Widget 999

ListView and GridView create widgets lazily, but (at least as far as I currently know) they are not free to lay out widgets like Wrap.

Is there a way to implement the layout I want?

H.Kim
  • 525
  • 4
  • 14

1 Answers1

2

Try this:

Wrap(
  children: someItems.map((item) => _createTile(context, item)).toList().cast<Widget>(),
)
Crazzygamerr
  • 346
  • 2
  • 11