How To Easily Set Space Between Flutter Text And Underline

Space between Flutter text and underline

In this post, I will be introducing a very easy method to set a space between Flutter text and underline. I will be implementing the space between Flutter text and underline using a very simple example so you can also easily implement it in your own Flutter code. So let’s jump right into implementing space between Flutter text and underline.

What is Space Between Flutter Text And Underline?

Space between Flutter text and underline as the name suggests, it is the distance between the Flutter text and underline. When we define a Flutter text widget and give it an underline then there is not much of a distance/space between the text and underline and if we make the underline a little thick(heighted) then the Flutter text and underline will be attached and will look very bad.

Let’s understand it using a proper Flutter code implementation. First we will see what the default space between Flutter text and underline looks like, then we change it using a simple method.

Default Space Between Flutter Text And Underline

To see the default space between Flutter text and underline, we have to define a Flutter text widget with some text and an underline. If you want to know how to properly implement Flutter text underline then underline. See the below code:

Text('Default space between text and underline',
          style: TextStyle(decoration: TextDecoration.underline))
 flutter text underline default space
This is the default space between Flutter text and underline.

Default Space Between Text And Thick Underline

Let’s now make the underline a bit thick and then see what the default space between Flutter text and underline will look. To make the Flutter text underline thick, we have to use the decoration thickness constructor of the text style class. This constructor takes a double(decimal) value but integer will also work(it will convert it automatically). For demonstration, we have passed it a value of 3. See the below code:

Text(
       'Space between text and thick underline',
          style: TextStyle(
              decoration: 
                    TextDecoration.underline, 
                     decorationThickness: 3))
 flutter text underline default space
You can see that the underline is now attached to the text and its not looking so good. So now let’s see how to given space between Flutter text and underline.

Implementing Space Between Flutter Text And Underline(All Steps)

In order to implement it, we will use a simple trick. See the below steps:

Step 1: Give Shadow To The Text
shadows: [Shadow(color: Colors.black45, offset: Offset(0, -10))]
 flutter text shadow
We have to use the shadows constructor of the text style class which takes a list of shadow. For demonstration, we have passed it a shadow of color black and offset is used to positioned the shadow so we have given negative value to the vertical direction to the position of the shadow will be placed slightly upward compared to the text. You can see in the above code for practical implementation. In the image above, you can see that the shadow is implemented.
Step 2: Give Custom Colors To The Underline
decorationColor: Colors.grey
flutter text underline color
We have to give a custom color to the underline, the reason is that if we don’t use its own color then underline will disappear if we give transparent color to the Flutter text. We have to use the color constructor of the text style class which takes a color. So for demonstration, we have passed it a grey color and can be seen in the above image.

Step 3: Make The Flutter Text Color Transparent

 color: Colors.transparent
Space between Flutter text and underline

Now the final step is to make the color of the Flutter text transparent. For that we have to use the color constructor of the text style class and pass it Colors.transparent. In the image above you can see that the text color is transparent and cannot be seen. We also have made the shadow color complete from Colors.black45 to Colors.black. You can leave the thickness of the underline or you can change it depends on the requirements of your Flutter app project.

You can see that we have successfully implemented a space between Flutter text and underline. The text is actually a shadow in the above image but still it works. Hope you now have a complete understanding of how to implement space between Flutter text and underline. Don’t hesitate to ask in the comment section if you still have any doubts related to the implementation of space between Flutter text and underline. I would love to answer all.

The complete source code for the above implemented space between Flutter text and underline is given in the below section.

Space Between Flutter Text And Underline Implementation Source Code

Space between Flutter text and underline

import 'package:flutter/material.dart';
void main() {
  runApp(const MyApp());
}
class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      debugShowCheckedModeBanner: false,
      home: Homepage(),
    );
  }
}
class Homepage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return SafeArea(
        child: Scaffold(
            body: Center(
                child: Padding(
      padding: const EdgeInsets.all(15),
      child: Text('Custom space between text and thick underline',
          style: TextStyle(
              color: Colors.transparent,
              decorationColor: Colors.grey,
              shadows: [Shadow(color: Colors.black, offset: Offset(0, -10))],
              decoration: TextDecoration.underline,
              decorationThickness: 3)),
    ))));
  }
}

Conclusion

To conclude this post, now you have a complete implementation knowledge of how to use and implement space between Flutter text and underline. Don’t hesitate to share your valuable thoughts about this post. Thanks for reading this article.

Leave a Comment

Your email address will not be published.