Rules / Performance / prefer-correct-edge-insets-constructor

prefer-correct-edge-insets-constructor

lint/performance/prefer-correct-edge-insets-constructor
recommendedflutter
Same as prefer-correct-edge-insets-constructor · Dart Code Metrics

Flags an EdgeInsets.only(...) that a more specific constructor expresses better.

EdgeInsets.only with symmetric values is clearer and cheaper as EdgeInsets.all(n) (all four sides equal), EdgeInsets.symmetric(vertical: n) (only top and bottom, equal), or EdgeInsets.symmetric(horizontal: n) (only left and right, equal). The rule detects these three shapes on EdgeInsets.only(...) (and a direct EdgeInsets(...) carrying the same named arguments) and reports the equivalent to prefer.

Invalid

example.dartdart
// Test cases for prefer-correct-edge-insets-constructor rule
// Flags EdgeInsets.only() where a simpler constructor should be used

void testSymmetricVertical() {
  final padding = EdgeInsets.only(top: 8, bottom: 8);
}

void testSymmetricHorizontal() {
  final padding = EdgeInsets.only(left: 4, right: 4);
}

void testAllEqual() {
  final padding = EdgeInsets.only(left: 4, top: 4, right: 4, bottom: 4);

  final padding2 = EdgeInsets.only(
    left: 16,
    top: 16,
    right: 16,
    bottom: 16,
  );
}

void testMultipleViolations() {
  final pad1 = EdgeInsets.only(top: 10, bottom: 10);
  final pad2 = EdgeInsets.only(left: 8, right: 8);
  final pad3 = EdgeInsets.only(left: 12, top: 12, right: 12, bottom: 12);
}

class MyWidget {
  final padding = EdgeInsets.only(top: 20, bottom: 20);
}
EdgeInsets.only() should use EdgeInsets.symmetric() or EdgeInsets.all().
4void testSymmetricVertical() {
5 final padding = EdgeInsets.only(top: 8, bottom: 8);
EdgeInsets.only() should use EdgeInsets.symmetric() or EdgeInsets.all().
8void testSymmetricHorizontal() {
9 final padding = EdgeInsets.only(left: 4, right: 4);
EdgeInsets.only() should use EdgeInsets.symmetric() or EdgeInsets.all().
12void testAllEqual() {
13 final padding = EdgeInsets.only(left: 4, top: 4, right: 4, bottom: 4);
EdgeInsets.only() should use EdgeInsets.symmetric() or EdgeInsets.all().
14
15 final padding2 = EdgeInsets.only(
EdgeInsets.only() should use EdgeInsets.symmetric() or EdgeInsets.all().
23void testMultipleViolations() {
24 final pad1 = EdgeInsets.only(top: 10, bottom: 10);
EdgeInsets.only() should use EdgeInsets.symmetric() or EdgeInsets.all().
24 final pad1 = EdgeInsets.only(top: 10, bottom: 10);
25 final pad2 = EdgeInsets.only(left: 8, right: 8);
EdgeInsets.only() should use EdgeInsets.symmetric() or EdgeInsets.all().
25 final pad2 = EdgeInsets.only(left: 8, right: 8);
26 final pad3 = EdgeInsets.only(left: 12, top: 12, right: 12, bottom: 12);
EdgeInsets.only() should use EdgeInsets.symmetric() or EdgeInsets.all().
29class MyWidget {
30 final padding = EdgeInsets.only(top: 20, bottom: 20);

Valid

example.dartdart
// Good examples for prefer-correct-edge-insets-constructor rule
// Using simpler constructors when appropriate

void testSymmetricConstructor() {
  final padding1 = EdgeInsets.symmetric(vertical: 8);
  final padding2 = EdgeInsets.symmetric(horizontal: 4);
  final padding3 = EdgeInsets.symmetric(vertical: 10, horizontal: 16);
}

void testAllConstructor() {
  final padding = EdgeInsets.all(4);
  final padding2 = EdgeInsets.all(16);
}

void testOnlyWithAsymmetricValues() {
  final padding = EdgeInsets.only(
    top: 8,
    bottom: 12,
    left: 4,
    right: 6,
  );

  final padding2 = EdgeInsets.only(top: 10, left: 5);
}

void testZeroConstructors() {
  final padding1 = EdgeInsets.zero;
  final padding2 = EdgeInsets.only(left: 8, right: 16);
}

How to configure

Set the severity of prefer-correct-edge-insets-constructor in your falcon.json:

falcon.jsonjson
{
  "linter": {
    "rules": {
      "performance": {
        "prefer-correct-edge-insets-constructor": "error"
      }
    }
  }
}