Repository navigation
[material_ui] Migrate Dialog, AlertDialog, and SimpleDialog API doc snippets to {@example} (batch 12) #13721
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
dkwingsmt
wants to merge
2
commits into
flutter:main
Choose a base branch
from
dkwingsmt:snippet_material_dialog
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
29 changes: 29 additions & 0 deletions
29
packages/material_ui/example/lib/dialog/alert_dialog.actions_padding.snippet.0.dart
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| // Copyright 2013 The Flutter Authors | ||
| // Use of this source code is governed by a BSD-style license that can be | ||
| // found in the LICENSE file. | ||
|
|
||
| import 'package:material_ui/material_ui.dart'; | ||
|
|
||
| /// Flutter code sample for [AlertDialog.actionsPadding]. | ||
|
|
||
| class AlertDialogExample extends StatelessWidget { | ||
| const AlertDialogExample({super.key}); | ||
|
|
||
| @override | ||
| Widget build(BuildContext context) { | ||
| return | ||
| // #region body | ||
| AlertDialog( | ||
| title: const Text('Title'), | ||
| contentPadding: const EdgeInsets.all(8.0), | ||
| content: Container(width: 200, height: 200, color: Colors.green), | ||
| actions: <Widget>[ | ||
| ElevatedButton(onPressed: () {}, child: const Text('Button 1')), | ||
| ElevatedButton(onPressed: () {}, child: const Text('Button 2')), | ||
| ], | ||
| actionsPadding: const EdgeInsets.symmetric(horizontal: 8.0), | ||
| ) | ||
| // #endregion body | ||
| ; | ||
| } | ||
| } |
51 changes: 51 additions & 0 deletions
51
packages/material_ui/example/lib/dialog/alert_dialog.snippet.0.dart
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,51 @@ | ||
| // Copyright 2013 The Flutter Authors | ||
| // Use of this source code is governed by a BSD-style license that can be | ||
| // found in the LICENSE file. | ||
|
|
||
| import 'package:material_ui/material_ui.dart'; | ||
|
|
||
| /// Flutter code sample for [AlertDialog]. | ||
|
|
||
| class AlertDialogExample extends StatefulWidget { | ||
| const AlertDialogExample({super.key}); | ||
|
|
||
| @override | ||
| State<AlertDialogExample> createState() => AlertDialogExampleState(); | ||
| } | ||
|
|
||
| class AlertDialogExampleState extends State<AlertDialogExample> { | ||
| // #region body | ||
| Future<void> _showMyDialog() async { | ||
| return showDialog<void>( | ||
| context: context, | ||
| barrierDismissible: false, // user must tap button! | ||
| builder: (BuildContext context) { | ||
| return AlertDialog( | ||
| title: const Text('AlertDialog Title'), | ||
| content: const SingleChildScrollView( | ||
| child: ListBody( | ||
| children: <Widget>[ | ||
| Text('This is a demo alert dialog.'), | ||
| Text('Would you like to approve of this message?'), | ||
| ], | ||
| ), | ||
| ), | ||
| actions: <Widget>[ | ||
| TextButton( | ||
| child: const Text('Approve'), | ||
| onPressed: () { | ||
| Navigator.of(context).pop(); | ||
| }, | ||
| ), | ||
| ], | ||
| ); | ||
| }, | ||
| ); | ||
| } | ||
| // #endregion body | ||
|
|
||
| Future<void> showMyDialog() => _showMyDialog(); | ||
|
|
||
| @override | ||
| Widget build(BuildContext context) => const SizedBox.shrink(); | ||
| } |
20 changes: 20 additions & 0 deletions
20
packages/material_ui/example/lib/dialog/dialog.snippet.0.dart
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| // Copyright 2013 The Flutter Authors | ||
| // Use of this source code is governed by a BSD-style license that can be | ||
| // found in the LICENSE file. | ||
|
|
||
| import 'package:material_ui/material_ui.dart'; | ||
|
|
||
| /// Flutter code sample for [Dialog]. | ||
|
|
||
| class DialogExample extends StatelessWidget { | ||
| const DialogExample({super.key}); | ||
|
|
||
| @override | ||
| Widget build(BuildContext context) { | ||
| return | ||
| // #region body | ||
| const Dialog(constraints: BoxConstraints(maxWidth: 560, minHeight: 280)) | ||
| // #endregion body | ||
| ; | ||
| } | ||
| } |
55 changes: 55 additions & 0 deletions
55
packages/material_ui/example/lib/dialog/simple_dialog.snippet.0.dart
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,55 @@ | ||
| // Copyright 2013 The Flutter Authors | ||
| // Use of this source code is governed by a BSD-style license that can be | ||
| // found in the LICENSE file. | ||
|
|
||
| import 'package:material_ui/material_ui.dart'; | ||
|
|
||
| /// Flutter code sample for [SimpleDialog]. | ||
|
|
||
| enum Department { treasury, state } | ||
|
|
||
| class SimpleDialogExample { | ||
| SimpleDialogExample(this.context); | ||
|
|
||
| final BuildContext context; | ||
|
|
||
| // #region body | ||
| Future<void> _askedToLead() async { | ||
| switch (await showDialog<Department>( | ||
| context: context, | ||
| builder: (BuildContext context) { | ||
| return SimpleDialog( | ||
| title: const Text('Select assignment'), | ||
| children: <Widget>[ | ||
| SimpleDialogOption( | ||
| onPressed: () { | ||
| Navigator.pop(context, Department.treasury); | ||
| }, | ||
| child: const Text('Treasury department'), | ||
| ), | ||
| SimpleDialogOption( | ||
| onPressed: () { | ||
| Navigator.pop(context, Department.state); | ||
| }, | ||
| child: const Text('State department'), | ||
| ), | ||
| ], | ||
| ); | ||
| }, | ||
| )) { | ||
| case Department.treasury: | ||
| // Let's go. | ||
| // ... | ||
| break; | ||
| case Department.state: | ||
| // ... | ||
| break; | ||
| case null: | ||
| // dialog dismissed | ||
| break; | ||
| } | ||
| } | ||
| // #endregion body | ||
|
|
||
| Future<void> askedToLead() => _askedToLead(); | ||
| } | ||
27 changes: 27 additions & 0 deletions
27
packages/material_ui/example/lib/dialog/simple_dialog_option.snippet.0.dart
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| // Copyright 2013 The Flutter Authors | ||
| // Use of this source code is governed by a BSD-style license that can be | ||
| // found in the LICENSE file. | ||
|
|
||
| import 'package:material_ui/material_ui.dart'; | ||
|
|
||
| /// Flutter code sample for [SimpleDialogOption]. | ||
|
|
||
| enum Department { treasury, state } | ||
|
|
||
| class SimpleDialogOptionExample extends StatelessWidget { | ||
| const SimpleDialogOptionExample({super.key}); | ||
|
|
||
| @override | ||
| Widget build(BuildContext context) { | ||
| return | ||
| // #region body | ||
| SimpleDialogOption( | ||
| onPressed: () { | ||
| Navigator.pop(context, Department.treasury); | ||
| }, | ||
| child: const Text('Treasury department'), | ||
| ) | ||
| // #endregion body | ||
| ; | ||
| } | ||
| } |
35 changes: 35 additions & 0 deletions
35
packages/material_ui/example/test/dialog/alert_dialog.actions_padding.snippet.0_test.dart
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,35 @@ | ||
| // Copyright 2013 The Flutter Authors | ||
| // Use of this source code is governed by a BSD-style license that can be | ||
| // found in the LICENSE file. | ||
|
|
||
| import 'package:flutter_test/flutter_test.dart'; | ||
| import 'package:material_ui/material_ui.dart'; | ||
| import 'package:material_ui_examples/dialog/alert_dialog.actions_padding.snippet.0.dart' | ||
| as example; | ||
|
|
||
| void main() { | ||
| testWidgets('AlertDialog.actionsPadding aligns actions with content widget', ( | ||
| WidgetTester tester, | ||
| ) async { | ||
| await tester.pumpWidget( | ||
| const MaterialApp(home: Scaffold(body: example.AlertDialogExample())), | ||
| ); | ||
|
|
||
| final Finder dialogMaterial = find | ||
| .descendant( | ||
| of: find.byType(AlertDialog), | ||
| matching: find.byType(Material), | ||
| ) | ||
| .first; | ||
| final Finder contentContainer = find.byType(Container); | ||
| final Finder button2 = find.widgetWithText(ElevatedButton, 'Button 2'); | ||
|
|
||
| final double dialogRight = tester.getTopRight(dialogMaterial).dx; | ||
| final double contentRight = tester.getTopRight(contentContainer).dx; | ||
| final double button2Right = tester.getTopRight(button2).dx; | ||
|
|
||
| expect(dialogRight - contentRight, 8.0); | ||
| expect(dialogRight - button2Right, 8.0); | ||
| expect(button2Right, contentRight); | ||
| }); | ||
| } |
52 changes: 52 additions & 0 deletions
52
packages/material_ui/example/test/dialog/alert_dialog.snippet.0_test.dart
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,52 @@ | ||
| // Copyright 2013 The Flutter Authors | ||
| // Use of this source code is governed by a BSD-style license that can be | ||
| // found in the LICENSE file. | ||
|
|
||
| import 'package:flutter_test/flutter_test.dart'; | ||
| import 'package:material_ui/material_ui.dart'; | ||
| import 'package:material_ui_examples/dialog/alert_dialog.snippet.0.dart' | ||
| as example; | ||
|
|
||
| void main() { | ||
| testWidgets( | ||
| 'AlertDialog shows non-barrier-dismissible dialog and completes Future on Approve', | ||
| (WidgetTester tester) async { | ||
| await tester.pumpWidget( | ||
| const MaterialApp(home: Scaffold(body: example.AlertDialogExample())), | ||
| ); | ||
|
|
||
| expect(find.byType(AlertDialog), findsNothing); | ||
|
|
||
| final example.AlertDialogExampleState state = tester.state( | ||
| find.byType(example.AlertDialogExample), | ||
| ); | ||
| bool dialogCompleted = false; | ||
| final Future<void> dialogFuture = state.showMyDialog().then((_) { | ||
| dialogCompleted = true; | ||
| }); | ||
| await tester.pumpAndSettle(); | ||
|
|
||
| expect(find.byType(AlertDialog), findsOneWidget); | ||
| expect(find.text('AlertDialog Title'), findsOneWidget); | ||
| expect(find.text('This is a demo alert dialog.'), findsOneWidget); | ||
| expect( | ||
| find.text('Would you like to approve of this message?'), | ||
| findsOneWidget, | ||
| ); | ||
| expect(dialogCompleted, isFalse); | ||
|
|
||
| // Tapping outside the dialog does not dismiss it or complete the Future. | ||
| await tester.tapAt(const Offset(10, 10)); | ||
| await tester.pumpAndSettle(); | ||
| expect(find.byType(AlertDialog), findsOneWidget); | ||
| expect(dialogCompleted, isFalse); | ||
|
|
||
| // Tapping Approve dismisses the dialog and completes the returned Future. | ||
| await tester.tap(find.text('Approve')); | ||
| await tester.pumpAndSettle(); | ||
| expect(find.byType(AlertDialog), findsNothing); | ||
| expect(dialogCompleted, isTrue); | ||
| await dialogFuture; | ||
| }, | ||
| ); | ||
| } |
38 changes: 38 additions & 0 deletions
38
packages/material_ui/example/test/dialog/dialog.snippet.0_test.dart
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,38 @@ | ||
| // Copyright 2013 The Flutter Authors | ||
| // Use of this source code is governed by a BSD-style license that can be | ||
| // found in the LICENSE file. | ||
|
|
||
| import 'package:flutter/rendering.dart'; | ||
| import 'package:flutter_test/flutter_test.dart'; | ||
| import 'package:material_ui/material_ui.dart'; | ||
| import 'package:material_ui_examples/dialog/dialog.snippet.0.dart' as example; | ||
|
|
||
| void main() { | ||
| testWidgets('Dialog applies M3 BoxConstraints', (WidgetTester tester) async { | ||
| await tester.pumpWidget( | ||
| const MaterialApp(home: Scaffold(body: example.DialogExample())), | ||
| ); | ||
|
|
||
| final Finder dialogMaterial = find.descendant( | ||
| of: find.byType(Dialog), | ||
| matching: find.byType(Material), | ||
| ); | ||
| expect(tester.getSize(dialogMaterial), const Size(0, 280)); | ||
|
|
||
| final RenderConstrainedBox constrainedBox = tester.renderObject( | ||
| find.descendant( | ||
| of: find.byType(Dialog), | ||
| matching: find.byWidgetPredicate( | ||
| (Widget widget) => | ||
| widget is ConstrainedBox && | ||
| widget.constraints == | ||
| const BoxConstraints(maxWidth: 560, minHeight: 280), | ||
| ), | ||
| ), | ||
| ); | ||
| expect( | ||
| constrainedBox.additionalConstraints, | ||
| const BoxConstraints(maxWidth: 560, minHeight: 280), | ||
| ); | ||
| }); | ||
| } |
72 changes: 72 additions & 0 deletions
72
packages/material_ui/example/test/dialog/simple_dialog.snippet.0_test.dart
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,72 @@ | ||
| // Copyright 2013 The Flutter Authors | ||
| // Use of this source code is governed by a BSD-style license that can be | ||
| // found in the LICENSE file. | ||
|
|
||
| import 'package:flutter_test/flutter_test.dart'; | ||
| import 'package:material_ui/material_ui.dart'; | ||
| import 'package:material_ui_examples/dialog/simple_dialog.snippet.0.dart' | ||
| as example; | ||
|
|
||
| class _DialogObserver extends NavigatorObserver { | ||
| Future<dynamic>? lastPopped; | ||
|
|
||
| @override | ||
| void didPush(Route<dynamic> route, Route<dynamic>? previousRoute) { | ||
| lastPopped = route.popped; | ||
| } | ||
| } | ||
|
|
||
| void main() { | ||
| testWidgets( | ||
| 'SimpleDialog returns selected Department or null when dismissed', | ||
| (WidgetTester tester) async { | ||
| final _DialogObserver observer = _DialogObserver(); | ||
| late BuildContext savedContext; | ||
|
|
||
| await tester.pumpWidget( | ||
| MaterialApp( | ||
| navigatorObservers: <NavigatorObserver>[observer], | ||
| home: Scaffold( | ||
| body: Builder( | ||
| builder: (BuildContext context) { | ||
| savedContext = context; | ||
| return const SizedBox.shrink(); | ||
| }, | ||
| ), | ||
| ), | ||
| ), | ||
| ); | ||
|
|
||
| final example.SimpleDialogExample dialogExample = | ||
| example.SimpleDialogExample(savedContext); | ||
|
|
||
| // Select Treasury department. | ||
| Future<void> future = dialogExample.askedToLead(); | ||
| await tester.pumpAndSettle(); | ||
| expect(find.text('Select assignment'), findsOneWidget); | ||
| Future<dynamic> popped = observer.lastPopped!; | ||
| await tester.tap(find.text('Treasury department')); | ||
| await tester.pumpAndSettle(); | ||
| expect(await popped, example.Department.treasury); | ||
| await future; | ||
|
|
||
| // Select State department. | ||
| future = dialogExample.askedToLead(); | ||
| await tester.pumpAndSettle(); | ||
| popped = observer.lastPopped!; | ||
| await tester.tap(find.text('State department')); | ||
| await tester.pumpAndSettle(); | ||
| expect(await popped, example.Department.state); | ||
| await future; | ||
|
|
||
| // Dismiss dialog by tapping the barrier. | ||
| future = dialogExample.askedToLead(); | ||
| await tester.pumpAndSettle(); | ||
| popped = observer.lastPopped!; | ||
| await tester.tapAt(const Offset(10, 10)); | ||
| await tester.pumpAndSettle(); | ||
| expect(await popped, isNull); | ||
| await future; | ||
| }, | ||
| ); | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.