Let part of a piece of text be displayed in other styles in flutter

For text, we generally use Text widget, for example:

Text("HelloWorld",
        style: TextStyle(
          fontSize: 14.0,
          color: Colors.black,
        ));

When you need to bold and change the color of HelloWorld, you can rewrite it as a rich text widget  RichTextand use  TextSpan it for splicing. The writing method is as follows: 

    RichText(
      text: TextSpan(
        // 注意:TextSpan需要指定样式
        // TextSpan子组件会继承父组件的样式
        style: TextStyle(
          fontSize: 14.0, //设置大小
          color: Colors.red,//设置颜色
        ),
        children: <TextSpan>[
          TextSpan(text: "Hello"),
          TextSpan(text: "World", style: new TextStyle(fontWeight: FontWeight.bold)),//其他样式设置粗体
        ],
      ),
    );

Guess you like

Origin blog.csdn.net/lqw200931116/article/details/125692782