How do I bold (or format) a piece of text within a

2020-02-20 06:00发布

问题:

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

e.g.:

Hello World

回答1:

You should use the RichText widget.

A RichText widget will take in a TextSpan widget that can also have a list of children TextSpans.

Each TextSpan widget can have a different TextStyle.

Here is the example code to render: 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)),
    ],
  ),
 );


回答2:

I like keeping my code short and clean this is How I Would do it add two text fields in a row one with Normal font and another bold,

Note: This may not look good for a long paragraph looks good for Headlines etc.

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

and you should get a desired output as "Hello World"



回答3:

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?'),
    ],
  ),
);


标签: dart flutter