Flutter Tutorials

Flutter app development tutorials


flutter – How to change Text underline thickness

Flutter – Text Underline Thickness

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 then 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 make the underline bolder instead default height/thickness. That means we will change the text underline’s line height/thickness to make it thinner or thicker than the default value. 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 decorationThickness property to change the text underline’s line/stroke thickness.

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 decorationThickness property value is a double instance that defines the text underline’s thickness.

The default value of the decorationThickness property is 1.0, which will use the font’s base stroke thickness/width. The thickness of the decoration stroke is a multiplier of the thickness defined by the font. The decorationThickness property may be used to achieve a thinner or thicker decoration stroke, without changing the font size.

So finally, the flutter app developers can put an underline on a Text by using the TextStyle class’s decoration property. They also have to set the decoration property value to the underline constant as TextDecoration.underline. And the flutter developers can change the text underline’s line thickness by using the TextStyle class’s decorationThickness property.

main.dart


import 'package:flutter/material.dart';

void main(){
  runApp(
      MaterialApp(
        home: Scaffold(
          appBar: AppBar(
              title: const Text("Flutter - Text Underline Thickness")
          ),
          body: const Center(
            child: Text(
              "Lorem Ipsum is simply dummy text of the printing"
                  " and typesetting industry.",
              style: TextStyle(
                  fontSize: 25,
                  decoration: TextDecoration.underline,
                  decorationThickness: 2
              ),
              textAlign: TextAlign.center,
            ),
          ),
        ),
      )
  );
}

More flutter tutorials



About Me

Flutter Developer

Design a site like this with WordPress.com
Get started