Skip to content

Commit 4dc4b4e

Browse files
boywithdvk-nkmr
andauthored
fix: Resolved warnings false detection in ColorScheme definition (#73)
* fix: warnings-false-detection-in-ColorScheme-definition * fix: selfreview * fix: Define in colorscheme only the colors to be used. * Update packages/altive_lints/lib/src/lints/avoid_hardcoded_color.dart Co-authored-by: nkmr <[email protected]> * chore: revision * fix: revision --------- Co-authored-by: nkmr <[email protected]>
1 parent 02d1848 commit 4dc4b4e

File tree

2 files changed

+20
-1
lines changed

2 files changed

+20
-1
lines changed

packages/altive_lints/example/lints/avoid_hardcoded_color.dart

+4-1
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,12 @@ class MyWidget extends StatelessWidget {
1414
// expect_lint: avoid_hardcoded_color
1515
const ColoredBox(color: Colors.green),
1616
ColoredBox(color: Theme.of(context).colorScheme.primary),
17-
17+
ColoredBox(color: _colorScheme.primary),
1818
const ColoredBox(color: Colors.transparent),
1919
],
2020
);
2121
}
2222
}
23+
ColorScheme get _colorScheme => const ColorScheme.dark(
24+
primary:Color.fromRGBO(0, 255, 0, 1),
25+
);

packages/altive_lints/lib/src/lints/avoid_hardcoded_color.dart

+16
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import 'package:analyzer/dart/ast/ast.dart';
12
import 'package:analyzer/dart/element/element.dart';
23
import 'package:analyzer/dart/element/type.dart';
34
import 'package:analyzer/error/listener.dart';
@@ -46,6 +47,9 @@ class AvoidHardcodedColor extends DartLintRule {
4647
CustomLintContext context,
4748
) {
4849
context.registry.addInstanceCreationExpression((node) {
50+
if (_isInsideColorScheme(node)) {
51+
return;
52+
}
4953
final typeName = node.staticType?.getDisplayString();
5054

5155
if (typeName == 'Color') {
@@ -78,4 +82,16 @@ class AvoidHardcodedColor extends DartLintRule {
7882
type.getDisplayString() == 'MaterialColor' ||
7983
type.getDisplayString() == 'MaterialAccentColor');
8084
}
85+
86+
bool _isInsideColorScheme(AstNode node) {
87+
var parent = node.parent;
88+
while (parent != null) {
89+
if (parent is InstanceCreationExpression &&
90+
parent.staticType?.getDisplayString() == 'ColorScheme') {
91+
return true;
92+
}
93+
parent = parent.parent;
94+
}
95+
return false;
96+
}
8197
}

0 commit comments

Comments
 (0)