Flutter - Space between Text and underline
The Text widget displays a string of text with a single style in a flutter
app. But depending on the layout constraints the string might break across
multiple lines or might all be displayed on the same line. The style argument
is optional in a Text instance. When the style argument is omitted, the text
will use the style from the closest enclosing DefaultTextStyle. By default,
the Text is not selectable. But flutter developers can make a Text selectable
by wrapping a subtree with a SelectionArea widget.
The following flutter application development tutorial will demonstrate how we can underline text. And we will also show how we can put some extra space between the Text and the underline. How to increase the gap between the text and its underline. Here we will assign a TextStyle instance for the Text widget’s style property.
Then we will use the TextStyle class’s decoration property to put an underline underneath each line of text. And we will use the TextStyle class’s shadows property to add a shadow to the Text. And we also set the text color to transparent to hide it. So the offset of the shadow will seem to be a gap between the text and its underline. That means, the shadow will draw the actual text and the text will be hidden.
The Text class’s style property value is a TextStyle object. The TextStyle class represents an immutable style describing how to format and paint text.
The TextStyle class decoration property is used to apply decorations to paint near the text such as an underline. The flutter app developer can apply multiple decorations with TextDecoration.combine.
The TextDecoration class represents a linear decoration to draw near the text. The TextDecoration class’s underline const draws a line underneath each line of text.
The TextStyle class’s shadows property value is a list of Shadows that will be painted underneath the text. Multiple shadows are supported to replicate lighting from multiple light sources.
The Shadow class represents a single shadow. Multiple shadows are stacked together in a TextStyle. The Shadow class’s color property value is the color with which the shadow will be drawn.
The Shadow class’s offset property value is the displacement of the shadow from the casting element.
The TextStyle class’s color property value is a Color. This Color is used when painting the text.
So finally, the flutter app developers can put an underline on a Text by using the TextStyle class’s decoration property. They must also set the decoration property value to the underline constant as TextDecoration.underline.
The flutter developers can increase the gap between text and its underline by putting a shadow element for the text and setting the text color to transparent. But the shadow and underline color should have to same.
The following flutter application development tutorial will demonstrate how we can underline text. And we will also show how we can put some extra space between the Text and the underline. How to increase the gap between the text and its underline. Here we will assign a TextStyle instance for the Text widget’s style property.
Then we will use the TextStyle class’s decoration property to put an underline underneath each line of text. And we will use the TextStyle class’s shadows property to add a shadow to the Text. And we also set the text color to transparent to hide it. So the offset of the shadow will seem to be a gap between the text and its underline. That means, the shadow will draw the actual text and the text will be hidden.
The Text class’s style property value is a TextStyle object. The TextStyle class represents an immutable style describing how to format and paint text.
The TextStyle class decoration property is used to apply decorations to paint near the text such as an underline. The flutter app developer can apply multiple decorations with TextDecoration.combine.
The TextDecoration class represents a linear decoration to draw near the text. The TextDecoration class’s underline const draws a line underneath each line of text.
The TextStyle class’s shadows property value is a list of Shadows that will be painted underneath the text. Multiple shadows are supported to replicate lighting from multiple light sources.
The Shadow class represents a single shadow. Multiple shadows are stacked together in a TextStyle. The Shadow class’s color property value is the color with which the shadow will be drawn.
The Shadow class’s offset property value is the displacement of the shadow from the casting element.
The TextStyle class’s color property value is a Color. This Color is used when painting the text.
So finally, the flutter app developers can put an underline on a Text by using the TextStyle class’s decoration property. They must also set the decoration property value to the underline constant as TextDecoration.underline.
The flutter developers can increase the gap between text and its underline by putting a shadow element for the text and setting the text color to transparent. But the shadow and underline color should have to same.
main.dart
import 'package:flutter/material.dart';
void main(){
runApp(
MaterialApp(
home: Scaffold(
appBar: AppBar(
title: const Text("Flutter - Text Underline Space")
),
body: const Center(
child: Text(
"Lorem Ipsum is simply dummy text of the printing"
" and typesetting industry.",
style: TextStyle(
shadows: [
Shadow(
color: Colors.black87,
offset: Offset(0, -6)
)
],
color: Colors.transparent,
fontSize: 25,
decoration: TextDecoration.underline,
decorationColor: Colors.black87,
),
textAlign: TextAlign.center,
),
),
),
)
);
}