Flutter Tutorials

Flutter app development tutorials


flutter – How to change Text underline color

Flutter – Text Underline Color

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 show a different color for the underline instead the default color. 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 decorationColor property to change the underline color.

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 decorationColor property value is a Color which is the color in which to paint the text decorations.

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 color using the TextStyle class’s decorationColor property.

main.dart


import 'package:flutter/material.dart';

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

More flutter tutorials



About Me

Flutter Developer

Design a site like this with WordPress.com
Get started