1

Look as you can see here the message box at the bottom should crop to the content but it doesn't as the word doesn't fit so just goes to next line but message box doesn't update.

enter image description here

heres my current code the max width is 0.7 but yeah it should be able to have a lower limit like 0.5 right.

return Padding(
  padding: EdgeInsets.only(
    top: sameUserAsNext ? 4.0 : 10.0,
    bottom: 4.0,
    left: 10.0,
    right: 10.0,
  ),
  child: Align(
    alignment: isCurrentUser ? Alignment.centerRight : Alignment.centerLeft,
    child: Container(
      padding: EdgeInsets.symmetric(vertical: 10, horizontal: 15),
      decoration: BoxDecoration(
        color: isCurrentUser
            ? const Color.fromARGB(255, 68, 129, 235)
            : Colors.grey[800],
        borderRadius: BorderRadius.circular(25),
      ),
      child: ConstrainedBox(
        constraints: BoxConstraints(
          maxWidth: MediaQuery.of(context).size.width * 0.7,
        ),
        child: Text(
          message.text,
          style: TextStyle(
            color: Colors.white,
            fontSize: messageFontSize,
            height: 1.4,
          ),
          overflow: TextOverflow.visible,
          softWrap: true,
          maxLines: null,
          textAlign: TextAlign.left,
        ),
      
                      ),
                    ),
                  ),
                );
              },
            ),
          ),

3 Answers 3

0

You can fix that by adding this property to your Text widget:

textWidthBasis: TextWidthBasis.longestLine

Your code should look something like this:

Text(
          message.text,
          style: TextStyle(
            color: Colors.white,
            fontSize: messageFontSize,
            height: 1.4,
          ),
          overflow: TextOverflow.visible,
          softWrap: true,
          maxLines: null,
          textAlign: TextAlign.left,
          textWidthBasis: TextWidthBasis.longestLine,
        )

So instead of having this:
Without TextWidthBasis.longestLine

It will look like this:
With TextWidthBasis.longestLine

Hope this help!

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

2 Comments

This solves the issue nicely I never knew about that, thankyou
@MatthewWarner I'm glad this helped you! Please consider marking this answer as accepted so that other users can find it more easily.
0
 minWidth: MediaQuery.of(context).size.width * 0.5,
 maxWidth: MediaQuery.of(context).size.width * 0.7,
//use minwidth looks great for the text of short messages 

Comments

0

because you are using ConstrainedBox just to limited the mxaWidth , but minwidth or intrinsic behavior ,

solution simply is using IntrinsicWidth insinde Align or Contianier like for exaple :

return Padding(
  padding: EdgeInsets.only(
    top: sameUserAsNext ? 4.0 : 10.0,
    bottom: 4.0,
    left: 10.0,
    right: 10.0,
  ),
  child: Align(
    alignment: isCurrentUser ? Alignment.centerRight : Alignment.centerLeft,
    child: ConstrainedBox(
      constraints: BoxConstraints(
        maxWidth: MediaQuery.of(context).size.width * 0.7,
        minWidth: MediaQuery.of(context).size.width * 0.1, 
      ),
      child: Container(
        padding: EdgeInsets.symmetric(vertical: 10, horizontal: 15),
        decoration: BoxDecoration(
          color: isCurrentUser
              ? const Color.fromARGB(255, 68, 129, 235)
              : Colors.grey[800],
          borderRadius: BorderRadius.circular(25),
        ),
        child: Text(
          message.text,
          style: TextStyle(
            color: Colors.white,
            fontSize: messageFontSize,
            height: 1.4,
          ),
          softWrap: true,
          overflow: TextOverflow.visible,
        ),
      ),
    ),
  ),
);

tell me if that works well .

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.