1

I want to design a combined Text widget that consists of two Text widget.

The size of the first Text widget changes as the content changes, and when it reaches the maximum length, an ellipsis is displayed. Like below, the red line part is fixed.

Then this is my code:

Row(
  children: <Widget>[
    Expanded(
      child: Text(
        'W',
        overflow: TextOverflow.ellipsis,
      ),
    ),
    Text(
      '(0x1234…1234)',
      style: TextStyle(
        fontSize: 12,
        color: Colors.black,
      ),
    ),
  ],
);

When the length is the largest, the display is normal. But when the length is very small, there will be a blank in the middle.

So, how do I need to improve my code?

3 Answers 3

4

You need to use Flexible widget instead of the Expanded.

Row(
  children: <Widget>[
    Flexible(
      child: Text(
        'Was',
        overflow: TextOverflow.ellipsis,
      ),
    ),
    Text(
      '(0x1234…1234)',
      style: TextStyle(
        fontSize: 12,
        color: Colors.black,
      ),
    ),
  ],
),

Using a Flexible widget gives a child of a Row, Column, or Flex the flexibility to expand to fill the available space in the main axis.

While Expanded, forces the child to expand to fill the available space.

You can also pass the fit: FlexFit.tight [The child is forced to fill the available space] or fit: FlexFit.loose [The child can be at most as large as the available space (but is allowed to be smaller).] to Flexible widget.

By Default it is set to FlexFit.loose.

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

1 Comment

Thank you for your help, this is the effect I want.
1

You should use Flexible instead of Expanded

Row(
  children: <Widget>[
    Flexible(
      child: Text(
        'W',
        overflow: TextOverflow.ellipsis,
      ),
    ),
    Text(
      '(0x1234…1234)',
      style: TextStyle(
        fontSize: 12,
        color: Colors.black,
      ),
    ),
  ],
)

Comments

0

Not sure if I got your question properly, do you want to remove the white space?

Row(
   children: <Widget>[
     Expanded(
       child: Align( // add this
         heightFactor: 1,
         alignment: Alignment.centerRight, // this is what you need
         child: Text(
           'W',
           overflow: TextOverflow.ellipsis,
         ),
       ),
     ),
     Text(
       '(0x1234…1234)',
       style: TextStyle(
         fontSize: 12,
         color: Colors.black,
       ),
     ),
   ],
 )

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.