【问题标题】:How to underline text in flutter如何在颤动中为文本加下划线
【发布时间】:2018-11-09 21:32:59
【问题描述】:

如何在 Text 小部件内的颤动中为文本加下划线?

我似乎在TextStylefontStyle 属性内找不到下划线

【问题讨论】:

    标签: flutter


    【解决方案1】:

    在为所有内容加下划线时,您可以在 Text 小部件上设置 TextStyle。

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

    如果您只想给部分文本加下划线,那么您需要使用Text.rich()(或 RichText 小部件)并将字符串分解为可以添加样式的 TextSpans。

    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 有点奇怪。 text 参数是默认样式,但children 列表包含其后的样式化(可能是非样式化)文本。如果您想以样式文本开头,则可以为 text 使用空字符串。

    您还可以添加 TextDecorationStyle 来更改装饰的外观。这里是虚线:

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

    TextDecorationStyle.dotted:

    TextDecorationStyle.double:

    TextDecorationStyle.wavy:

    【讨论】:

    • 单词和下划线之间可以加空格吗?
    • @felangga,这是个好问题。它可能与基线有关。这是我想进一步探索的东西,但我还不知道该怎么做。探索源代码,如果你弄明白了,请告诉我。
    • 在文本和下划线之间添加空格存在一个未解决的问题。 github.com/flutter/flutter/issues/30541
    • @felangga 在下面查看我的答案,了解两种解决方案,让您增加文本和下划线之间的间距
    • 感谢您的努力和解释
    【解决方案2】:

    您可以通过将decoration: TextDecoration.underline 应用于TextStyleText 来做到这一点。

    以主题为例:

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

    基本示例:

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

    【讨论】:

      【解决方案3】:

      令人兴奋的解决方案
      如果你想控制文本和下划线之间的距离,你可以使用这个技巧。简而言之,您使用 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,
                  ),
                )
      

      正如您将在下面看到的,如果您使用开箱即用的文本下划线,它会粘在文本的底部,看起来有点难看,

      无聊的解决方案

      仅使用 Text 小部件,您可以添加带有自定义样式和颜色的下划线:

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

      我对这种方法的唯一问题是您无法控制下划线与文本底部的距离。

      如果要增加间距,则必须使用非常规的方法,即使用 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,
              ),
             ),
            )
      

      请关注此GitHub issue 以获得更简单的解决方案。

      【讨论】:

      • 又一个使用阴影的丑陋解决方案。最终答案Style = TextStyle(装饰:TextDecoration.underline,装饰风格:TextDecorationStyle.dashed,颜色:Colors.transparent,装饰颜色:Colors.black,阴影:[Shadow(颜色:Colors.black,偏移:偏移(0,-5)) ], );
      • @Joe,非常聪明!我偷了你的部分解决方案并将其应用于我的问题(试图给你信用,但它没有链接到你的个人资料):stackoverflow.com/q/65293992/1459653 谢谢!
      • 偷偷摸摸!不错!
      • 不错的解决方案,我发现的唯一一个可以控制文本下的距离。然而它有很多代码只是为了一个下划线,所以我创建了一个扩展来在一行中完成它。看看我的回答 (TextStyleX)
      【解决方案4】:

      您可以在样式中使用 TextDecoration 为给定文本添加下划线。

      例如

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

      【讨论】:

        【解决方案5】:

        例如

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

        【讨论】:

          【解决方案6】:

          控制一行文字与下划线的距离

          以 Joe Muller 的回答为基础,一种扩展方法允许控制文本/下划线距离,而不会弄乱小部件树的代码。它是这样使用的:

          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),
              //------------------------------------------------
            );
          }
          

          这是扩展名:

          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,
              );
            }
          }
          

          该扩展还允许控制下划线颜色、粗细和样式,就像任何其他下划线装饰一样。看看a more complete example in this DartPad

          它仍然是一个 hack,但它允许在等待 Flutter 团队修复时保持小部件树的代码干净。

          【讨论】:

            猜你喜欢
            • 2021-04-19
            • 1970-01-01
            • 2021-09-01
            • 2012-04-18
            • 2011-04-13
            • 1970-01-01
            • 1970-01-01
            • 2012-09-07
            • 2013-02-21
            相关资源
            最近更新 更多