简体   繁体   English

如何给flutter中的文字加下划线

[英]How to underline text in flutter

How to underline text in flutter inside Text widget?如何在Text小部件内的 flutter 中为文本添加下划线?

I cannot seem to find underline inside fontStyle property of TextStyle我似乎无法在TextStylefontStyle属性中找到下划线

When underlining everything you can set a TextStyle on the Text widget.在为所有内容加下划线时,您可以在 Text 小部件上设置 TextStyle。

在此处输入图像描述

Text(
  'Hello world',
  style: TextStyle(
    decoration: TextDecoration.underline,
  ),
)

If you only want to underline part of the text then you need to use Text.rich() (or a RichText widget) and break the string into TextSpans that you can add a style to.如果您只想在部分文本下划线,则需要使用Text.rich() (或 RichText 小部件)并将字符串分解为可以添加样式的 TextSpan。

在此处输入图像描述

Text.rich(
  TextSpan(
    text: 'Hello ',
    style: TextStyle(fontSize: 50),
    children: <TextSpan>[
      TextSpan(
          text: 'world',
          style: TextStyle(
            decoration: TextDecoration.underline,
          )),
      // can add more TextSpans here...
    ],
  ),
)

TextSpan is a little strange. TextSpan 有点奇怪。 The text parameter is the default style but the children list contains the styled (and possibly unstyled) text that follow it. text参数是默认样式,但children列表包含其后的样式化(可能是非样式化)文本。 You can use an empty string for text if you want to start with styled text.如果要从样式文本开始,可以使用空字符串作为text

You can also add a TextDecorationStyle to change how the decoration looks.您还可以添加 TextDecorationStyle 来更改装饰的外观。 Here is dashed:这里是虚线:

在此处输入图像描述

Text(
  'Hello world',
  style: TextStyle(
    decoration: TextDecoration.underline,
    decorationStyle: TextDecorationStyle.dashed,
  ),
)

and TextDecorationStyle.dotted :TextDecorationStyle.dotted

在此处输入图像描述

and TextDecorationStyle.double :TextDecorationStyle.double

在此处输入图像描述

and TextDecorationStyle.wavy :TextDecorationStyle.wavy

在此处输入图像描述

Exciting solution令人兴奋的解决方案
If you want to have control over the distance between the text and the underline, you can use this hack.如果你想控制文本和下划线之间的距离,你可以使用这个技巧。 In short, you hide the actual text using Colors.transparent and then display an offset shadow that hovers above the Text underline.简而言之,您使用 Colors.transparent 隐藏实际文本,然后显示悬停在文本下划线上方的偏移阴影。

        Text(
            "Forgot Password?",
            style: TextStyle(
              shadows: [
                Shadow(
                    color: Colors.black,
                    offset: Offset(0, -5))
              ],
              color: Colors.transparent,
              decoration:
              TextDecoration.underline,
              decorationColor: Colors.blue,
              decorationThickness: 4,
              decorationStyle:
              TextDecorationStyle.dashed,
            ),
          )

在此处输入图像描述

As you'll see below, if you use the out-of-the-box Text underline, it sticks to the bottom of the text and can look a bit ugly,正如您将在下面看到的,如果您使用开箱即用的文本下划线,它会粘在文本的底部,看起来有点难看,

Boring Solutions无聊的解决方案

Using just the Text widget you can add the underline with a custom style and color:仅使用Text小部件,您可以使用自定义样式和颜色添加下划线:

Text(
  "Forgot Password?",
  style: TextStyle(
    decoration: TextDecoration.underline,
    decorationColor: Colors.blue,
    decorationThickness: 4,
    decorationStyle: TextDecorationStyle.dashed,
   ),
)

The only issue I have with this approach is that you have no control over how close the underline is to the bottom of the text.这种方法的唯一问题是您无法控制下划线与文本底部的距离。

在此处输入图像描述

If you want to increase the spacing, you'll have to use an unconventional approach that uses Container and it's padding property.如果要增加间距,则必须使用一种非常规的方法,该方法使用 Container 及其 padding 属性。

Container(
     padding: EdgeInsets.only(
       bottom: 5, // Space between underline and text
     ),
     decoration: BoxDecoration(
         border: Border(bottom: BorderSide(
         color: Colors.amber, 
         width: 1.0, // Underline thickness
        ))
      ),
     child: Text(
        "Forgot Password?",
        style: TextStyle(
        color: Colors.black,
        ),
       ),
      )

在此处输入图像描述

Keep an eye on this GitHub issue for a simpler solution.密切关注这个GitHub 问题以获得更简单的解决方案。

You do it by applying decoration: TextDecoration.underline to TextStyle of a Text .您可以通过将decoration: TextDecoration.underline应用于TextTextStyle来实现。

With Theme example:以主题为例:

          Text(
            "text",
            style: Theme
                .of(context)
                .accentTextTheme
                .subhead
                .copyWith(decoration: TextDecoration.underline),
          )

