textspan的文本不会显示出来的话,如果它溢出

发布于 2025-02-04 07:24:53 字数 3598 浏览 2 评论 0原文

That's how it looks when I have a WidgetSpan and a TextSpan, and the text inside the TextSpan is too long:

problem

这里已经提出了一个类似的问题 TextSpan的文本不会显示它是否溢出,并以Widgetspan 放置,但是一个答案不适合我的问题。

我有两个text.rich() widgets(每个主机一个widgetspan和一个textspan> textspan )展开了widget,都带有flex:1的扩展,因为我希望它们的大小相同并尽可能多地占用空间。这就是为什么一排不是一个选项的原因,您(据我所知)不能并排放置两行,这发生了...

使用行

仅使用witchoverflow.ellipsis,如果文本太长:

target ui

< img src =“ https://i.sstatic.net/4ps2b.png” alt =“ target ui”>

这是我的代码:

                Row(
                  children: [
                    Expanded(
                      flex: 1,
                      child: Text.rich(
                        TextSpan(children: [
                          WidgetSpan(
                            child: Container(
                              margin: const EdgeInsets.only(right: 4.0),
                              child: SvgPicture.asset("assets/icons/building.svg", color: 
                                     htLightGrey),
                            ),
                          ),
                          TextSpan(
                            text: incident.building.name,
                            style: const TextStyle(
                              fontWeight: FontWeight.normal,
                              fontSize: 16.0,
                              color: htLightGrey, /*overflow: TextOverflow.ellipsis*/
                            ),
                          ),
                        ]),
                        maxLines: 1,
                        overflow: TextOverflow.ellipsis,
                      ),
                    ),
                    Expanded(
                      flex: 1,
                      child: incident.department != null
                          ? Text.rich(
                              TextSpan(children: [
                                WidgetSpan(
                                  child: Container(
                                    margin: const EdgeInsets.only(right: 4.0),
                                    child: SvgPicture.asset("assets/icons/department.svg", 
                                           color: htLightGrey),
                                  ),
                                ),
                                TextSpan(
                                  text: incident.department!.name,
                                  style: const TextStyle(fontWeight: FontWeight.normal, 
                                         fontSize: 16.0, color: htLightGrey, overflow: 
                                         TextOverflow.ellipsis),
                                ),
                              ]),
                              maxLines: 1,
                              overflow: TextOverflow.ellipsis,
                            )
                          : const SizedBox.shrink(),
                    )
                  ],
                )

That's how it looks when I have a WidgetSpan and a TextSpan, and the text inside the TextSpan is too long:

problem

problem

A similar question has already been asked here TextSpan's text doesn't show if it overflows and is placed after WidgetSpan, but the one answer isn't suitable for my problem.

I have two Text.rich() widgets (which each host one WidgetSpan and one TextSpan) inside an Expanded widget, both Expanded with flex:1, because I want them to be the same size and take as much space as they can. That's why a row is not an option, you can't (as far as I know) put two rows side by side, at this happens...

using rows

using rows

This is how it's supposed to look like, just with the TextOverflow.ellipsis, if the text is too long:

target UI

target ui

Here is my code:

                Row(
                  children: [
                    Expanded(
                      flex: 1,
                      child: Text.rich(
                        TextSpan(children: [
                          WidgetSpan(
                            child: Container(
                              margin: const EdgeInsets.only(right: 4.0),
                              child: SvgPicture.asset("assets/icons/building.svg", color: 
                                     htLightGrey),
                            ),
                          ),
                          TextSpan(
                            text: incident.building.name,
                            style: const TextStyle(
                              fontWeight: FontWeight.normal,
                              fontSize: 16.0,
                              color: htLightGrey, /*overflow: TextOverflow.ellipsis*/
                            ),
                          ),
                        ]),
                        maxLines: 1,
                        overflow: TextOverflow.ellipsis,
                      ),
                    ),
                    Expanded(
                      flex: 1,
                      child: incident.department != null
                          ? Text.rich(
                              TextSpan(children: [
                                WidgetSpan(
                                  child: Container(
                                    margin: const EdgeInsets.only(right: 4.0),
                                    child: SvgPicture.asset("assets/icons/department.svg", 
                                           color: htLightGrey),
                                  ),
                                ),
                                TextSpan(
                                  text: incident.department!.name,
                                  style: const TextStyle(fontWeight: FontWeight.normal, 
                                         fontSize: 16.0, color: htLightGrey, overflow: 
                                         TextOverflow.ellipsis),
                                ),
                              ]),
                              maxLines: 1,
                              overflow: TextOverflow.ellipsis,
                            )
                          : const SizedBox.shrink(),
                    )
                  ],
                )

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

感性 2025-02-11 07:24:53

要实现目标UI,请使用包装小部件而不是行小部件。在这里,当小部件溢出/超过它的内容将其分解为下一行,因此您将实现目标UI。

To achieve your target Ui, use the Wrap widget instead of the Row widget. Here, when the contents of the widget overflow/exceed it will break it to the next line, hence you'll achieve your target Ui.

调妓 2025-02-11 07:24:53

当我们喜欢归档此UI时,我们将行用作父窗口小部件,这是完美的。然后,我们可以将行分为树的零件,左两个部分将获得相同的宽度和最大可用宽度。对于这种情况,小部件结构如下。

