So, I have a Widget that builds a message (imagine a typical WhatsApp message dialog, or what one can call a message bubble).
Widget _buildMessage(String text, String time) {
return Padding(
padding: EdgeInsets.symmetric(vertical: 7.5),
child: Column(
mainAxisSize: MainAxisSize.min,
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Row(
crossAxisAlignment: CrossAxisAlignment.end,
children: <Widget>[
SizedBox(width: 10),
ClipRRect(
borderRadius: BorderRadius.only(
topLeft: Radius.circular(20),
topRight: Radius.circular(20),
bottomRight: Radius.circular(20),
bottomLeft: Radius.circular(20)
),
child: Container(
color: Colors.lightBlue,
padding: EdgeInsets.all(17),
width: MediaQuery.of(context).size.width * 0.3,
child: Text(
text,
style: TextStyle(
color: Colors.white,
height: 1.2,
),
),
),
),
],
),
Padding(
padding: EdgeInsets.only(left: 20),
child: Text(
time,
style: TextStyle(
color: Colors.grey,
height: 1.5,
fontSize: 8,
),
),
),
],
),
);
}
To this, I feed the text using a simple call:
_buildMessage("Yep, sure!","20 minutes ago",)
Now, the problem here is that the child Container I have right now has a fixed width being determined by:
width: MediaQuery.of(context).size.width * 0.3,
This is problematic, because it does not essentially wrap around the text. Rather, for small texts such as hi
or hello
, the chat bubble is excessively wide. It does work normally for longer bits of text, but one cannot assume that it will always be the case.
Therefore, could you please advice me regarding how to best achieve this media-query so that the width has a max value set at width: MediaQuery.of(context).size.width * 0.3,
, but also falls down to wrap the text when it is smaller?