Flutter's Listview doesn't have word wrapping like Wrap?
Asked Answered
B

1

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?

Blinking answered 25/12, 2019 at 11:25 Comment(4)
listen scrollposition then load more items easily, doesnt work?Vivyan
I think you should try with Grid.builder(). It might help you to achieve something similar but not the same as you're looking for.Partan
@OMiShah Do you mean GridView.Builder? I have already used it with SliverGridDelegateWithFixedCrossAxisCount.Blinking
You can refer to this answer.Clambake
K
2

Try this:

Wrap(
  children: someItems.map((item) => _createTile(context, item)).toList().cast<Widget>(),
)
Kagera answered 10/8, 2020 at 13:33 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.