How to make bold a specific portion of a dynamic string in flutter?

638 Views Asked by At

I am getting a message from the server like - THE ONLY MARATHON YOU'VE EVER DONE IS A MOVIE MARATHON. GET OFF THE COUCH CHUNKY BUTT AND DO SOME CARDIO. I have to make it bolder those character which is in asterisks. I am using flutter package "simple_text". It's working but not as like expectation. So, could anyone please help me with sharing effective suggestions? Here you go with my code and output screenshot. Thanks in advance!

SimpleRichText(

                "THE ONLY MARATHON YOU'VE EVER DONE IS A MOVIE MARATHON. GET OFF THE COUCH *CHUNKY BUTT* AND DO SOME CARDIO.",

                  textAlign: TextAlign.center,
                  style: const TextStyle(
                    fontSize: 35.0,
                    fontFamily: 'SFPRO',
                    color: white,
                    letterSpacing: 1,
                    fontWeight: FontWeight.w100,
                  ),
                ),

enter image description here

2

There are 2 best solutions below

3
Rubens Melo On

RichText widget

enter image description here

RichText(
  text: TextSpan(
    text: 'Hello ',
    style: DefaultTextStyle.of(context).style,
    children: const <TextSpan>[
      TextSpan(text: 'bold', style: TextStyle(fontWeight: FontWeight.bold)),
      TextSpan(text: ' world!'),
    ],
  ),
)

https://api.flutter.dev/flutter/widgets/RichText-class.html

0
Jscti On

Seems like you receive some Markdown formatting from server (?)

So you can use https://pub.dev/packages/flutter_markdown to parse it and render it

Markdown(
    controller: controller,
    selectable: true,
    data: 'Hey *there*',
)