简体   繁体   English

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

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

Is there any way to make Text color in white while underline is in yellow?有没有办法使文本颜色为白色而下划线为黄色?

RichText, TextSpan or Text seem like don't have opportunity to do this. RichText、TextSpan 或 Text 似乎没有机会这样做。

this what i'm looking for.这就是我要找的。

在此处输入图片说明

First you have to use the Material widget and within it you define a RichText , TextSpan and a Text .首先,您必须使用Material小部件,并在其中定义RichTextTextSpanText

Inside the Material widget you need to set underline color.Material小部件中,您需要设置下划线颜色。

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

You can use decorationColor: yourColor inside your Text attribute to change the underline's color.您可以在 Text 属性中使用decorationColor: yourColor 来更改下划线的颜色。 For example, if you want to underline all of your text:例如,如果您想为所有文本加下划线:

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

In case you want to underline only a part of text you have to use RichText with TextSpan.如果您只想在文本的一部分下划线,则必须将 RichText 与 TextSpan 结合使用。 For example:例如:

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)
  ))

You can achieve this behaviour with a RichText .您可以使用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