Search code examples
flutterdartlistview

Flutter: FlexLayoutManager similar to the one in native android


So I'm trying to implement a flex listview similar to the google FlexLayoutManager to the horizontal axis with wrap, you can see an example in the wrap section

https://github.com/google/flexbox-layout/raw/main/assets/flex-wrap.gif

here is my code:

ListView.builder(
      scrollDirection: Axis.horizontal,
      itemCount: categories.length,
      padding: EdgeInsetsDirectional.only(start: 16, end: 12),
      itemBuilder: (context, index) {
        return InkWell(
          onTap: (){
          },
          child: Container(
            height: 24,
            alignment: Alignment.center,
            child: Text(
                categories[index].name,
            ),
          ),
        );
      },
    )

what I have to add to my listview to make the items wrap and move to the next row if they exceeded the screen width?


Solution

  • Not tested, but as I indicated in my comment, I'd use something like:

    Wrap(
      children: [
        for (final c in categories)
          InkWell(child: Text(c.name)),
        ]);
    

    adding of course all your parameters, and the extra container.