Basic example:基本示例:

          Text(
            "text",
            style: TextStyle(decoration: TextDecoration.underline),
          )

You can use TextDecoration in style to underline a given text.您可以在样式中使用 TextDecoration 来为给定文本添加下划线。

For example例如

Text(
    "Your text here",
    style: TextStyle(
        decoration: TextDecoration.underline),
    )
)

for example例如

Text(
  "Terms and Condition",
  style: TextStyle(
    decoration:
        TextDecoration.underline,
    height: 1.5,
    fontSize: 15,
    fontWeight: FontWeight.bold,
    fontFamily: 'Roboto-Regular',
  ),
),

Following is a simple code下面是一个简单的代码

Text(
  'Welcome to Flutter',
  style: TextStyle(decoration:TextDecoration.underline,),
),

在此处输入图像描述

You can use decorationStyle to customize it您可以使用decorationStyle来自定义它

Text(
  'Welcome to Flutter',
  style: TextStyle(
  fontSize: 24,
  decoration: TextDecoration.underline,
  decorationStyle: TextDecorationStyle.double,
  ),
),

Here is other TextDecorationStyle values:这是其他TextDecorationStyle值:

  • TextDecorationStyle.dashed TextDecorationStyle.dashed
  • TextDecorationStyle.dotted TextDecorationStyle.dotted
  • TextDecorationStyle.double TextDecorationStyle.double
  • TextDecorationStyle.wavy TextDecorationStyle.wavy

Control the distance between the text and underline in one line控制一行文字和下划线的距离

Building on Joe Muller's answer here is an extension method that allows to have control over the text/underline distance without cluttering the widget tree's code .基于 Joe Muller 的回答,这里是一种扩展方法,它允许控制文本/下划线距离,而不会弄乱小部件树的代码 It's used like this:它是这样使用的:

Widget build(BuildContext context) {
  final myStyle = Theme.of(context).textTheme.headline4;

  return Text(
    'Hello, World!',
    //------------------------------------------------
    // simply apply the underlined method to the style
    style: myStyle?.underlined(distance: 2),
    //------------------------------------------------
  );
}

Here is the extension:这是扩展名:

extension TextStyleX on TextStyle {
  /// A method to underline a text with a customizable [distance] between the text
  /// and underline. The [color], [thickness] and [style] can be set
  /// as the decorations of a [TextStyle].
  TextStyle underlined({
    Color? color,
    double distance = 1,
    double thickness = 1,
    TextDecorationStyle style = TextDecorationStyle.solid,
  }) {
    return copyWith(
      shadows: [
        Shadow(
          color: this.color ?? Colors.black,
          offset: Offset(0, -distance),
        )
      ],
      color: Colors.transparent,
      decoration: TextDecoration.underline,
      decorationThickness: thickness,
      decorationColor: color ?? this.color,
      decorationStyle: style,
    );
  }
}

The extension also allows to control the underline color, thickness and style just like any other underline decoration.该扩展还允许控制下划线颜色、粗细和样式,就像任何其他下划线装饰一样。 Have a look at a more complete example in this DartPad .查看此 DartPad 中更完整的示例

It is still a hack but it allows to keep the widget tree's code clean in the wait for a fix from the Flutter team.它仍然是一个 hack,但它允许在等待 Flutter 团队修复时保持小部件树的代码干净。

Another example另一个例子

child: Text.rich(
  TextSpan(
    text: 'By using our mobile app, you agree to our ',
    children: [
      TextSpan(
        text: 'Term of Use',
        style: TextStyle(
          decoration: TextDecoration.underline,
        )
      ),
      TextSpan(text: ' and '),
      TextSpan(
        text: 'Privacy Policy',
        style: TextStyle(
          decoration: TextDecoration.underline,
        )
      ),
    ]
  ),
),

Output:输出:

就这样

或者,为了防止复杂性,您还可以使用 Flutter Markdown https://pub.dev/packages/flutter_markdown

Here's how to generate the most basic underline.下面介绍如何生成最基本的下划线。

Text(
  'Hello,flutter!',
  style: TextStyle(
     decoration: TextDecoration.underline,
  ),
)

The most customized way to do this is to wrap text in container and give the bottom border.最自定义的方法是将文本包装在容器中并给出底部边框。 You can also give space between text and underline as many as you want according to your design.您还可以根据您的设计在文本和下划线之间留出任意数量的空间。 Please refer to Joe Muller , last option.请参考Joe Muller ,最后一个选项。

Container(
 padding: const EdgeInsets.only(
   bottom: 5, // Space between underline and text
 ),
 decoration: BoxDecoration(
     border: Border(bottom: BorderSide(
     color: Colors.amber, 
     width: 2.0, // Underline thickness
    ))
  ),
 child: Text(
    "Forgot Password?",
    style: TextStyle(
    color: Colors.black,
    ),
   ),
  )
Text(
  'Text',
  style: TextStyle(
    decoration: TextDecoration.underline,
  ),
)

样式:TextStyle(装饰:TextDecoration.underline,),

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

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