如何设计一个自动在颤动中自动换行的Text Widget?

时间:2019-10-09 06:53:32

标签: flutter dart flutter-layout

我想设计一个自动包装的文本小部件,当文本长度超过最大长度时,它会使用省略号。

以下代码可以达到类似的效果。

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

enter image description here

但是它必须指定maxLines,我不希望这样,我需要小部件根据父项的大小自动设置maxLines

如何改进代码?

3 个答案:

答案 0 :(得分:1)

您可以尝试这种方法:

@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,
        ),
      ),
    ),
  );
}

输出:

enter image description here

答案 1 :(得分:0)

只需从代码中删除maxLines,并且假设传递给Text的约束是有界的,则您的父窗口小部件将相应地对其进行处理,例如,在本示例中,我使用了SizedBox height: 100,输出正是您想要的:

编辑:

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

输出:

enter image description here

答案 2 :(得分:-1)

文本换行有一些已知的问题,特别是省略号,如果您未指定maxLines:Flexible text overflow ellipsis (softWrap false)

但是,您可以执行以下操作根据父窗口小部件确定maxLines:

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

如果您知道父母的身高,则可以执行以下操作:

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