Row(
  mainAxisSize: MainAxisSize.min,
  children: [
    Expanded(
      flex: 1,
      child: Container(
        color: Colors.green,
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start, //start from left
          children: [
            buildItem(
                text:
                    "incident.buildi cident.buildi cident.buildi cident.buildi cident.building.name"),
            buildItem(text: "incide ing.ssss"),
          ],
        ),
      ),
    ),
    Expanded(
      flex: 1,
      child: Container(
        color: Colors.red,
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            buildItem(text: "i ent.building.name"),
            buildItem(text: "incident.building.ssss"),
          ],
        ),
      ),
    ),
    IconButton(
      onPressed: () {},
      iconSize: 33,
      icon: Icon(Icons.arrow_right),
    ),
  ],
),

内部text.rich添加使用对齐:lastholderalignmign.middle, on widgetspan以设置中间。

maxLines: 1,
overflow: TextOverflow.ellipsis,
textAlign: TextAlign.start,

您将获得

我正在使用额外的容器指向UI,只需从列中删除容器即可。另外,您可以使用行而不是富文本。

测试窗口

class _TestGroundState extends State<TestGround> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
        body: Column(
      children: [
        Row(
          mainAxisSize: MainAxisSize.min,
          children: [
            Expanded(
              flex: 1,
              child: Container(
                color: Colors.green,
                child: Column(
                  crossAxisAlignment:
                      CrossAxisAlignment.start, //start from left
                  children: [
                    buildItem(
                        text:
                            "incident.buildi cident.buildi cident.buildi cident.buildi cident.building.name"),
                    buildItem(text: "incide ing.ssss"),
                  ],
                ),
              ),
            ),
            Expanded(
              flex: 1,
              child: Container(
                color: Colors.red,
                child: Column(
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: [
                    buildItem(text: "i ent.building.name"),
                    buildItem(text: "incident.building.ssss"),
                  ],
                ),
              ),
            ),
            IconButton(
              onPressed: () {},
              iconSize: 33,
              icon: Icon(Icons.arrow_right),
            ),
          ],
        ),
      ],
    ));
  }

  Text buildItem({
    required String text,
  }) {
    return Text.rich(
      TextSpan(
        children: [
          WidgetSpan(
            alignment: PlaceholderAlignment.middle,
            child: Container(
              child: Icon(Icons.ac_unit),
            ),
          ),
          TextSpan(
            text: text,
            style: TextStyle(
              fontWeight: FontWeight.normal,
              fontSize: 16.0,
            ),
          ),
        ],
      ),
      maxLines: 1,
      overflow: TextOverflow.ellipsis,
      textAlign: TextAlign.start,
    );
  }
}

小部件有关 flutter布局

While we like to archive this UI, we are using Row as parent widget, which is perfect. Then we can split the row into tree part, left two part will get same and maximum available width. For this scenario, widget structure as follows.

Row(
  mainAxisSize: MainAxisSize.min,
  children: [
    Expanded(
      flex: 1,
      child: Container(
        color: Colors.green,
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start, //start from left
          children: [
            buildItem(
                text:
                    "incident.buildi cident.buildi cident.buildi cident.buildi cident.building.name"),
            buildItem(text: "incide ing.ssss"),
          ],
        ),
      ),
    ),
    Expanded(
      flex: 1,
      child: Container(
        color: Colors.red,
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            buildItem(text: "i ent.building.name"),
            buildItem(text: "incident.building.ssss"),
          ],
        ),
      ),
    ),
    IconButton(
      onPressed: () {},
      iconSize: 33,
      icon: Icon(Icons.arrow_right),
    ),
  ],
),

And inside Text.rich add use alignment: PlaceholderAlignment.middle, on WidgetSpan to set the middle.

maxLines: 1,
overflow: TextOverflow.ellipsis,
textAlign: TextAlign.start,

And you will get

enter image description here

I am using extra container to point the UI, simply remove the container from the column. Also, you can use Row instead of Rich Text.

Test widget

class _TestGroundState extends State<TestGround> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
        body: Column(
      children: [
        Row(
          mainAxisSize: MainAxisSize.min,
          children: [
            Expanded(
              flex: 1,
              child: Container(
                color: Colors.green,
                child: Column(
                  crossAxisAlignment:
                      CrossAxisAlignment.start, //start from left
                  children: [
                    buildItem(
                        text:
                            "incident.buildi cident.buildi cident.buildi cident.buildi cident.building.name"),
                    buildItem(text: "incide ing.ssss"),
                  ],
                ),
              ),
            ),
            Expanded(
              flex: 1,
              child: Container(
                color: Colors.red,
                child: Column(
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: [
                    buildItem(text: "i ent.building.name"),
                    buildItem(text: "incident.building.ssss"),
                  ],
                ),
              ),
            ),
            IconButton(
              onPressed: () {},
              iconSize: 33,
              icon: Icon(Icons.arrow_right),
            ),
          ],
        ),
      ],
    ));
  }

  Text buildItem({
    required String text,
  }) {
    return Text.rich(
      TextSpan(
        children: [
          WidgetSpan(
            alignment: PlaceholderAlignment.middle,
            child: Container(
              child: Icon(Icons.ac_unit),
            ),
          ),
          TextSpan(
            text: text,
            style: TextStyle(
              fontWeight: FontWeight.normal,
              fontSize: 16.0,
            ),
          ),
        ],
      ),
      maxLines: 1,
      overflow: TextOverflow.ellipsis,
      textAlign: TextAlign.start,
    );
  }
}

More about flutter layout

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文