0

ive been using flutter for past few months and now ive stuck in a problem where i get a list of tag names and i have to design a ui to show the tags to user as the image given below. tags are those which are marked in orange,

when i use ListViewBuilder and Row to display the tags as it renders all the widgets in the stragint line i get renderfelx overflow error as shown in the image below..

This is how my code looks-

                   Container(
                              height: 40,
                              child: ListView.builder(
                                shrinkWrap: true,
                                itemCount: brandmodels[index].tags.length,
                                itemBuilder: (context, index0) {
                                  return Row(
                                    children: [
                                      Padding(
                                        padding: const EdgeInsets.all(8.0),
                                        child: CircleAvatar(
                                            radius: 05,
                                            backgroundColor: Color(0xffC4C4C4)),
                                      ),
                                      Padding(
                                        padding:
                                            const EdgeInsets.only(right: 8.0),
                                        child: Expanded(
                                          child: Text(
                                            brandmodels[index].tags[index0],
                                          ),
                                        ),
                                      ),
                                    ],
                                  );
                                },
                              ),
                            )

So how to bring the widgets to next line when it hits the maximum width of the screen?.

Arvind suresh
  • 221
  • 1
  • 3
  • 12

1 Answers1

3

I just changed ListView to Wrap widget.
(To check whether there is overflow issue, I set Container's width to 300.
And give a background color for checking Container area.)

enter image description here

import 'package:flutter/material.dart';

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
        visualDensity: VisualDensity.adaptivePlatformDensity,
      ),
      home: MyHomePage(title: 'Flutter Demo Home Page'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  MyHomePage({Key key, this.title}) : super(key: key);

  final String title;

  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  @override
  void initState() {
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: _buildBody(),
      floatingActionButton: FloatingActionButton(
        onPressed: () {},
        tooltip: 'Increment',
        child: Icon(Icons.add),
      ),
    );
  }

  Widget _buildBody() {
    List data = ['biscuits', 'cakes', 'drinks', 'snacks', 'food'];

    return Container(
      color: Colors.yellow[100],
      height: 60,
      width: 300,
      child: Wrap(
        direction: Axis.horizontal,
        children: data.map((item) {
          return Row(
            mainAxisSize: MainAxisSize.min,
            children: [
              Padding(
                padding: const EdgeInsets.all(8.0),
                child: CircleAvatar(
                    radius: 05, backgroundColor: Color(0xffC4C4C4)),
              ),
              Padding(
                padding: const EdgeInsets.only(right: 8.0),
                child: Expanded(
                  child: Text(
                    item,
                  ),
                ),
              ),
            ],
          );
        }).toList(),
      ),
    );
  }
}
KuKu
  • 6,654
  • 1
  • 13
  • 25