Senger CodeLab πŸš€

How to underline text in flutter

September 29, 2026

πŸ“‚ Categories: Flutter
🏷 Tags: Flutter
How to underline text in flutter

Flutter, Google’s UI toolkit, offers a robust and flexible system for creating beautiful and engaging user interfaces. A seemingly simple yet surprisingly versatile styling element is the underline. Whether you’re highlighting key information, creating interactive elements, or simply adding a touch of visual flair, knowing how to underline text in Flutter is essential for any developer. This article will guide you through various methods, from basic text decoration to more advanced techniques, ensuring your Flutter app stands out with its polished and professional look. We’ll explore the nuances of each approach and provide practical examples to help you implement them effortlessly.

Using TextDecoration

The most straightforward way to underline text in Flutter is using the TextDecoration property within the TextStyle class. This property allows you to apply various text decorations, including underlines, overlines, and line-throughs. Simply set the decoration property to TextDecoration.underline.

Here’s a basic example:

Text( 'Underlined Text', style: TextStyle(decoration: TextDecoration.underline), )This code snippet will render the text “Underlined Text” with a simple underline. This method is ideal for quick and easy underlining, especially when dealing with static text elements.

Customizing the Underline

The TextDecoration class offers further customization options. You can control the style, color, and thickness of the underline. For instance, you might want a thicker, dashed underline in a specific color.

Example:

Text( 'Custom Underline', style: TextStyle( decoration: TextDecoration.underline, decorationStyle: TextDecorationStyle.dashed, decorationThickness: 2.0, decorationColor: Colors.blue, ), )This example showcases a dashed, blue underline with double the default thickness. Experimenting with these properties allows for greater visual control and brand consistency.

Underlining Specific Parts of Text with RichText

Sometimes, you may need to underline only certain portions of a text string. The RichText widget provides the flexibility to apply different styles to different text spans within a single Text widget. This allows for more complex and dynamic text formatting.

Example:

RichText( text: TextSpan( children: <textspan>[ TextSpan(text: 'This is '), TextSpan( text: 'underlined', style: TextStyle(decoration: TextDecoration.underline), ), TextSpan(text: ' text.'), ], ), )</textspan>In this example, only the word “underlined” is underlined, leaving the rest of the text unaffected. This approach is particularly useful for highlighting keywords or phrases within a larger body of text.

Using Underline with Gestures

Underlining is often used to indicate interactive elements, like hyperlinks. Combine the GestureDetector widget with underlined text to create clickable links or buttons.

Example:

GestureDetector( onTap: () { // Action to perform on tap }, child: Text( 'Clickable Underlined Text', style: TextStyle(decoration: TextDecoration.underline, color: Colors.blue), ), )This creates an underlined, blue text that triggers an action when tapped. This technique is crucial for enhancing user interaction and navigation within your Flutter application.

  • Use TextDecoration.underline for simple underlining.
  • Customize underline properties using decorationStyle, decorationThickness, and decorationColor.
  1. Import the ‘flutter/material.dart’ package.
  2. Create a Text widget.
  3. Apply the desired TextStyle with TextDecoration.

For more advanced styling and text manipulation in Flutter, consult the official Flutter documentation. This comprehensive resource provides in-depth information on styling text and other UI elements.

See also: TextDecoration class and Underlining Text Cookbook.

Learn more about Flutter Development.Implementing underlines strategically enhances readability and user experience. Think of it as a visual cue, guiding users through your content and highlighting important information. Don’t underestimate the impact of this simple yet powerful styling element.

Infographic Placeholder

[Infographic illustrating different underline styles and their use cases]

FAQ

Q: How do I change the underline color in Flutter?

A: Use the decorationColor property within the TextStyle.

  • Flutter text decoration
  • Flutter underline text style
  • Customize underline in Flutter
  • Flutter rich text underline
  • Underline specific text Flutter
  • Flutter gesture detector underline
  • Flutter text styling

Mastering text styling, particularly underlining, is a cornerstone of creating visually appealing and user-friendly Flutter applications. By understanding and implementing the techniques outlined in this guide, you can significantly enhance the overall presentation and interactivity of your Flutter projects. Explore the different methods, experiment with customization options, and discover the subtle yet significant impact of the humble underline. Now it’s time to put this knowledge into action and elevate your Flutter UIs to the next level. Start incorporating these underlining techniques in your next project and see the difference it makes.

Question & Answer :
How to underline text in flutter inside Text widget?

I cannot seem to find underline inside fontStyle property of TextStyle

When underlining everything you can set a TextStyle on the Text widget.

enter image description here

Text( 'Hello world', style: TextStyle( decoration: TextDecoration.underline, ), ) 

If you only want to underline part of the text then you need to use Text.rich() (or a RichText widget) and break the string into TextSpans that you can add a style to.

enter image description here

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 is a little strange. The text parameter is the default style but the children list contains the styled (and possibly unstyled) text that follow it. You can use an empty string for text if you want to start with styled text.

You can also add a TextDecorationStyle to change how the decoration looks. Here is dashed:

enter image description here

Text( 'Hello world', style: TextStyle( decoration: TextDecoration.underline, decorationStyle: TextDecorationStyle.dashed, ), ) 

and TextDecorationStyle.dotted:

enter image description here

and TextDecorationStyle.double:

enter image description here

and TextDecorationStyle.wavy:

enter image description here