flutter中让一段文字中的部分显示为其他样式

文本我们一般使用Text widget,例如:

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

当需要将 HelloWorld 加粗改变颜色时,可以改写成富文本widget RichText,利用 TextSpan 进行拼接,写法如下: 

    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)),//其他样式设置粗体
        ],
      ),
    );

猜你喜欢

转载自blog.csdn.net/lqw200931116/article/details/125692782
今日推荐