首页 > 解决方案 > Flutter拆分并使特定单词加粗

问题描述

我有类似“Hi @username how are you”的字符串 我想将@username 文本更改为粗体......只是@username 不是整个句子

示例:“嗨@username你好吗

标签: stringflutteruiviewflutter-test

解决方案


使用颤振的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')




],
  ),
)

推荐阅读