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.
Why use this rule
Section titled “Why use this rule”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 scalingRichText( text: TextSpan( text: 'Hello ', children: [ TextSpan( text: 'bold', style: TextStyle(fontWeight: FontWeight.bold), ), TextSpan(text: ' world!'), ], ),);
// Even simple RichText should use Text.richRichText(text: TextSpan(text: 'Simple text'));// Text.rich handles text scaling and inherits default styleText.rich( TextSpan( text: 'Hello ', children: [ TextSpan( text: 'bold', style: TextStyle(fontWeight: FontWeight.bold), ), TextSpan(text: ' world!'), ], ),);
Text.rich(TextSpan(text: 'Simple text'));Configuration
Section titled “Configuration”To disable this rule:
plugins: many_lints: diagnostics: prefer_text_rich: false