Open ricardodalarme opened 2 weeks ago
Summary: This issue proposes a new Dart lint rule, prefer_padding_over_margin_container
, to warn developers when using Container
with only a margin
property, suggesting the use of Padding
instead. This is because Padding
is more performant and results in a cleaner widget tree.
CC @gspencergoog what do you think of this suggested lint rule? Would flutter recommend this rule?
It's just another type of unnecessary Container
, so my first inclination was to add it to avoid_unnecessary_containers
.
However, it looks like we already have one for Container
s that only have a color (ColoredBox
) (use_colored_box
) or only have a decoration (replace with DecoratedBox
)(use_decorated_box
), so maybe the granularity of the lint messages is finer than I thought. In that case, we should probably add one for each of these cases too:
use_padding
Container
s that only have a margin (replace with Padding
) (as OP suggests)Container
s that only have a padding (replace with Padding
)use_align
Container
s that only have an alignment (replace with Align
, or Center
when the alignment is Alignment.center
)We could also extend this behavior to other props as well:
Container
with only alignment -> Align
widgetContainer
with only transform -> Transform
widgetContainer
with only constraints -> ConstrainedBox
widgetWe also have sized_box_for_whitespace
, but we should probably also recommend SizedBox
if the container only has width
and/or height
set. (called use_sized_box
?)
Proposal for New Dart Lint Rule
TLDR
Add lint rule to avoid using
Container
with only amargin
property.Motivation
Using
Container
with only amargin
property introduces an unnecessary widget in the widget tree, asContainer
adds a range of unused functionalities (decoration, padding, alignment) in this case. ReplacingContainer
withPadding
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
use_padding
Container
is used with only amargin
property, suggesting the use ofPadding
instead.Code Examples
Bad
Good