diff --git a/packages/flutter/lib/src/material/dialog.dart b/packages/flutter/lib/src/material/dialog.dart index 93d5cb86b7..861ee716c0 100644 --- a/packages/flutter/lib/src/material/dialog.dart +++ b/packages/flutter/lib/src/material/dialog.dart @@ -973,6 +973,7 @@ Future showDialog({ Widget? child, }) { assert(child == null || builder == null); + assert(child != null || builder != null); assert(barrierDismissible != null); assert(useSafeArea != null); assert(useRootNavigator != null); diff --git a/packages/flutter/test/material/dialog_test.dart b/packages/flutter/test/material/dialog_test.dart index 646f3fd7bb..2e29042af5 100644 --- a/packages/flutter/test/material/dialog_test.dart +++ b/packages/flutter/test/material/dialog_test.dart @@ -217,6 +217,57 @@ void main() { expect(materialWidget.shape, customBorder); }); + testWidgets('showDialog child and builder cannot be simultaneously defined', (WidgetTester tester) async { + late BuildContext currentBuildContext; + await tester.pumpWidget( + MaterialApp( + home: Scaffold( + body: Center( + child: Builder( + builder: (BuildContext context) { + currentBuildContext = context; + return Container(); + } + ), + ), + ), + ), + ); + + expect(() async { + showDialog( + context: currentBuildContext, + child: const Text('Child'), + builder: (BuildContext context) { + return const Text('Builder'); + }, + ); + }, throwsAssertionError); + }); + + testWidgets('showDialog child or builder must be defined', (WidgetTester tester) async { + late BuildContext currentBuildContext; + await tester.pumpWidget( + MaterialApp( + home: Scaffold( + body: Center( + child: Builder( + builder: (BuildContext context) { + currentBuildContext = context; + return Container(); + } + ), + ), + ), + ), + ); + + expect( + () => showDialog(context: currentBuildContext), + throwsAssertionError, + ); + }); + testWidgets('Simple dialog control test', (WidgetTester tester) async { await tester.pumpWidget( const MaterialApp(