TExcelTAD
Home › Flutter › Text & Styling

Flutter Text & Styling

The Text widget displays a string with a single style. It is probably the widget you will use most. Its appearance is controlled by a TextStyle, and layout behaviour (alignment, line limits, overflow) by properties on Text itself.

Basic Text and TextStyle

flutter · try it yourself
import 'package:flutter/material.dart';

void main() => runApp(const MaterialApp(home: Scaffold(body: Center(child: StyledTexts()))));

class StyledTexts extends StatelessWidget {
  const StyledTexts({super.key});

  @override
  Widget build(BuildContext context) {
    return const Column(
      mainAxisSize: MainAxisSize.min,
      children: [
        Text('Plain text'),
        Text('Big and bold', style: TextStyle(fontSize: 28, fontWeight: FontWeight.bold)),
        Text('Italic teal', style: TextStyle(fontStyle: FontStyle.italic, color: Colors.teal)),
        Text('Spaced out', style: TextStyle(letterSpacing: 6)),
        Text('Underlined', style: TextStyle(decoration: TextDecoration.underline)),
        Text('Struck through', style: TextStyle(decoration: TextDecoration.lineThrough, color: Colors.grey)),
      ],
    );
  }
}

Common TextStyle properties:

Property Example Effect
fontSize 18 Size in logical pixels
fontWeight FontWeight.w600 Thickness from w100 to w900 (bold = w700)
fontStyle FontStyle.italic Italic
color Colors.indigo Text colour
letterSpacing / wordSpacing 1.5 Extra space between letters/words
height 1.4 Line height as a multiple of font size
decoration TextDecoration.underline Underline, overline, line-through
fontFamily 'RobotoMono' A font registered in pubspec.yaml

Alignment, line limits and overflow

When text is longer than the available width it wraps. You can limit lines and decide what happens to the rest:

flutter · try it yourself
import 'package:flutter/material.dart';

const blurb = 'Flutter draws every pixel itself, which means your text looks the '
    'same on Android, iOS, web and desktop — no surprises between platforms.';

void main() => runApp(const MaterialApp(
      home: Scaffold(
        body: Padding(
          padding: EdgeInsets.all(24),
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.stretch,
            children: [
              Text(blurb, textAlign: TextAlign.center),
              SizedBox(height: 16),
              Text(blurb, maxLines: 1, overflow: TextOverflow.ellipsis),
              SizedBox(height: 16),
              Text(blurb, maxLines: 2, overflow: TextOverflow.fade, textAlign: TextAlign.justify),
            ],
          ),
        ),
      ),
    ));

Using the theme's text styles

Instead of hard-coding sizes everywhere, use the type scale from the theme. It keeps your app consistent and respects the user's font-size settings:

flutter · try it yourself
import 'package:flutter/material.dart';

void main() => runApp(const MaterialApp(home: ArticlePage()));

class ArticlePage extends StatelessWidget {
  const ArticlePage({super.key});

  @override
  Widget build(BuildContext context) {
    final text = Theme.of(context).textTheme;
    return Scaffold(
      body: Padding(
        padding: const EdgeInsets.all(20),
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            Text('Morning Run', style: text.headlineMedium),
            Text('5.2 km · 28 min', style: text.titleMedium?.copyWith(color: Colors.grey)),
            const SizedBox(height: 12),
            Text('A calm loop around the lake before work.', style: text.bodyLarge),
          ],
        ),
      ),
    );
  }
}

copyWith() returns a copy of a style with some properties replaced — the standard way to tweak a theme style. The ?. is needed because theme styles are nullable.

Mixing styles with Text.rich

Text applies one style to the whole string. To style parts differently, build a tree of TextSpans:

flutter · try it yourself
import 'package:flutter/material.dart';

void main() => runApp(const MaterialApp(
      home: Scaffold(
        body: Center(
          child: Text.rich(
            TextSpan(
              style: TextStyle(fontSize: 18, color: Colors.black87),
              children: [
                TextSpan(text: 'Total: '),
                TextSpan(text: '\$42.50', style: TextStyle(fontWeight: FontWeight.bold, color: Colors.green)),
                TextSpan(text: ' (incl. tax)', style: TextStyle(fontSize: 12, color: Colors.grey)),
              ],
            ),
          ),
        ),
      ),
    ));

Child spans inherit the parent span's style and override only what they set. Note the \$ — in Dart strings $ starts interpolation, so a literal dollar sign must be escaped.

Tip: To use a custom font, add the .ttf files under fonts: in pubspec.yaml and set fontFamily. Or use the google_fonts package for hundreds of fonts with one line of code.

Test yourself

Which property shows … when text does not fit on its last allowed line?

  1. softWrap: false
  2. overflow: TextOverflow.ellipsis
  3. textAlign: TextAlign.end
  4. style: TextStyle(ellipsis: true)

How do you style different words of one sentence differently?

  1. Use several Text widgets in a Stack
  2. Use Text.rich with TextSpan children
  3. Set TextStyle.multi
  4. It is not possible in Flutter

What does style.copyWith(color: Colors.red) return?

  1. The same style object, modified
  2. A new TextStyle with only the colour changed
  3. A Text widget
  4. null