Skip to content

prefer_text_rich

v0.4.0 Warning Fix Widget Replacement

Flags usages of RichText which should be replaced with Text.rich. RichText does not respect MediaQuery text scaling by default, which can break accessibility for users who configure larger text sizes.

Text.rich inherits the default TextStyle from the nearest DefaultTextStyle ancestor and automatically applies text scaling from MediaQuery. RichText does neither — it requires you to pass these explicitly. Using Text.rich gives you correct accessibility behavior out of the box and is the recommended approach for rich text in Flutter.

See also: Text.rich | RichText

// RichText does not handle text scaling
RichText(
text: TextSpan(
text: 'Hello ',
children: [
TextSpan(
text: 'bold',
style: TextStyle(fontWeight: FontWeight.bold),
),
TextSpan(text: ' world!'),
],
),
);
// Even simple RichText should use Text.rich
RichText(text: TextSpan(text: 'Simple text'));
// Text.rich handles text scaling and inherits default style
Text.rich(
TextSpan(
text: 'Hello ',
children: [
TextSpan(
text: 'bold',
style: TextStyle(fontWeight: FontWeight.bold),
),
TextSpan(text: ' world!'),
],
),
);
Text.rich(TextSpan(text: 'Simple text'));

To disable this rule:

plugins:
many_lints:
diagnostics:
prefer_text_rich: false