Skip to content

[Linter] use_padding lint rule #59563

@ricardodalarme

Description

@ricardodalarme

Proposal for New Dart Lint Rule

TLDR

Add lint rule to avoid using Container with only a margin property.

Motivation

Using Container with only a margin property introduces an unnecessary widget in the widget tree, as Container adds a range of unused functionalities (decoration, padding, alignment) in this case. Replacing Container with Padding improves performance and makes the widget tree more readable. Padding is also a constant widget, which tends to be more performative.

Alternatives

Instead of creating a new rule, this could be added as an enhancement to the existing avoid_unnecessary_containers rule.

Lint Rule Implementation

  • Rule Name: use_padding
  • Description: Warns when a Container is used with only a margin property, suggesting the use of Padding instead.
  • Severity Level: WARNING (since it is a performance and readability improvement, not a breaking issue)

Code Examples

Bad
Container(
  margin: EdgeInsets.all(8),
  child: Text("Hello, World!"),
);
Good
Padding(
  padding: EdgeInsets.all(8),
  child: Text("Hello, World!"),
);

Metadata

Metadata

Assignees

No one assigned

    Labels

    P2A bug or feature request we're likely to work onarea-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-set-fluttertype-enhancementA request for a change that isn't a bug

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions