How to select (or format) text in a paragraph?

How can I have a line of text with different formatting?

eg:.

Hello world

+28
source share
3 answers

You must use the RichText widget.

The RichText widget will take the form of TextSpan , which can also have a list of child TextSpans.

Each TextSpan widget may have a different TextStyle .

Here is a sample code for rendering: Hello World

var text = new RichText( text: new TextSpan( // Note: Styles for TextSpans must be explicitly defined. // Child text spans will inherit styles from parent style: new TextStyle( fontSize: 14.0, color: Colors.black, ), children: <TextSpan>[ new TextSpan(text: 'Hello'), new TextSpan(text: 'World', style: new TextStyle(fontWeight: FontWeight.bold)), ], ), ); 
+68
source

I like my code to be short and clean, this is how I can add two text fields in a row, one with a normal font and the other in bold ,

Note: this may not look very good if a long paragraph looks good for headings, etc.

 Row(children: <Widget>[ Text("Hello"), Text("World", style: TextStyle(fontWeight: FontWeight.bold)) ]) ' 

and you should get the desired result as "Hello World "

+10
source
 return RichText( text: TextSpan( text: 'Can you ', style: TextStyle(color: Colors.black), children: <TextSpan>[ TextSpan( text: 'find the', style: TextStyle( color: Colors.green, decoration: TextDecoration.underline, decorationStyle: TextDecorationStyle.wavy, ), recognizer: _longPressRecognizer, ), TextSpan(text: 'secret?'), ], ), ); 
0
source

Source: https://habr.com/ru/post/1262544/


All Articles