Flutter实现富文本效果

如图所示,我们希望让一段文字实现不同颜色展示,并且每个颜色有自己的点击事件,实现这个功能我们可以使用RichText,实现代码如下:


  _protocolText() {
    return Padding(
      padding: const EdgeInsets.only(
        top: 20,
      ),
      child: RichText(
        strutStyle:
            const StrutStyle(forceStrutHeight: true, height: 0.5, leading: 0.7),
        text: TextSpan(
          text: "富文本实现富文本实现富文本实现富文本",
          style: const TextStyle(
            fontSize: 11,
            fontWeight: FontWeight.normal,
            color: ConfigColor.normalTextColor,
          ),
          children: [
            TextSpan(
                text: "蓝色字体",
                style: const TextStyle(
                  color: Colors.blue,
                ),
                recognizer: TapGestureRecognizer()
                  ..onTap = () {
                    print("------------点击 1");
                  }),
            TextSpan(
                text: "红色字体",
                style: const TextStyle(
                  fontSize: 15,
                  color: Colors.red,
                ),
                recognizer: TapGestureRecognizer()
                  ..onTap = () {
                    print("------------点击 2");
                  }),
            const TextSpan(
              text: "黑色字体 ",
              style: TextStyle(
                fontSize: 10,
                color: Colors.black,
              ),
            ),
            TextSpan(
                text: "黄色字体",
                style: const TextStyle(
                    color: Colors.yellow,
                    fontSize: 20),
                recognizer: TapGestureRecognizer()
                  ..onTap = () {
                    print("------------点击 3");
                  }),
          ],
        ),
      ),
    );
  }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值