flutter中修改一段文本中部分字体的样式

东明兄 2020-11-10
0条评论 1,991 次浏览
东明兄 2020-11-100条评论 1,991 次浏览

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

       Container(
                                      child: Text(
                                    "今年是2020年",
                                    style: TextStyle(
                                        fontSize: fitFont(14),
                                        color: Color.fromRGBO(71, 75, 82, 1)),
                                  )),

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

           Container(
                                      child: RichText(
                                    text: TextSpan(
                                        style: TextStyle(
                                            fontSize: fitFont(14),
                                            color:
                                                Color.fromRGBO(71, 75, 82, 1)),
                                        children: [
                                          TextSpan(text: '今年是'),
                                          TextSpan(
                                              text: '2020',
                                              style: TextStyle(
                                                  fontWeight: FontWeight.bold,
                                                  color: Colors.red)),
                                          TextSpan(text: '年'),
                                        ]),
                                  ))

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注