【发布时间】:2020-07-29 19:19:43
【问题描述】:
我有类似的字符串 “嗨@username你好吗” 我想将@username 文本更改为粗体...只是@username 不是整个句子
示例:“嗨 @username 你好吗
【问题讨论】:
-
@jbarat 不,实际上我有列表,从列表中是,味精,用户 ID 被抛出,在味精中我想有上面的输出
标签: string flutter uiview flutter-test
我有类似的字符串 “嗨@username你好吗” 我想将@username 文本更改为粗体...只是@username 不是整个句子
示例:“嗨 @username 你好吗
【问题讨论】:
标签: string flutter uiview flutter-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')
],
),
)
【讨论】:
这是一个小函数,它会为您执行此操作,然后返回一个小部件列表。
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(),
),
【讨论】: