Skip to content

several_public_widgets_in_file

Reports each public widget of a file after the first. Make the helper widgets private, or give each public widget a file of its own.

Tier
advisory
Scope
Flutter
Lowest preset
strict
Fix
none

Each public widget class of a file after the first, at its name. A class counts if it is not abstract, has a public name and is a subtype of Flutter’s Widget.

It does not count:

  • a private widget, a helper used only in its library;
  • a State, which belongs to its widget;
  • an abstract widget, a base for others;
  • any class that is not a widget.

A reader looks for a widget in the file named after it. A widget that other files use is easier to find, review and move when it has a file of its own.

Violating
profile_page.dart
class ProfilePage extends StatelessWidget { … }
class ProfileHeader extends StatelessWidget { … }
Corrected
profile_page.dart
class ProfilePage extends StatelessWidget { … }
class _ProfileHeader extends StatelessWidget { … }

Set under the assay: key of the project’s root analysis_options.yaml.

Rule parameter Default Effect
enabled true false switches the rule off for the whole project.
exclude [] Path globs where the rule is silent.

Write why on the line directly above the suppression:

// Why it stays as written here.
// ignore: assay/several_public_widgets_in_file

// ignore_for_file: assay/several_public_widgets_in_file silences it in a whole file, and // ignore: several_public_widgets_in_file without the prefix works too. See Suppressions.