skip to Main Content

I have a Container inside a list view builder and I want this container to be dynamic in size according to the size of child of container (here textwidget), so I used expanded widget for container but it gave an error:

incorrect use of ParentDataWidget.

List<String> values = [
    "short text",
    "long textlong textlong textlong textlong textlong textlong textlong textlong textlong textlong textlong textlong textlong textlong textlong textlong textlong text"
  ];
body: ListView.builder(
      shrinkWrap: true,
      itemCount: values.length,
      itemBuilder: (context, index) {
        return Expanded(
          child: Container(
              margin: EdgeInsets.all(10),
              decoration: BoxDecoration(
                color: Colors.white,
                borderRadius: BorderRadius.circular(20),
                boxShadow: const [
                  BoxShadow(
                    color: Colors.grey,
                    blurRadius: 2.0,
                    spreadRadius: 0.0,
                    offset: Offset(2.0, 2.0),
                  ),
                ],
              ),
              child: Text(values[index])),
        );
      }),

I want the container to expand according to the size of text in list of strings.

2

Answers


  1. Try this :

    List<String> values = [
        "short text",
        "long textlong textlong textlong textlong textlong textlong textlong textlong textlong textlong textlong textlong textlong textlong textlong textlong textlong text"
      ];
     body: ListView.builder(
              shrinkWrap: true,
              itemCount: values.length,
              itemBuilder: (context, index) {
                return Container(
                      margin: EdgeInsets.all(10),
                      decoration: BoxDecoration(
                        color: Colors.white,
                        borderRadius: BorderRadius.circular(20),
                        boxShadow: const [
                          BoxShadow(
                            color: Colors.grey,
                            blurRadius: 2.0,
                            spreadRadius: 0.0,
                            offset: Offset(2.0, 2.0),
                          ),
                        ],
                      ),
                      child: Text(values[index]));
              }),
    
    Login or Signup to reply.
  2. just remove Expanded and give some padding to the container. like below

    ListView.builder(
        shrinkWrap: true,
        itemCount: values.length,
        itemBuilder: (context, index) {
          return Container(
              margin: EdgeInsets.all(10),
              padding: EdgeInsets.all(10),
              decoration: BoxDecoration(
                color: Colors.white,
                borderRadius: BorderRadius.circular(20),
                boxShadow: const [
                  BoxShadow(
                    color: Colors.grey,
                    blurRadius: 2.0,
                    spreadRadius: 0.0,
                    offset: Offset(2.0, 2.0),
                  ),
                ],
              ),
              child: Text(values[index]));
        }),
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search