Flutter RichText Widget

CodeWithFlutter
2 Min Read

The RichText widget in Flutter is a widget for styling a paragraph of mixed-style text. It displays text that uses multiple different styles. Each text span in the paragraph can have a different style, such as font size, color, and font weight.

Here’s a basic example of using RichText in Flutter:

RichText(
  text: TextSpan(
    style: DefaultTextStyle.of(context).style,
    children: <TextSpan>[
      TextSpan(text: 'Bold text: ', style: TextStyle(fontWeight: FontWeight.bold)),
      TextSpan(text: 'Normal text'),
    ],
  ),
)

In this example, the first text span has a bold font weight, while the second text span has the default font weight.

Some of the properties that you can use with the RichText widget include:

  • text: The text to display in the RichText widget. It’s specified as a TextSpan object.
  • textAlign: The horizontal alignment of the text. The default value is TextAlign.start.
  • textDirection: The direction of the text. The default value is TextDirection.ltr.
  • textScaleFactor: The scaling factor for the text. The default value is 1.0.
  • maxLines: The maximum number of lines to display. If the text overflows, it will be truncated and an ellipsis will be displayed.
  • softWrap: Specifies whether the text should wrap. The default value is true.
Share this Article
Leave a comment