繁体   English   中英

有没有办法在 Flutter 中使 Text 和 Underline 不同颜色?

[英]is there any way to make Text and Underline different colors in Flutter?

有没有办法使文本颜色为白色而下划线为黄色?

RichText、TextSpan 或 Text 似乎没有机会这样做。

这就是我要找的。

在此处输入图片说明

首先,您必须使用Material小部件,并在其中定义RichTextTextSpanText

Material小部件中,您需要设置下划线颜色。

Material(
 child:RichText(),//input our need
);

您可以在 Text 属性中使用decorationColor: yourColor 来更改下划线的颜色。 例如,如果您想为所有文本加下划线:

Text("Your text",
      style: TextStyle(
             color: Colors.white
             decoration: TextDecoration.underline,
             decorationColor: Colors.yellow,
          ))

如果您只想在文本的一部分下划线,则必须将 RichText 与 TextSpan 结合使用。 例如:

RichText(
   text: TextSpan(
   children: [
         TextSpan(
             text: "NOT UNDERLINED TEXT",
             style: TextStyle(
             color: Colors.white)
         ),
         TextSpan(
             text: "UNDERLINED TEXT",
             style: TextStyle(
             color: Colors.white
             decoration: TextDecoration.underline,
             decorationThickness: 2,
             decorationStyle: TextDecorationStyle.wavy))
              ], 
     style: TextStyle(color: Colors.yellow)
  ))

您可以使用RichText实现此行为。

RichText(
  text: TextSpan(
    text: 'You should only use this app when it is safe and legal to do so. ',
    style: TextStyle(color: Colors.white),
    children: [
      TextSpan(
        text: 'Please read our ',
        style: TextStyle(
          color: Colors.white,
        ),
        children: [
          TextSpan(
            text: 'Privacy Policy',
            style: TextStyle(
              decoration: TextDecoration.underline,
              decorationColor: Colors.yellow,
              fontWeight: FontWeight.bold,
              color: Colors.white,
            ),
          )
        ],
      ),
    ],
  ),
);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM