Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to underline text in flutter

Tags:

flutter

People also ask

How do you underline in text flutters?

Step 1: Inside the Text widget, add the style parameter and assign the TextStyle() class. Step 2: Inside the TextStyle(), add the decoration parameter and assign the TextDecoration. underline value. Step 3: Add one more parameter called decorationStyle and choose among the TextDecorationStyle.

How do you underline text?

The quickest way to underline text is to press Ctrl+U and start typing. When you want to stop underlining, press Ctrl+U again.

How do you space and underline in Flutter?

Example 3: Adding some Space between Text and Underline You can control the gap between text and its underlining by adding a drop shadow and making the original text transparent. The Y offset of the shadow will determine the gap.

How do you get rid of the underline on text flutters?

To remove TextField underline/border in Flutter, you can simply set the border property to InputBorder. none. This will remove the underline for all states such as Focused, Enabled, Error, Disabled.


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


You do it by applying decoration: TextDecoration.underline to TextStyle of a Text.

With Theme example:

          Text(
            "text",
            style: Theme
                .of(context)
                .accentTextTheme
                .subhead
                .copyWith(decoration: TextDecoration.underline),
          )

Basic example:

          Text(
            "text",
            style: TextStyle(decoration: TextDecoration.underline),
          )

Exciting solution
If you want to have control over the distance between the text and the underline, you can use this hack. In short, you hide the actual text using Colors.transparent and then display an offset shadow that hovers above the Text underline.

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

enter image description here

As you'll see below, if you use the out-of-the-box Text underline, it sticks to the bottom of the text and can look a bit ugly,

Boring Solutions

Using just the Text widget you can add the underline with a custom style and color:

Text(
  "Forgot Password?",
  style: TextStyle(
    decoration: TextDecoration.underline,
    decorationColor: Colors.blue,
    decorationThickness: 4,
    decorationStyle: TextDecorationStyle.dashed,
   ),
)

The only issue I have with this approach is that you have no control over how close the underline is to the bottom of the text.

enter image description here

If you want to increase the spacing, you'll have to use an unconventional approach that uses Container and it's padding property.

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

enter image description here

Keep an eye on this GitHub issue for a simpler solution.


You can use TextDecoration in style to underline a given text.

For example

Text(
    "Your text here",
    style: TextStyle(
        decoration: TextDecoration.underline),
    )
)

for example

Text(
  "Terms and Condition",
  style: TextStyle(
    decoration:
        TextDecoration.underline,
    height: 1.5,
    fontSize: 15,
    fontWeight: FontWeight.bold,
    fontFamily: 'Roboto-Regular',
  ),
),