diff --git a/packages/altive_lints/example/lints/avoid_single_child.dart b/packages/altive_lints/example/lints/avoid_single_child.dart index 7093cfd..a6d2204 100644 --- a/packages/altive_lints/example/lints/avoid_single_child.dart +++ b/packages/altive_lints/example/lints/avoid_single_child.dart @@ -7,6 +7,7 @@ class MyWidget extends StatelessWidget { @override Widget build(BuildContext context) { + final isValued = Random().nextBool(); final random = Random(); return ListView( children: [ @@ -59,7 +60,6 @@ class MyWidget extends StatelessWidget { if (random.nextBool()) const Text('Hello World'), ], ), - // expect_lint: avoid_single_child Column( children: [ if (random.nextBool()) @@ -70,6 +70,56 @@ class MyWidget extends StatelessWidget { ], ], ), + // expect_lint: avoid_single_child + Column( + children: [ + if(isValued)...[ + Container(), + ]else...[ + Container(), + ], + ], + ), + Column( + children: [ + if(isValued)...[ + Container(), + ], + Container(), + ], + ), + // expect_lint: avoid_single_child + Column( + children: [ + if(isValued)...[ + Container(), + ], + ], + ), + Column( + children: [ + if(isValued)...[ + Container(), + Container(), + ], + ], + ), + // expect_lint: avoid_single_child + Column( + children: [ + if(isValued)...[] + else...[] + ], + ), + // expect_lint: avoid_single_child + Column( + children: [ + if(isValued)...[] + else...[ + Container(), + ] + ], + ), Column( children: random.nextBool() ? [ diff --git a/packages/altive_lints/lib/src/lints/avoid_single_child.dart b/packages/altive_lints/lib/src/lints/avoid_single_child.dart index ab86d87..a47c3c8 100644 --- a/packages/altive_lints/lib/src/lints/avoid_single_child.dart +++ b/packages/altive_lints/lib/src/lints/avoid_single_child.dart @@ -76,7 +76,18 @@ class AvoidSingleChild extends DartLintRule { if (childrenList.elements.length != 1) { return; } + for (final element in childrenList.elements) { + if (element is IfElement) { + if (_hasMultipleChild(element.thenElement)) { + return; + } + if (element.elseElement case final CollectionElement ce + when _hasMultipleChild(ce)) { + return; + } + } + } final element = childrenList.elements.first; if (element is ForElement) { return; @@ -85,4 +96,12 @@ class AvoidSingleChild extends DartLintRule { } }); } + + bool _hasMultipleChild(CollectionElement element) { + if (element is SpreadElement && element.expression is ListLiteral) { + final spreadElement = element.expression as ListLiteral; + return spreadElement.elements.length > 1; + } + return false; + } }