Skip to content

Commit

Permalink
Provide a helpful error message when ColorScheme.brightness doesn't…
Browse files Browse the repository at this point in the history
… match `ThemeData.brightness` (#137611)

fixes [Unexpected behaviour with ColorScheme.fromSeed and Brightness.dark](flutter/flutter#127523)
  • Loading branch information
TahaTesser authored Nov 6, 2023
1 parent aa3436d commit 826bb0a
Show file tree
Hide file tree
Showing 2 changed files with 102 additions and 1 deletion.
7 changes: 6 additions & 1 deletion packages/flutter/lib/src/material/theme_data.dart
Original file line number Diff line number Diff line change
Expand Up @@ -388,7 +388,12 @@ class ThemeData with Diagnosticable {
: InkSplash.splashFactory;

// COLOR
assert(colorScheme?.brightness == null || brightness == null || colorScheme!.brightness == brightness);
assert(
colorScheme?.brightness == null || brightness == null || colorScheme!.brightness == brightness,
'ThemeData.brightness does not match ColorScheme.brightness. '
'Either override ColorScheme.brightness or ThemeData.brightness to '
'match the other.'
);
assert(colorSchemeSeed == null || colorScheme == null);
assert(colorSchemeSeed == null || primarySwatch == null);
assert(colorSchemeSeed == null || primaryColor == null);
Expand Down
96 changes: 96 additions & 0 deletions packages/flutter/test/material/theme_data_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -1284,6 +1284,102 @@ void main() {
// Ensure they are all there.
expect(propertyNames, expectedPropertyNames);
});

testWidgetsWithLeakTracking(
'ThemeData.brightness not matching ColorScheme.brightness throws a helpful error message', (WidgetTester tester) async {
AssertionError? error;

// Test `ColorScheme.light()` and `ThemeData.brightness == Brightness.dark`.
try {
await tester.pumpWidget(
MaterialApp(
theme: ThemeData(
colorScheme: const ColorScheme.light(),
brightness: Brightness.dark,
),
home: const Placeholder(),
),
);
} on AssertionError catch (e) {
error = e;
} finally {
expect(error, isNotNull);
expect(error?.message, contains(
'ThemeData.brightness does not match ColorScheme.brightness. '
'Either override ColorScheme.brightness or ThemeData.brightness to '
'match the other.'
));
}

// Test `ColorScheme.dark()` and `ThemeData.brightness == Brightness.light`.
try {
await tester.pumpWidget(
MaterialApp(
theme: ThemeData(
colorScheme: const ColorScheme.dark(),
brightness: Brightness.light,
),
home: const Placeholder(),
),
);
} on AssertionError catch (e) {
error = e;
} finally {
expect(error, isNotNull);
expect(error?.message, contains(
'ThemeData.brightness does not match ColorScheme.brightness. '
'Either override ColorScheme.brightness or ThemeData.brightness to '
'match the other.'
));
}

// Test `ColorScheme.fromSeed()` and `ThemeData.brightness == Brightness.dark`.
try {
await tester.pumpWidget(
MaterialApp(
theme: ThemeData(
colorScheme: ColorScheme.fromSeed(seedColor: const Color(0xffff0000)),
brightness: Brightness.dark,
),
home: const Placeholder(),
),
);
} on AssertionError catch (e) {
error = e;
} finally {
expect(error, isNotNull);
expect(error?.message, contains(
'ThemeData.brightness does not match ColorScheme.brightness. '
'Either override ColorScheme.brightness or ThemeData.brightness to '
'match the other.'
));
}

// Test `ColorScheme.fromSeed()` using `Brightness.dark` and `ThemeData.brightness == Brightness.light`.
try {
await tester.pumpWidget(
MaterialApp(
theme: ThemeData(
colorScheme: ColorScheme.fromSeed(
seedColor: const Color(0xffff0000),
brightness: Brightness.dark,
),
brightness: Brightness.light,
),
home: const Placeholder(),
),
);
} on AssertionError catch (e) {
error = e;
} finally {
expect(error, isNotNull);
expect(error?.message, contains(
'ThemeData.brightness does not match ColorScheme.brightness. '
'Either override ColorScheme.brightness or ThemeData.brightness to '
'match the other.'
));
}
});
}

@immutable
Expand Down

0 comments on commit 826bb0a

Please sign in to comment.