【问题标题】:Flutter split and make specific word bold [closed]颤振拆分并使特定单词加粗[关闭]
【发布时间】:2020-07-29 19:19:43
【问题描述】:

我有类似的字符串 “嗨@username你好吗” 我想将@username 文本更改为粗体...只是@username 不是整个句子

示例:“嗨 @username 你好吗

【问题讨论】:

标签: string flutter uiview flutter-test


【解决方案1】:

利用颤振TextSpan

Text _myText;
/*set _myText.text to whatever text you want */
RichText(
  text: TextSpan(
    text: 'Hi',
    style: DefaultTextStyle.of(context).style,
    children: <TextSpan>[
      TextSpan(text: _myText.text, style: TextStyle(fontWeight: FontWeight.bold)),
      TextSpan(text: 'how are you')




],
  ),
)

【讨论】:

  • 为我工作,谢谢
  • @HadiHaidar 欢迎您!
【解决方案2】:

这是一个小函数,它会为您执行此操作,然后返回一个小部件列表。

List<Text> _transformWord(String word) {
    List<String> name = word.split(' ');
    List<Text> textWidgets = [];
    for (int i = 0; i < name.length; i++) {
      if (name[i].contains('@')) {
        Text bold = Text(
          name[i] + ' ',
          style: TextStyle(
            fontWeight: FontWeight.bold,
          ),
        );
        textWidgets.add(bold);
      } else {
        Text normal = Text(
          name[i] + ' ',
        );
        textWidgets.add(normal);
      }
    }
    return textWidgets;
  }

您可以从行小部件调用此函数

Row(
     children: _transformWord(),
    ),

【讨论】:

    猜你喜欢
    • 2023-03-29
    • 2022-08-13
    • 2019-04-30
    • 1970-01-01
    • 2013-07-12
    • 2021-11-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多