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:
@@ -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) {
|
||||
|
||||
@@ -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);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user