2

I have string which is like "Hi @username how are you" I want to change @username text to bold... just @username not whole sentence

Example : " Hi @username how are you

Yash
  • 55
  • 2
  • 6
  • Does this answer your question? [How do I bold (or format) a piece of text within a paragraph?](https://stackoverflow.com/questions/41557139/how-do-i-bold-or-format-a-piece-of-text-within-a-paragraph) – jbarat Apr 16 '20 at 14:07
  • @jbarat nope, actually I have list and from the list is , msg, userid is thrown and In msg I want to have above out put – Yash Apr 16 '20 at 14:16

2 Answers2

3

This is a small function that would do that for you then returns a list of widgets.

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;
  }

You would call this function from a row widget

Row(
     children: _transformWord(),
    ),
wcyankees424
  • 2,554
  • 2
  • 12
  • 24
2

Make use of flutter 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')




],
  ),
)