1

I'm making a list with some tiles containing texts, but they can't be big enough that it will break the layout. So I take only a substring from the text and add '...' to them to indicate there's more text hidden by the layout. Problem is, some texts are in caps lock and that makes the text have different sizes. Here is the sample code for the tile:

class _MessageTileState extends State<MessageTile> {
  @override
  Widget build(BuildContext context) {
    final colorGrey = Theme.of(context).unselectedWidgetColor;
    final screenSize = MediaQuery.of(context).size;

    return Row(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: <Widget>[
        messageIcon(widget.message),
        SizedBox(width: 16.0),
        Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: <Widget>[
            Text(
              widget.message.author.length > 38 ? widget.message.author.substring(0, 35) + '...' : widget.message.author,
              style: TextStyle(
                fontSize: screenSize.width * 0.033,
                fontWeight: FontWeight.w600,
                height: 1.6,
                color: widget.message.read ? colorGrey : null,
              ),
            ),
            Text(
              widget.message.title.length > 38 ? widget.message.title.substring(0, 35) + '...' : widget.message.title,
              style: TextStyle(
                fontSize: screenSize.width * 0.033,
                fontWeight: FontWeight.w600,
                height: 1.6,
                color: widget.message.read ? colorGrey : null,
              ),
            ),
            Text(
              widget.message.message.length > 38 ? widget.message.message.substring(0, 35).replaceAll('\n\n', ' ') + '...' : widget.message.message.replaceAll('\n\n', ' '),
              style: TextStyle(
                fontSize: screenSize.width * 0.033,
                height: 1.6,
                color: widget.message.read ? colorGrey : null,
              ),
            ),
            Text(
              DateFormat('MM/dd/yyyy - h:mm a').format(widget.message.date),
              style: TextStyle(
                fontSize: screenSize.width * 0.033,
                fontWeight: FontWeight.w600,
                color: colorGrey,
                height: 1.6,
              ),
            ),
          ],
        ),
      ],
    );

And here is a image showing the problem:

enter image description here

I want the text to occupy the same amount of space, regardless of being in uppercase or lowercase. (Making a simple check for upper or lower case doesn't work cause "there ARE TEXTS like THIS on those TILES" - different amount of lowercase and uppercase letters).

PS: Monospace font didn't work.

1 Answer 1

1

Do not apply substring or anything.

Please try a Text() property called overflow and maxLines

Text(
   "your long very long text",
   maxLines: 1,
   overflow: TextOverflow.ellipsis
)

UPDATE:

You should reestructure some things also.

Inside your Row, try using only 2 Widgets. One Container and one Expanded

Row(
  crossAxisAlignment: CrossAxisAlignment.start,
  children: <Widget>[
     Container(
        child: messageIcon(widget.message)
     ),
     Expanded(
        child: Column(
         // ... all your column code
        )
     ),
  ],
),

Documentation for: TextOverflow enum

Sign up to request clarification or add additional context in comments.

5 Comments

All tiles are overflowing now and it's creating numerous lines, I want just one line with '...' : A RenderFlex overflowed by 52 pixels on the right. The relevant error-causing widget was Row
also add the maxLines: 1
that solved the number of lines, but it's still giving overflow error for all tiles
I wrapped the Text widget with a container and passed a width size based on screen width. This is giving me better results, although it's not perfect. Thanks for the help. Result: i.imgur.com/P51tP3e.png
I updated my answer. Check. Try to avoid using the "based on screen width". In this case you can get the desired results without it.

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.