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: 'Total: ', children: [ TextSpan( text: '42 USD', style: TextStyle(fontWeight: FontWeight.bold), ), TextSpan(text: ' incl. VAT'), ], ),);
// Even simple RichText should use Text.richRichText(text: TextSpan(text: 'Simple text'));// Text.rich handles text scaling and inherits default styleText.rich( TextSpan( text: 'Total: ', children: [ TextSpan( text: '42 USD', style: TextStyle(fontWeight: FontWeight.bold), ), TextSpan(text: ' incl. VAT'), ], ),);
Text.rich(TextSpan(text: 'Simple text'));Configuration
Section titled “Configuration”This rule is in the recommended preset, so it is on with
preset: recommended or preset: opinionated. Add it to preset: core with
prefer_text_rich: true.
To turn it off:
rules: prefer_text_rich: falseTo keep the rule on but skip certain paths, use per-rule exclude.
Related rules
Section titled “Related rules”prefer_center_over_align— Use Center instead of Align when alignment is center.prefer_sized_box_square— Use SizedBox.square when width and height are equal.avoid_border_all— Use Border.fromBorderSide instead of Border.all for const support.avoid_expanded_as_spacer— Use Spacer instead of Expanded with an empty child.