Martin Zhu
Martin Zhu

Reputation: 441

How to design a Text Widget that wraps automatically in flutter?

I want to design a text widget that wraps automatically, and it uses an ellipsis when the text length exceeds the maximum.

The following code can achieve a similar effect.

Container(
  color: Colors.red,
  child: Text(
    'DENTIFICATIONDENTIFICATIONDENTIFICATIONDENTIFICATIONDENTIFICATIONDENTIFICATIONDENTIFICATIONDENTIFICATIONDENTIFICATIONDENTIFICATIONDENTIFICATIONDENTIFICATIONDENTIFICATIONDENTIFICATIONDENTIFICATIONDENTIFICATIONDENTIFICATIONDENTIFICATIONDENTIFICATIONDENTIFICATION',
    overflow: TextOverflow.ellipsis,
    maxLines: 6,
  ),
),

enter image description here

But it must specify maxLines, I don't want this, I need the widget to automatically set the maxLines based on the size of parent.

How can I improve the code?

Upvotes: 1

Views: 715

Answers (3)

Pablo Barrera
Pablo Barrera

Reputation: 10963

There are some known issues with Text wrapping, specially with the ellipsis if you don't specify the maxLines: Flexible text overflow ellipsis (softWrap false)

But, you could do something like this to determine the maxLines according to the parent widget:

SizedBox(
  height: 40.0,
  child: LayoutBuilder(
    builder: (BuildContext context, BoxConstraints constraints) {
      double fontSize = 16.0;
      return Text(
        "DENTIFICATIONDDENTIFICATIONDDENTIFICATIONDDENTIFICATIONDDENTIFICATIONDDENTIFICATIONDDENTIFICATIONDDENTIFICATIONDDENTIFICATIONDDENTIFICATIONDDENTIFICATIONDDENTIFICATIONDDENTIFICATIONDDENTIFICATIONDDENTIFICATIOND",
        style: TextStyle(fontSize: fontSize, height: 1.0),
        maxLines: constraints.maxHeight ~/ fontSize,
        overflow: TextOverflow.ellipsis,
      );
    },
  )

If you know the height of the parent, you could do something like this:

double fontSize = 16.0;
double height = 40.0;

SizedBox(
  height: height,
  child: Text(
    "DENTIFICATIONDDENTIFICATIONDDENTIFICATIONDDENTIFICATIONDDENTIFICATIONDDENTIFICATIONDDENTIFICATIONDDENTIFICATIONDDENTIFICATIONDDENTIFICATIONDDENTIFICATIONDDENTIFICATIONDDENTIFICATIONDDENTIFICATIONDDENTIFICATIOND",
    style: TextStyle(fontSize: fontSize, height: 1.0),
    maxLines: parentHeight ~/ fontSize,
    overflow: TextOverflow.ellipsis,
  )

Upvotes: 0

CopsOnRoad
CopsOnRoad

Reputation: 268404

You can try this approach:

@override
Widget build(BuildContext context) {
  String longText = "Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum";
  double size = 100, fontSize = 16; 

  return Scaffold(
    appBar: AppBar(),
    body: Padding(
      padding: const EdgeInsets.all(20.0),
      child: SizedBox(
        height: size,
        child: Text(
          longText,
          style: TextStyle(fontSize: fontSize, height: 1),
          overflow: TextOverflow.ellipsis,
          maxLines: size ~/ fontSize,
        ),
      ),
    ),
  );
}

Output:

enter image description here

Upvotes: 3

CopsOnRoad
CopsOnRoad

Reputation: 268404

Simply remove maxLines from your code, and your parent widget will handle it accordingly assuming the fact the constraints passed to Text are bounded, for instance in this example, I used SizedBox with height: 100, and output is exactly what you want:

Edit:

SizedBox(
  height: 100,
  child: Text(
    'DENTIFICATIOND...DENTIFICATIOND...DENTIFICATIOND...DENTIFICATIOND...DENTIFICATIOND...DENTIFICATIOND...DENTIFICATIOND...DENTIFICATIOND...DENTIFICATIOND...DENTIFICATIOND...',
    overflow: TextOverflow.ellipsis,
    maxLines: 1000, // you can freely use any value here
  ),
)

Output:

enter image description here

Upvotes: 0

Related Questions