3

I have a sample layout as following:

  1. 3 containers of fixed size (red, blue, green, container labels are height x width)
  2. a text widget that may have variable size (based on the text content):

enter image description here

The overall height of the widget is 300px. The layout is roughly as follows:

Row(
    [
        red,
        Column(
            [
                Row([green, text]),
                Spacer(),
                blue,
            ]
        ),
    ]
)

The problem is if I increase the text length, it overflows the screen as follows:

enter image description here

The text has already been styled to be maxLines:1 and overflow:TextOverflow.ellipsis. Moreover, I have tried adding Expanded at various levels (to the text widget, to the row containing it, to the column containing it, to the overall outer box), but no avail.

Can you please suggest how to go about this?

Here's the source code:

import 'package:flutter/material.dart';

class SampleWidget extends StatelessWidget {
  const SampleWidget({Key? key}) : super(key: key);

  Container getContainer({h, w, color}) => Container(
        color: color,
        height: h,
        width: w,
        child: Center(
          child: Text(
            '$h x $w',
            textAlign: TextAlign.center,
          ),
        ),
      );

  Text getText(text) => Text(
        text,
        textAlign: TextAlign.center,
        overflow: TextOverflow.ellipsis,
        maxLines: 1,
      );

  Widget applyBorder(widget) => Container(
        decoration: BoxDecoration(
          border: Border.all(
            color: Colors.white,
            width: 1,
          ),
        ),
        child: widget,
      );

  @override
  Widget build(BuildContext context) {
    double overallHeight = 300;

    var red = getContainer(h: overallHeight, w: 150, color: Colors.red);
    var green = getContainer(h: 100, w: 60, color: Colors.green);
    var blue = getContainer(h: 100, w: 150, color: Colors.blue);
    var text = getText('hello! ' * 10);

    Widget row1 = Row(
      mainAxisSize: MainAxisSize.min,
      mainAxisAlignment: MainAxisAlignment.start,
      children: [green, text],
    );
    row1 = applyBorder(row1);

    Widget col1 = Column(
      mainAxisSize: MainAxisSize.min,
      mainAxisAlignment: MainAxisAlignment.center,
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [row1, Spacer(), blue],
    );
    col1 = applyBorder(col1);
    col1 = SizedBox(height: overallHeight, child: col1);

    Widget row2 = Row(
      mainAxisSize: MainAxisSize.min,
      mainAxisAlignment: MainAxisAlignment.start,
      children: [red, col1],
    );
    row2 = applyBorder(row2);

    return row2;
  }
}

Thanks!

2
  • 1
    Try wrapping your Text widget with Wrap widget or place it in a SizedBox and assign Custom width Commented Aug 4, 2022 at 11:15
  • @Prashant: Wrapping it inside Wrap or Expanded doesn't work. I can't assign custom width since I may not know it beforehand. The red container may also shrink expand based on user data. Thanks! Commented Aug 4, 2022 at 11:20

3 Answers 3

7

Wrap the text with a flexible widget and add overflow if required

Flexible(
 child: Text(
  "Some big text here",
  overflow: TextOverflow.ellipsis,
  maxLines : 3//add any max line here
 )
)

Also wrap the col1's column widget with a flexible so the row gets its bounds

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

6 Comments

Unfortunately, it doesn't work. Thanks!
Wrap col1's column also in a flexible
Thanks that does work (wrapping text and col1 inside Expanded). Care to explain why this work? Thanks!
You had a row - column then again a row. The text in inside the last row. The column has no horizontal bound. So additing a flexible to column made sure that it is bound to the end of the screen the row also is bound till that extent so the text overflow worked
Please edit your answer, I'll be happy to accept it.
|
2

Wrap your Row with Flexible:

Widget row1 = Row(
      mainAxisSize: MainAxisSize.min,
      mainAxisAlignment: MainAxisAlignment.start,
      children: [green, Flexible(child: text)],
    );

Widget row2 = Row(
      mainAxisSize: MainAxisSize.min,
      mainAxisAlignment: MainAxisAlignment.start,
      children: [red, Flexible(child: col1)],
    );

result

Comments

1

Wrap your text into flexible widget and then try to expand some of its properties

    Flexible(
     child: Text(
      'Your text...!',
     )
    )

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.