Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/flutter/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Flutter 如何设计一个文本小部件,自动包装在颤振?_Flutter_Dart_Flutter Layout - Fatal编程技术网

Flutter 如何设计一个文本小部件,自动包装在颤振?

Flutter 如何设计一个文本小部件,自动包装在颤振?,flutter,dart,flutter-layout,Flutter,Dart,Flutter Layout,我想设计一个自动包装的文本小部件,当文本长度超过最大值时,它会使用省略号 下面的代码可以实现类似的效果 容器( 颜色:颜色,红色, 子:文本( “识别识别识别识别识别识别识别识别识别识别识别识别识别识别识别识别识别识别识别识别识别识别识别识别识别识别识别识别识别”, 溢出:TextOverflow.省略号, maxLines:6, ), ), 但是它必须指定maxLines,我不想这样,我需要小部件根据父对象的大小自动设置maxLines 如何改进代码?只需从代码中删除maxLines,并且

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

下面的代码可以实现类似的效果

容器(
颜色:颜色,红色,
子:文本(
“识别识别识别识别识别识别识别识别识别识别识别识别识别识别识别识别识别识别识别识别识别识别识别识别识别识别识别识别识别”,
溢出:TextOverflow.省略号,
maxLines:6,
),
),

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


如何改进代码?

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

编辑:

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
  ),
)
输出:

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

文本换行存在一些已知问题,特别是如果未指定最大行,则省略号:

但是,您可以这样做,根据父窗口小部件确定最大线:

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

您可以尝试以下方法:

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

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

我需要文本自动换行,而不是手动指定
\n
。我使用
\n
只是为了向您展示如果您的文本超过您选择的6行,它会是什么样子。如果不使用
\n
,文本将只显示为一行。好的,收到了,谢谢,请查看更新的代码。谢谢您的帮助,但这仍然不是我想要的,也许我没有说清楚。省略号由文本自动生成,而不是手动生成。当文本超过父控件的最大高度时,请使用省略号而不是多余的文本。但是我不知道父控件的高度,它会根据屏幕的不同而变化。有什么方法可以动态地获取它吗?这就是为什么我问你谁是
文本的父对象,是的,你可以使用
LayoutBuilder
动态地更改它