如何设置Flutter文本最大长度值?

问题描述 投票:0回答:2

我正在使用Flutter编程。我需要显示标题和标题旁边的按钮。如果文本长度较小,则该按钮也应显示在文本旁边。如果文本长度更多,我想将文本长度固定为最大200像素,并需要在标题旁边显示按钮。

我正在使用以下代码。和使用较少的文本长度可以正常工作

问题是:如果文本长度更大,则我的按钮移出了屏幕。您可以在下面的图像中看到该行为。

          Row(
            children: <Widget>[
                    Container(
                      child: RichText(
                        overflow: TextOverflow.ellipsis,
                        strutStyle: StrutStyle(fontSize: 12.0),
                        text: TextSpan(
                            style: TextStyle(color: Colors.white), text: model.title),
                      ),
                    ),

                    Text("My Button"),
                  ],
                )

enter image description here

flutter flutter-layout
2个回答
0
投票

您可以使用扩展或为富文本容器提供固定宽度,例如使用扩展小部件:-

Row(
                            children: <Widget>[
                              Expanded(
                                child: RichText(
                                  overflow: TextOverflow.ellipsis,
                                  strutStyle: StrutStyle(fontSize: 12.0),
                                  text: TextSpan(
                                      style: TextStyle(color: Colors.white), text: model.title),
                                ),
                              ),

                              Text("My Button"),
                            ],
                          )

0
投票

我认为maxLengthRichText小部件没有Text属性。如果使用RichText类,则将具有可以使用的maxLines属性。我尝试重新创建案例,并用RichText小部件包装Flexible小部件,使子小部件仅使用最小的可用空间。但是我无法复制您提到的错误。我使用的长文本为ellipsis,并且button不会超出屏幕。请注意,我还使用了maxLines属性,如果将其设置为2,则将长文本换行而不换掉button,长文本将显示为ellipsis。下面的示例工作代码:

body: Center(
          child: Row(
            children: <Widget>[
              Container(
                child: Flexible(
                  child: RichText(
                    maxLines: 2,
                    overflow: TextOverflow.ellipsis,
                    strutStyle: StrutStyle(fontSize: 12.0),
                    text: TextSpan(
                        style: TextStyle(color: Colors.white), text: 'I am using Flutter programming. I need to display a title and a button next to the title. If the text length is less button also should display next to the text. If text length is more i want to fix the text length as maximum 200px and need to show the button next to the title.'),
                  ),
                )

              ),

              Text("My Button"),
            ],
          )
        )

enter image description here

如果将maxLines设置为1,则长文本将以省略号引起来。

希望这会有所帮助。

© www.soinside.com 2019 - 2024. All rights reserved.