Validate the contrastLevel during ColorScheme creation (#150176)

Implement as an `assert` to help developers catch potential mistakes during development.

Related to https://github.com/flutter/flutter/issues/149683
This commit is contained in:
Parker Lougheed
2024-06-13 19:07:48 -05:00
committed by GitHub
parent dd2ac7cdbe
commit 9ea5b1383f
2 changed files with 23 additions and 0 deletions

View File

@@ -1898,6 +1898,10 @@ class ColorScheme with Diagnosticable {
DynamicSchemeVariant schemeVariant,
double contrastLevel,
) {
assert(
contrastLevel >= -1.0 && contrastLevel <= 1.0,
'contrastLevel must be between -1.0 and 1.0 inclusive.',
);
final bool isDark = brightness == Brightness.dark;
final Hct sourceColor = Hct.fromInt(seedColor.value);
return switch (schemeVariant) {

View File

@@ -639,6 +639,25 @@ void main() {
}, skip: isBrowser, // [intended] uses dart:isolate and io.
);
test('fromSeed() asserts on invalid contrast levels', () {
expect(() {
ColorScheme.fromSeed(seedColor: Colors.blue, contrastLevel: -1.5);
}, throwsAssertionError);
expect(() {
ColorScheme.fromSeed(seedColor: Colors.blue, contrastLevel: 1.5);
}, throwsAssertionError);
});
test('fromImageProvider() asserts on invalid contrast levels', () async {
final Uint8List blueSquareBytes = Uint8List.fromList(kBlueSquarePng);
final ImageProvider image = MemoryImage(blueSquareBytes);
expect(() => ColorScheme.fromImageProvider(provider: image, contrastLevel: -1.5), throwsAssertionError);
expect(() => ColorScheme.fromImageProvider(provider: image, contrastLevel: 1.5), throwsAssertionError);
});
test('fromImageProvider() propagates TimeoutException when image cannot be rendered', () async {
final Uint8List blueSquareBytes = Uint8List.fromList(kBlueSquarePng);