CodeBlanks Logo Icon CodeBlanks

The Text widget

Learn to display text with the Text widget.

The Text widget

The Text widget is used to display text on screen. In its simplest form it takes a String and shows it. You can set the font with the style parameter and the alignment with textAlign. Text is an immutable widget extending StatelessWidget.

Text('Merhaba Flutter')

Text(
  'Uzun bir metin',
  style: TextStyle(fontSize: 16),
  textAlign: TextAlign.center,
)

Text parameters

The most commonly used parameters of the Text widget are: data (the text to show), style (formatting via TextStyle), textAlign (horizontal alignment), maxLines (maximum line count) and overflow (behavior for overflowing text). textAlign can be TextAlign.left, TextAlign.center or TextAlign.right.

Text(
  'Hizalı metin',
  textAlign: TextAlign.center,
  maxLines: 2,
  overflow: TextOverflow.ellipsis,
  style: TextStyle(fontSize: 18),
)

Practice Print Text info

In main, print two lines with print: text metin and style bicim.

Starter code

main.dart
void main() {
  // iki satır yazdır:
  // text metin
  // style bicim

}

Sample solution

main.dart
void main() {
  print('text metin');
  print('style bicim');
}

Expected output

text metin
style bicim

Questions and answers

Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.

1 What is the Text widget used for?

  • To display images
  • To play sound
  • To create buttons
  • To display text on screen Correct answer

Explanation: The Text widget is used to display text on screen; in its simplest form it takes a String.

2 Align the text

Question
Text(
  'Metin',
  ____: TextAlign.center,
)
Answer
Text(
  'Metin',
  textAlign: TextAlign.center,
)

Explanation: The textAlign parameter sets the horizontal alignment of the text.

3 Text concepts

Term Meaning
Text Widget that displays text
style Font formatting parameter
TextAlign Horizontal text alignment
maxLines Maximum line count

All lessons in Beginner