Search code examples
stringdartflutter

Flutter: Is it possible to format (bold, italicize, etc) with-in the string only before passing to the text widget?


For example :

String desc = "<bold>Hello<bold> World";
new Text(desc);

Solution

  • You can use the flutter_html_view package for that.

    String html = '<bold>Hello<bold> World';

    new HtmlTextView(data: html);

    If you just want different styles you can use a RichText widget with TextSpans like this.

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