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