Skip to content

Commit

Permalink
fix: warnings-false-detection-in-ColorScheme-definition
Browse files Browse the repository at this point in the history
  • Loading branch information
boywithdv committed Nov 1, 2024
1 parent e929401 commit 308729d
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 4 deletions.
21 changes: 20 additions & 1 deletion packages/altive_lints/example/lints/avoid_hardcoded_color.dart
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,28 @@ class MyWidget extends StatelessWidget {
// expect_lint: avoid_hardcoded_color
const ColoredBox(color: Colors.green),
ColoredBox(color: Theme.of(context).colorScheme.primary),

ColoredBox(color: _colorScheme.primary),
const ColoredBox(color: Colors.transparent),
],
);
}
}
ColorScheme get _colorScheme => const ColorScheme(
primary:Color.fromRGBO(0, 255, 0, 1),
secondary: Color(0xFFF45479),
onSecondary: Color(0xFFFFFFFF),
error: Color(0xFFFF3131),
surface: Color(0xFFFEFEFE),
surfaceDim: Color(0x1F787880),
surfaceTint: Color(0xFFF5F5F5),
surfaceContainerLowest: Color(0xFFF7F7F7),
surfaceContainerLow: Color(0xFFFAFBFC),
surfaceContainerHighest: Color(0xFFFFFFFF),
onSurface: Color(0xFF181A1F),
onSurfaceVariant: Color(0xFF989898),
outline: Color(0xFFDCDCDC),
scrim: Colors.black54,
onPrimary: Color.fromRGBO(100, 100, 100, 11),
onError: Color(0xFFFF3131),
brightness: Brightness.dark,
);
19 changes: 16 additions & 3 deletions packages/altive_lints/lib/src/lints/avoid_hardcoded_color.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import 'package:analyzer/dart/ast/ast.dart';
import 'package:analyzer/dart/element/element.dart';
import 'package:analyzer/dart/element/type.dart';
import 'package:analyzer/error/listener.dart';
Expand Down Expand Up @@ -48,7 +49,7 @@ class AvoidHardcodedColor extends DartLintRule {
context.registry.addInstanceCreationExpression((node) {
final typeName = node.staticType?.getDisplayString();

if (typeName == 'Color') {
if (typeName == 'Color' && !_isInsideColorScheme(node)) {
reporter.atNode(node, _code);
}
});
Expand All @@ -59,11 +60,11 @@ class AvoidHardcodedColor extends DartLintRule {
final element = node.staticElement;
if (element is PropertyAccessorElement) {
final returnType = element.returnType;
// Allow Colors.transparent as a valid hardcoded color, as it serves.
// Allow Colors.transparent as a valid hardcoded color.
if (node.identifier.name == 'transparent') {
return;
}
if (_isColorType(returnType)) {
if (_isColorType(returnType) && !_isInsideColorScheme(node)) {
reporter.atNode(node, _code);
}
}
Expand All @@ -78,4 +79,16 @@ class AvoidHardcodedColor extends DartLintRule {
type.getDisplayString() == 'MaterialColor' ||
type.getDisplayString() == 'MaterialAccentColor');
}

bool _isInsideColorScheme(AstNode node) {
var parent = node.parent;
while (parent != null) {
if (parent is InstanceCreationExpression &&
parent.staticType?.getDisplayString() == 'ColorScheme') {
return true;
}
parent = parent.parent;
}
return false;
}
}

0 comments on commit 308729d

Please sign in to comment.