Robert Benedetto
Robert Benedetto

Reputation: 1710

Container text wrap

I am trying to create chat bubbles, and have found a snippet that I am trying to modify. I have come pretty far but can't get the bubbles to wrap around the text. I did find Wrap text in container without using a fixed width in Flutter and followed the ideas there, but they didn't work for me. Can anyone provide some insight?

The image shows what I'm getting with the below code, but as mentioned, I want the bubbles to wrap around the text and not fully extend to the edges unless required.

enter image description here

class Bubble extends StatelessWidget {
  Bubble(
      {this.author, this.message, this.time, this.delivered, this.isEmployee});
  final String message, time, author;
  final delivered, isEmployee;
  @override
  Widget build(BuildContext context) {
    final rowAlignment =
        isEmployee ? MainAxisAlignment.start : MainAxisAlignment.end;
    final bg =
        isEmployee ? Colors.greenAccent.shade100 : Colors.blueAccent.shade100;
    final align =
        isEmployee ? CrossAxisAlignment.start : CrossAxisAlignment.end;
    final icon = delivered ? Icons.done_all : Icons.done;
    final radius = isEmployee
        ? BorderRadius.only(
            topRight: Radius.circular(5.0),
            bottomLeft: Radius.circular(10.0),
            bottomRight: Radius.circular(5.0),
          )
        : BorderRadius.only(
            topLeft: Radius.circular(5.0),
            bottomLeft: Radius.circular(5.0),
            bottomRight: Radius.circular(10.0),
          );
    return Column(
      crossAxisAlignment: align,
      children: <Widget>[
        Container(
          margin: const EdgeInsets.all(3.0),
          padding: const EdgeInsets.all(8.0),
          decoration: BoxDecoration(
            boxShadow: [
              BoxShadow(
                  blurRadius: .5,
                  spreadRadius: 1.0,
                  color: Colors.black.withOpacity(.12))
            ],
            color: bg,
            borderRadius: radius,
          ),
          child: Column(
            crossAxisAlignment: align,
            children: <Widget>[
              Text(author, style: TextStyle(fontWeight: FontWeight.bold)),
              Text(message),
              Row(mainAxisAlignment: rowAlignment, children: <Widget>[
                Text(time,
                    style: TextStyle(
                      color: Colors.black38,
                      fontSize: 10.0,
                    )),
                SizedBox(width: 3.0),
                Icon(
                  icon,
                  size: 12.0,
                  color: Colors.black38,
                )
              ])
            ],
          ),
        )
      ],
    );
  }
}

Upvotes: 1

Views: 225

Answers (2)

Jordan Davies
Jordan Davies

Reputation: 10861

You could set mainAxisSize on your Row to ManAxisSize.min

Row(
  mainAxisSize: MainAxisSize.min,
  mainAxisAlignment: rowAlignment,
  children: <Widget>[
    Text(
      time,
      style: TextStyle(
        color: Colors.black38,
        fontSize: 10.0,
      ),
    ),
    SizedBox(width: 3.0),
    Icon(
      icon,
      size: 12.0,
      color: Colors.black38,
    ),
  ],
);

Upvotes: 0

anmol.majhail
anmol.majhail

Reputation: 51206

Wrap your- first Column in FittedBox widget.

FittedBox(
      fit: BoxFit.contain,
      child: Column(
       ....

Upvotes: 1

Related Questions