Home > Net >  How to fit Text to container without using FittedBox
How to fit Text to container without using FittedBox

Time:02-27

When using FittedBox, the text size gets very small compared to the text length. I don't want the text to shrink. Instead I want the text to stay the same size and fit only the part that can fit.

Container(
 width: 250,
 child: Text("Data will end with ... instead of shrinking..."),),

CodePudding user response:

For your text to work this way, you should set how text behaves in case of overflow.

What you need to do, is set the overflow property to TextOverflow.ellipsis:

Text(
  'Hello, Test! How are you?',
  overflow: TextOverflow.ellipsis,
)

It is provided in the Flutter documentation of the Text widget: https://api.flutter.dev/flutter/widgets/Text-class.html.

Also, check other text overflow options: https://api.flutter.dev/flutter/painting/TextOverflow.html

CodePudding user response:

You can use Flexible

Container(
  child: Row(
  children: [
    Flexible(child: Text("A looooooooooooooooooong text"))
  ],
 ),
),

For more info about Flexible check https://api.flutter.dev/flutter/widgets/Flexible-class.html

  • Related