3

I want to show a list of elements, which have a picture, a title and some additional information in Flutter. My layout is supposed to look like this:

desired layout

I have implemented this using a ListView with Card children. I then use a Row to have the image on the left and the info on the right. Then I use a Column to have my title on top and the rest of the info below.

My problem is with the title, which may be too large for width of its container. What I originally thought was letting the user scroll through the text, but I'm not sure if it is possible to nest two scrolling-enabled widgets in Flutter, so I settled with using TextOverflow.ellipsis. However in my basic setup TextOverflow.ellipsis has no effect, and instead I face an overflow error.

After looking this up online I figured that I had to put my Text into an Expanded or Flexible, which themselves had to be a direct child of a Flex (I used Row). However this wasn't enough still, as I got the following error: RenderFlex children have non-zero flex but incoming width constraints are unbounded.. To fix this I had to add a Container as parent of my Row (Flex) with a fixed width.

Here is my current code:

Card(
  color: backgroundColor,
  child: Row(
    children: <Widget>[
      MyImageWidget(),
      Container(
        height: 60,
        child: Column(
          children: <Widget>[
            Container(
              padding: EdgeInsets.symmetric(vertical: 4, horizontal: 12),
              height: 30,
              width: 340, // I don't want this
              child: Row(
                children: [
                  Expanded(
                    child: Text(
                      "Title which may be long, very very long",
                      overflow: TextOverflow.ellipsis,
                      style: TextStyle(
                          fontWeight: FontWeight.bold,
                          fontSize: 16,
                    ),
                  ),
                ],
              ),
            ),
          ],
        ),
      ),
    ],
  ),
);

Is there a way to get rid of this fixed width constraint? I know that I could use Media Queries but I don't want to do this unless I absolutely have to, because I might change the padding of several elements, or even move to a grid view on larger screens.

Or if I absolutely have to set this fixed width, is there a way I could ask Flutter to find out what the width of my Container would be if it had no content, and then apply it as fixed width of itself?

1 Answer 1

3

You just need to wrap your right-side Column inside an Expanded widget to let it take all the remaining available space inside the Row and the TextOverflow.ellipsis will take care of the rest.

return Card(
      color: backgroundColor,
      child: Row(
        children: <Widget>[
          MyImageWidget(),
          Expanded(
            child: Padding(
              padding: const EdgeInsets.symmetric(
                vertical: 4,
                horizontal: 12,
              ),
              child: Column(
                children: <Widget>[
                  Text(
                    "Title which may very very very very very very",
                    overflow: TextOverflow.ellipsis,
                    style: TextStyle(
                      fontWeight: FontWeight.bold,
                      fontSize: 16,
                    ),
                  ),
                ],
              ),
            ),
          ),
        ],
      ),
    );
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.