Skip to content

Commit d420480

Browse files
committed
Improve Container color/decoration error message clarity
The previous assertion message was confusing because it stated 'Cannot provide both' but then suggested 'To provide both, use...', which contradicted the restriction. This confused developers about the actual requirement. Updated the message to clearly explain: - The color parameter is a shorthand for decoration: BoxDecoration(color: color) - Developers should use either color OR decoration, not both - To use both color and other decoration properties, set the color within BoxDecoration Also added a test to verify the assertion triggers with the new message when both parameters are provided. Fixes flutter#119484
1 parent 17e65b6 commit d420480

2 files changed

Lines changed: 23 additions & 2 deletions

File tree

‎packages/flutter/lib/src/widgets/container.dart‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -275,8 +275,9 @@ class Container extends StatelessWidget {
275275
assert(decoration != null || clipBehavior == Clip.none),
276276
assert(
277277
color == null || decoration == null,
278-
'Cannot provide both a color and a decoration\n'
279-
'To provide both, use "decoration: BoxDecoration(color: color)".',
278+
'Cannot provide both a color and a decoration.\n'
279+
'The color argument is just a shorthand for "decoration: BoxDecoration(color: color)".\n'
280+
'To use both a color and other decoration properties, set the color in the BoxDecoration instead.',
280281
),
281282
constraints = (width != null || height != null)
282283
? constraints?.tighten(width: width, height: height) ??

‎packages/flutter/test/widgets/container_test.dart‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -779,6 +779,26 @@ void main() {
779779
matchesGoldenFile('container.clipBehaviour.with.shadow.png'),
780780
);
781781
});
782+
783+
testWidgets('Container with both color and decoration throws clear error', (WidgetTester tester) async {
784+
expect(
785+
() => Container(
786+
color: const Color(0xFF0000FF),
787+
decoration: const BoxDecoration(),
788+
),
789+
throwsA(
790+
isA<AssertionError>().having(
791+
(AssertionError e) => e.toString(),
792+
'message',
793+
allOf(
794+
contains('Cannot provide both a color and a decoration'),
795+
contains('The color argument is just a shorthand'),
796+
contains('set the color in the BoxDecoration instead'),
797+
),
798+
),
799+
),
800+
);
801+
});
782802
}
783803

784804
class _MockPaintingContext extends Fake implements PaintingContext {

0 commit comments

Comments
 (0)