简体   繁体   English

在下划线上方显示带有填充的文本

[英]Display text above underline with padding

Is there a way to achieve the following results in Flutter where the underline below text will be nicely hidden by the padding around letters that overflow it, such as 'g', 'q', 'y', etc?有没有办法在 Flutter 中实现以下结果,其中文本下方的下划线将被溢出它的字母周围的填充很好地隐藏,例如“g”、“q”、“y”等?

Emulator (current):模拟器(当前):

模拟器视图

Design (desired):设计(所需):

设计和所需的视图

Font: Spartan字体:斯巴达

The best way I found to achieve that was to use text stroke and Stack to create the underline effect.我发现实现这一目标的最佳方法是使用文本笔划和堆栈来创建下划线效果。 Here is the code:这是代码:

Align(
  alignment: Alignment.centerLeft,
  child: Container(
    padding: EdgeInsets.symmetric(vertical: 6.0),
    child: Stack(
      children: [
        Positioned(
          bottom: 2,
          left: 0,
          right: 0,
          child: Container(height: 1.0, color: const Color(0xFF2F3543)),
        ),
        Text(
          "Forgot password?",
          style: TextStyle(
            fontWeight: FontWeight.w300,
            inherit: true,
            fontSize: 13.0,
            foreground: Paint()
              ..style = PaintingStyle.stroke
              ..strokeWidth = 2
              ..color = Colors.white,
          ),
        ),
        Text(
          "Forgot password?",
          style: TextStyle(
            color: const Color(0xFF2F3543),
            fontWeight: FontWeight.w300,
            inherit: true,
            fontSize: 13.0,
          ),
        )
      ],
    ),
  ),
)

And the screenshot of the results以及结果截图

结果

EDIT编辑

The first one looks working but not looks good and the second one working fine but p word not that good.第一个看起来工作但看起来不太好,第二个工作正常但 p 字不是那么好。 When you dont want to add underline just write decoration:TextDecoration.none.当您不想添加下划线时,只需编写装饰:TextDecoration.none。

    Text.rich(
      TextSpan(
        text: 'For ',
        style: TextStyle(decoration: TextDecoration.underline, fontSize: 25),
        children: <TextSpan>[
          TextSpan(text: 'g',style:TextStyle(decoration: TextDecoration.none)),
          TextSpan( text: ' ',style:TextStyle(decoration: TextDecoration.underline, decorationThickness: 1)),
          TextSpan( text: ' p',style:TextStyle(decoration: TextDecoration.none,)),
          TextSpan( text: 'assword?',style:TextStyle(decoration: TextDecoration.underline)),
          //you can add more text span here
        ],
      ),
    ),
    Row(
      children: <Widget>[
        Text('For', style: TextStyle(decoration: TextDecoration.underline, fontSize: 25)),
        Text('g', style: TextStyle(decoration: TextDecoration.none, fontSize: 25)),
        Text('ot', style: TextStyle(decoration: TextDecoration.underline, fontSize: 25)),
        Text(' ', style: TextStyle(decoration: TextDecoration.underline, fontSize: 25)),
        Text('p', style: TextStyle(decoration: TextDecoration.none, fontSize: 25)),
        Text('assword', style: TextStyle(decoration: TextDecoration.underline, fontSize: 25)),
      ],
    )

Working example image工作示例图像

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

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