Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
212 views
in Technique[技术] by (71.8m points)

How to solve for 'text overflow.ellipse' in flutter?

I am unable to make my text stop extending the screen and overflowing. My code is as follows:

class OrderTileDisplay extends StatelessWidget {
  final MenuOrderData orderItem;
  final editable;
  OrderTileDisplay(this.orderItem, {this.editable = true});

  @override
  Widget build(BuildContext context) {
    return Column(
      crossAxisAlignment: CrossAxisAlignment.stretch,
      children: [
        GestureDetector(
          onTap: (){},
          child: Container(
            color: Colors.transparent,
            margin: EdgeInsets.symmetric(vertical: 4),
            child: Row(
              mainAxisAlignment: MainAxisAlignment.spaceBetween,
              children: [
                Row(
                  children: [
                    Text(
                      orderItem.quantity.toString() + 'x',
                      style: Title16,
                    ),
                    SizedBox(
                      width: 8,
                    ),
                    Text(
                      orderItem.name, // OVERFLOWS WHEN IT IS A LONGER SENTENCE
                      style: Title18bold,
                      overflow: TextOverflow.ellipsis,
                    ),
                  ],
                ),
                Row(
                  children: [
                    editable? Icon(Icons.edit, color: Colors.grey[200], size: 20,) : Container(),
                    SizedBox(width: 8,),
                    Text("£" + Provider.of<CP>(context).getTotalForItem(orderItem).toStringAsFixed(2),
                      style: Title18bold,),
                  ],
                ),
              ],
            ),
          ),
        ),
Container(... and other stuff)

I have tried putting Text inside a container then Expanded widget but I keep getting errors. Not sure how to solve it.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Try to wrap your Row and Text inside a Flexible:

Flexible( //flexible here
  child: Row(
    children: [
      Text(
        orderItem.quantity.toString() + 'x',
        style: Title16,
      ),
      SizedBox(
        width: 8,
      ),
      Flexible( //and here
        child: Text(
          orderItem.name, // no more overflow
          style: Title18bold,
          overflow: TextOverflow.ellipsis, //working as expected
        ),
      ),
    ],
  ),
),

As the documentation states, TextOverflow says how overflowing text should be handled, but it will not work if it's parent don't have a fixed size. In this case, Flexible is been used to restrict the total size of the text to prevent overflow, when it reaches a very large width.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...