Skip to content

Flutter lint rule to prefer Text.rich over RichText #60334

Open
@IchordeDionysos

Description

@IchordeDionysos

prefer_text_over_rich_text

Description

Avoid using Text.rich() over RichText.

Details

RichText does not automatically scale it's font size based on device text scale settings.
Rich.text does adjust to the text scale factor settings on the device making it the preferred option.

Kind

Helps engineers build accessible Flutter apps.

Bad Examples

Widget build(BuildContext context) {
  return RichText(
    text: TextSpan(
      text: "Here is some text.",
      style: Theme.of(context).textTheme.bodyText2,
    ),
  );
}
Widget build(BuildContext context) {
  return RichText(
    text: TextSpan(
      text: "Here is some text.",
      style: Theme.of(context).textTheme.bodyText2,
      children: [
        TextSpan(
          text: " (some more text)",
          style: Theme.of(context).textTheme.bodyText1,
        ),
      ],
    ),
  );
}

Good Examples

Widget build(BuildContext context) {
  return Text.rich(
    TextSpan(
      text: "Here is some text.",
      style: Theme.of(context).textTheme.bodyText2,
    ),
  );
}
Widget build(BuildContext context) {
  return Text.rich(
    TextSpan(
      text: "Here is some text.",
      style: Theme.of(context).textTheme.bodyText2,
      children: [
        TextSpan(
          text: " (some more text)",
          style: Theme.of(context).textTheme.bodyText1,
        ),
      ],
    ),
  );
}

Discussion

From a accessibility-by-design/accessibility-by-default perspective, this lint would improve the developers and users experiences greatly.

There may be good reasons to use RichText if you need full control over it. However, in the majority of the cases Text.rich is the preferred option.
Engineers may not be aware of this fact, making this lint a great option to help engineers build accessible apps.

We have faced this issues in an internal app, and instead of having to educate people not to use RichText or to keep an eye out for it in PRs.
Having this automatically checked would be super helpful.

Reference: flutter/flutter#61452 (comment)

Discussion checklist

  • List any existing rules this proposal modifies, complements, overlaps or conflicts with.
  • List any relevant issues (reported here, the SDK Tracker, or elsewhere).
  • If there's any prior art (e.g., in other linters), please add references here.
  • If this proposal corresponds to Effective Dart or Flutter Style Guide advice, please call it out. (If there isn't any corresponding advice, should there be?)
  • If this proposal is motivated by real-world examples, please provide as many details as you can. Demonstrating potential impact is especially valuable.

Metadata

Metadata

Assignees

No one assigned

    Labels

    P3A lower priority bug or feature requestarea-devexpFor issues related to the analysis server, IDE support, linter, `dart fix`, and diagnostic messages.devexp-linterIssues with the analyzer's support for the linter packagelinter-lint-proposal

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions