【问题标题】:How can i dynamically change color of certain words of a paragraph in a Text Widget in flutter如何在颤动的文本小部件中动态更改段落中某些单词的颜色
【发布时间】:2021-09-16 00:50:28
【问题描述】:

假设你有两个字符串。一个包含要着色的单词,另一个包含段落。示例:

列出单词 =["cow","milk","cattle" ];

字符串段落 = “牛,或奶牛(雌性)和公牛(雄性),是大型驯化的偶蹄草食动物。它们是牛亚科的重要现代成员,是牛属中分布最广的物种,最常被归类为金牛座。”;

我需要段落中的这些词是彩色的或可链接的文本。请帮忙

Such As like the picture

【问题讨论】:

  • 你需要RichText小部件

标签: string list flutter colors textspan


【解决方案1】:

结帐Highlight Text。 这是一个小例子,你可以如何达到你想要的效果。

Map<String, HighlightedWord> words = {
    "cow": HighlightedWord(
        textStyle: textStyle,
    ),
    "milk": HighlightedWord(
       
        textStyle: textStyle,
    ),
    "cattle": HighlightedWord(
       
        textStyle: textStyle,
    ),
};

你的文字

TextHighlight(
    text: text, // Your text
    words: words, // Your highlighted words
);

【讨论】:

    【解决方案2】:

    您需要拆分段落中的所有单词,然后在从列表中检查后为每个单词动态分配 Richtext > TextSpan。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-02-09
      • 2018-12-24
      • 2021-07-07
      • 1970-01-01
      • 1970-01-01
      • 2022-12-03
      相关资源
      最近更新 更多