【发布时间】:2018-11-09 21:32:59
【问题描述】:
如何在 Text 小部件内的颤动中为文本加下划线?
我似乎在TextStyle 的fontStyle 属性内找不到下划线
【问题讨论】:
标签: flutter
如何在 Text 小部件内的颤动中为文本加下划线?
我似乎在TextStyle 的fontStyle 属性内找不到下划线
【问题讨论】:
标签: flutter
在为所有内容加下划线时,您可以在 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:
【讨论】:
您可以通过将decoration: TextDecoration.underline 应用于TextStyle 的Text 来做到这一点。
以主题为例:
Text(
"text",
style: Theme
.of(context)
.accentTextTheme
.subhead
.copyWith(decoration: TextDecoration.underline),
)
基本示例:
Text(
"text",
style: TextStyle(decoration: TextDecoration.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,
),
)
正如您将在下面看到的,如果您使用开箱即用的文本下划线,它会粘在文本的底部,看起来有点难看,
无聊的解决方案
仅使用 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 以获得更简单的解决方案。
【讨论】:
TextStyleX)
您可以在样式中使用 TextDecoration 为给定文本添加下划线。
例如
Text(
"Your text here",
style: TextStyle(
decoration: TextDecoration.underline),
)
)
【讨论】:
例如
Text(
"Terms and Condition",
style: TextStyle(
decoration:
TextDecoration.underline,
height: 1.5,
fontSize: 15,
fontWeight: FontWeight.bold,
fontFamily: 'Roboto-Regular',
),
),
【讨论】:
以 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 团队修复时保持小部件树的代码干净。
【讨论】: