Sub topic 5
Mixing styles with `Text.rich`
This chapter assumes basic Dart syntax, Flutter widgets, StatelessWidget, the build method, and a working Flutter project. It also assumes that you know how a normal Text widget displays one string.
A Text.rich widget displays one paragraph made from a tree of TextSpan objects. Each span can provide its own text and style. A child span inherits properties from its parent unless it overrides them.
Build a paragraph with mixed styles
The parent TextSpan contains the ordinary text. Its children list contains spans with different formatting.
import 'package:flutter/material.dart';
void main() {
runApp(const MaterialApp(home: PricePage()));
}
class PricePage extends StatelessWidget {
const PricePage({super.key});
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: const Text('Order')),
body: const Center(
child: Text.rich(
TextSpan(
text: 'Total: ',
style: TextStyle(fontSize: 18),
children: [
TextSpan(
text: 'USD 42',
style: TextStyle(fontWeight: FontWeight.bold),
),
TextSpan(text: ' including tax'),
],
),
textAlign: TextAlign.center,
),
),
);
}
}The parent span sets the font size. The USD 42 span adds bold weight. The final span inherits the parent size but keeps normal weight. Flutter lays out all spans as one paragraph, so the text wraps and aligns together.
A TextSpan is not a widget. It describes part of the text used by Text.rich. You cannot place layout widgets, buttons, or padding inside a span. Use a Row, Wrap, or another widget layout when the parts need separate layout behavior.
Control inheritance and themes
You can start the paragraph with a theme style and override only the properties that differ:
Text.rich(
TextSpan(
text: 'Read the ',
style: Theme.of(context).textTheme.bodyLarge,
children: [
TextSpan(
text: 'terms',
style: Theme.of(context).textTheme.bodyLarge?.copyWith(
color: Colors.indigo,
fontWeight: FontWeight.bold,
),
),
const TextSpan(text: ' before continuing.'),
],
),
maxLines: 2,
overflow: TextOverflow.ellipsis,
)Text.rich accepts many of the same layout settings as Text, including textAlign, maxLines, and overflow. This makes a styled paragraph behave predictably in a constrained area.
To replace every inherited property, create a style with inherit: false. Usually, inheritance is preferable because it keeps the paragraph consistent with the app theme.
Accessibility and interaction
Screen readers generally read the spans as one text flow. Keep the complete meaning in the text, and do not rely on color or bold weight alone to communicate important information.
For an inline link or action, a TextSpan can use a TapGestureRecognizer. The recognizer must be disposed when it is owned by a stateful object. For simple static content, avoid adding a recognizer unnecessarily.
Exercise
Which part of the example inherits the parent font size, and how could you make one span use a different size?
Show answer
Both child spans inherit the parent size. Give the selected child a TextStyle(fontSize: 24) or use copyWith(fontSize: 24) to override it.