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

别等时光非礼了梦想. 提交于 2020-01-05 08:10:29

问题


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,
  ),
),

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?


回答1:


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:




回答2:


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:




回答3:


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,
  )


来源:https://stackoverflow.com/questions/58298559/how-to-design-a-text-widget-that-wraps-automatically-in-flutter

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!