From 231170a7c5abc9f4f7d8dad321ba7f247a64a1e6 Mon Sep 17 00:00:00 2001 From: Jonah Williams Date: Wed, 14 Mar 2018 10:40:24 -0700 Subject: [PATCH] Replace child parameter with builder on showDialog (#15303) * replace child parameter with builder on showDialog * change builder parameter to WidgetBuilder * mark child as deprecated and expand documentation to cover this and how builder should be used * tidy comments and address some feedback * phrasing * move space to prev line and add //ignore comments for deprecated member use * address comments and fix it's its * update code samples * adds semicolon to code snippets --- .../demo/cupertino/cupertino_dialog_demo.dart | 2 +- .../lib/demo/material/dialog_demo.dart | 2 +- .../lib/demo/material/drawer_demo.dart | 24 ++-- .../material/full_screen_dialog_demo.dart | 40 +++--- .../persistent_bottom_sheet_demo.dart | 22 ++-- .../demo/material/text_form_field_demo.dart | 30 ++--- .../flutter_gallery/lib/gallery/updates.dart | 4 +- examples/stocks/lib/stock_home.dart | 2 +- examples/stocks/lib/stock_settings.dart | 38 +++--- packages/flutter/lib/src/material/about.dart | 16 +-- .../flutter/lib/src/material/date_picker.dart | 2 +- packages/flutter/lib/src/material/dialog.dart | 85 +++++++------ .../flutter/lib/src/material/time_picker.dart | 2 +- .../flutter/test/cupertino/dialog_test.dart | 40 +++--- .../flutter/test/material/dialog_test.dart | 116 ++++++++++-------- .../test/material/text_field_focus_test.dart | 2 +- .../flutter/test/material/theme_test.dart | 16 +-- .../flutter/test/material/will_pop_test.dart | 28 +++-- 18 files changed, 258 insertions(+), 213 deletions(-) diff --git a/examples/flutter_gallery/lib/demo/cupertino/cupertino_dialog_demo.dart b/examples/flutter_gallery/lib/demo/cupertino/cupertino_dialog_demo.dart index d16158c6ad..4d23789e3e 100644 --- a/examples/flutter_gallery/lib/demo/cupertino/cupertino_dialog_demo.dart +++ b/examples/flutter_gallery/lib/demo/cupertino/cupertino_dialog_demo.dart @@ -18,8 +18,8 @@ class _CupertinoDialogDemoState extends State { void showDemoDialog({ BuildContext context, Widget child }) { showDialog( context: context, - child: child, barrierDismissible: false, + builder: (BuildContext context) => child, ) .then((T value) { // The value passed to Navigator.pop() or null. if (value != null) { diff --git a/examples/flutter_gallery/lib/demo/material/dialog_demo.dart b/examples/flutter_gallery/lib/demo/material/dialog_demo.dart index 2908a3926b..a085106f23 100644 --- a/examples/flutter_gallery/lib/demo/material/dialog_demo.dart +++ b/examples/flutter_gallery/lib/demo/material/dialog_demo.dart @@ -68,7 +68,7 @@ class DialogDemoState extends State { void showDemoDialog({ BuildContext context, Widget child }) { showDialog( context: context, - child: child, + builder: (BuildContext context) => child, ) .then((T value) { // The value passed to Navigator.pop() or null. if (value != null) { diff --git a/examples/flutter_gallery/lib/demo/material/drawer_demo.dart b/examples/flutter_gallery/lib/demo/material/drawer_demo.dart index 54f6f35af7..4406e40078 100644 --- a/examples/flutter_gallery/lib/demo/material/drawer_demo.dart +++ b/examples/flutter_gallery/lib/demo/material/drawer_demo.dart @@ -233,17 +233,19 @@ class _DrawerDemoState extends State with TickerProviderStateMixin { void _onOtherAccountsTap(BuildContext context) { showDialog( context: context, - child: new AlertDialog( - title: const Text('Account switching not implemented.'), - actions: [ - new FlatButton( - child: const Text('OK'), - onPressed: () { - Navigator.of(context).pop(); - }, - ), - ], - ), + builder: (BuildContext context) { + return new AlertDialog( + title: const Text('Account switching not implemented.'), + actions: [ + new FlatButton( + child: const Text('OK'), + onPressed: () { + Navigator.of(context).pop(); + }, + ), + ], + ); + }, ); } } diff --git a/examples/flutter_gallery/lib/demo/material/full_screen_dialog_demo.dart b/examples/flutter_gallery/lib/demo/material/full_screen_dialog_demo.dart index ac762008ff..d44263be53 100644 --- a/examples/flutter_gallery/lib/demo/material/full_screen_dialog_demo.dart +++ b/examples/flutter_gallery/lib/demo/material/full_screen_dialog_demo.dart @@ -116,26 +116,28 @@ class FullScreenDialogDemoState extends State { return await showDialog( context: context, - child: new AlertDialog( - content: new Text( - 'Discard new event?', - style: dialogTextStyle - ), - actions: [ - new FlatButton( - child: const Text('CANCEL'), - onPressed: () { - Navigator.of(context).pop(false); // Pops the confirmation dialog but not the page. - } + builder: (BuildContext context) { + return new AlertDialog( + content: new Text( + 'Discard new event?', + style: dialogTextStyle ), - new FlatButton( - child: const Text('DISCARD'), - onPressed: () { - Navigator.of(context).pop(true); // Returning true to _onWillPop will pop again. - } - ) - ] - ) + actions: [ + new FlatButton( + child: const Text('CANCEL'), + onPressed: () { + Navigator.of(context).pop(false); // Pops the confirmation dialog but not the page. + } + ), + new FlatButton( + child: const Text('DISCARD'), + onPressed: () { + Navigator.of(context).pop(true); // Returning true to _onWillPop will pop again. + } + ) + ], + ); + }, ) ?? false; } diff --git a/examples/flutter_gallery/lib/demo/material/persistent_bottom_sheet_demo.dart b/examples/flutter_gallery/lib/demo/material/persistent_bottom_sheet_demo.dart index 53d3ae99b8..f35d0baba2 100644 --- a/examples/flutter_gallery/lib/demo/material/persistent_bottom_sheet_demo.dart +++ b/examples/flutter_gallery/lib/demo/material/persistent_bottom_sheet_demo.dart @@ -56,15 +56,19 @@ class _PersistentBottomSheetDemoState extends State { void _showMessage() { showDialog( context: context, - child: new AlertDialog( - content: const Text('You tapped the floating action button.'), - actions: [ - new FlatButton( - onPressed: () { Navigator.pop(context); }, - child: const Text('OK') - ) - ] - ) + builder: (BuildContext context) { + return new AlertDialog( + content: const Text('You tapped the floating action button.'), + actions: [ + new FlatButton( + onPressed: () { + Navigator.pop(context); + }, + child: const Text('OK') + ) + ], + ); + }, ); } diff --git a/examples/flutter_gallery/lib/demo/material/text_form_field_demo.dart b/examples/flutter_gallery/lib/demo/material/text_form_field_demo.dart index b133f7d95a..6011cca335 100644 --- a/examples/flutter_gallery/lib/demo/material/text_form_field_demo.dart +++ b/examples/flutter_gallery/lib/demo/material/text_form_field_demo.dart @@ -85,20 +85,22 @@ class TextFormFieldDemoState extends State { return await showDialog( context: context, - child: new AlertDialog( - title: const Text('This form has errors'), - content: const Text('Really leave this form?'), - actions: [ - new FlatButton( - child: const Text('YES'), - onPressed: () { Navigator.of(context).pop(true); }, - ), - new FlatButton( - child: const Text('NO'), - onPressed: () { Navigator.of(context).pop(false); }, - ), - ], - ), + builder: (BuildContext context) { + return new AlertDialog( + title: const Text('This form has errors'), + content: const Text('Really leave this form?'), + actions: [ + new FlatButton( + child: const Text('YES'), + onPressed: () { Navigator.of(context).pop(true); }, + ), + new FlatButton( + child: const Text('NO'), + onPressed: () { Navigator.of(context).pop(false); }, + ), + ], + ); + }, ) ?? false; } diff --git a/examples/flutter_gallery/lib/gallery/updates.dart b/examples/flutter_gallery/lib/gallery/updates.dart index fabafe0998..1b61ef7459 100644 --- a/examples/flutter_gallery/lib/gallery/updates.dart +++ b/examples/flutter_gallery/lib/gallery/updates.dart @@ -41,13 +41,13 @@ class UpdaterState extends State { final String updateUrl = await widget.updateUrlFetcher(); if (updateUrl != null) { - final bool wantsUpdate = await showDialog(context: context, child: _buildDialog()); + final bool wantsUpdate = await showDialog(context: context, builder: _buildDialog); if (wantsUpdate != null && wantsUpdate) launch(updateUrl); } } - Widget _buildDialog() { + Widget _buildDialog(BuildContext _) { final ThemeData theme = Theme.of(context); final TextStyle dialogTextStyle = theme.textTheme.subhead.copyWith(color: theme.textTheme.caption.color); diff --git a/examples/stocks/lib/stock_home.dart b/examples/stocks/lib/stock_home.dart index 26565d3921..d6ab754544 100644 --- a/examples/stocks/lib/stock_home.dart +++ b/examples/stocks/lib/stock_home.dart @@ -95,7 +95,7 @@ class StockHomeState extends State { case _StockMenuItem.refresh: showDialog( context: context, - child: new _NotImplementedDialog() + builder: (BuildContext context) => new _NotImplementedDialog(), ); break; case _StockMenuItem.speedUp: diff --git a/examples/stocks/lib/stock_settings.dart b/examples/stocks/lib/stock_settings.dart index a6f598c516..7390d12060 100644 --- a/examples/stocks/lib/stock_settings.dart +++ b/examples/stocks/lib/stock_settings.dart @@ -67,24 +67,26 @@ class StockSettingsState extends State { case StockMode.pessimistic: showDialog( context: context, - child: new AlertDialog( - title: const Text('Change mode?'), - content: const Text('Optimistic mode means everything is awesome. Are you sure you can handle that?'), - actions: [ - new FlatButton( - child: const Text('NO THANKS'), - onPressed: () { - Navigator.pop(context, false); - } - ), - new FlatButton( - child: const Text('AGREE'), - onPressed: () { - Navigator.pop(context, true); - } - ), - ] - ) + builder: (BuildContext context) { + return new AlertDialog( + title: const Text('Change mode?'), + content: const Text('Optimistic mode means everything is awesome. Are you sure you can handle that?'), + actions: [ + new FlatButton( + child: const Text('NO THANKS'), + onPressed: () { + Navigator.pop(context, false); + } + ), + new FlatButton( + child: const Text('AGREE'), + onPressed: () { + Navigator.pop(context, true); + } + ), + ], + ); + }, ).then(_handleOptimismChanged); break; } diff --git a/packages/flutter/lib/src/material/about.dart b/packages/flutter/lib/src/material/about.dart index 3e1bbb3f72..d9acadcc49 100644 --- a/packages/flutter/lib/src/material/about.dart +++ b/packages/flutter/lib/src/material/about.dart @@ -153,13 +153,15 @@ void showAboutDialog({ }) { showDialog( context: context, - child: new AboutDialog( - applicationName: applicationName, - applicationVersion: applicationVersion, - applicationIcon: applicationIcon, - applicationLegalese: applicationLegalese, - children: children - ) + builder: (BuildContext context) { + return new AboutDialog( + applicationName: applicationName, + applicationVersion: applicationVersion, + applicationIcon: applicationIcon, + applicationLegalese: applicationLegalese, + children: children, + ); + } ); } diff --git a/packages/flutter/lib/src/material/date_picker.dart b/packages/flutter/lib/src/material/date_picker.dart index 1ca931f1c4..11811ebc1f 100644 --- a/packages/flutter/lib/src/material/date_picker.dart +++ b/packages/flutter/lib/src/material/date_picker.dart @@ -1042,6 +1042,6 @@ Future showDatePicker({ return await showDialog( context: context, - child: child, + builder: (BuildContext context) => child, ); } diff --git a/packages/flutter/lib/src/material/dialog.dart b/packages/flutter/lib/src/material/dialog.dart index be73e66cb9..c6cabcb046 100644 --- a/packages/flutter/lib/src/material/dialog.dart +++ b/packages/flutter/lib/src/material/dialog.dart @@ -96,25 +96,27 @@ class Dialog extends StatelessWidget { /// return showDialog( /// context: context, /// barrierDismissible: false, // user must tap button! -/// child: new AlertDialog( -/// title: new Text('Rewind and remember'), -/// content: new SingleChildScrollView( -/// child: new ListBody( -/// children: [ -/// new Text('You will never be satisfied.'), -/// new Text('You\’re like me. I’m never satisfied.'), -/// ], +/// builder: (BuildContext context) { +/// return new AlertDialog( +/// title: new Text('Rewind and remember'), +/// content: new SingleChildScrollView( +/// child: new ListBody( +/// children: [ +/// new Text('You will never be satisfied.'), +/// new Text('You\’re like me. I’m never satisfied.'), +/// ], +/// ), /// ), -/// ), -/// actions: [ -/// new FlatButton( -/// child: new Text('Regret'), -/// onPressed: () { -/// Navigator.of(context).pop(); -/// }, -/// ), -/// ], -/// ), +/// actions: [ +/// new FlatButton( +/// child: new Text('Regret'), +/// onPressed: () { +/// Navigator.of(context).pop(); +/// }, +/// ), +/// ], +/// ); +/// }, /// ); /// } /// ``` @@ -301,19 +303,21 @@ class SimpleDialogOption extends StatelessWidget { /// Future _askedToLead() async { /// switch (await showDialog( /// context: context, -/// child: new SimpleDialog( -/// title: const Text('Select assignment'), -/// children: [ -/// new SimpleDialogOption( -/// onPressed: () { Navigator.pop(context, Department.treasury); }, -/// child: const Text('Treasury department'), -/// ), -/// new SimpleDialogOption( -/// onPressed: () { Navigator.pop(context, Department.state); }, -/// child: const Text('State department'), -/// ), -/// ], -/// ), +/// builder: (BuildContext context) { +/// return new SimpleDialog( +/// title: const Text('Select assignment'), +/// children: [ +/// new SimpleDialogOption( +/// onPressed: () { Navigator.pop(context, Department.treasury); }, +/// child: const Text('Treasury department'), +/// ), +/// new SimpleDialogOption( +/// onPressed: () { Navigator.pop(context, Department.state); }, +/// child: const Text('State department'), +/// ), +/// ], +/// ); +/// } /// )) { /// case Department.treasury: /// // Let's go. @@ -457,13 +461,18 @@ class _DialogRoute extends PopupRoute { /// Displays a dialog above the current contents of the app. /// -/// This function typically receives a [Dialog] widget as its child argument. -/// Content below the dialog is dimmed with a [ModalBarrier]. +/// This function takes a `builder` which typically builds a [Dialog] widget. +/// Content below the dialog is dimmed with a [ModalBarrier]. This widget does +/// not share a context with the location that `showDialog` is originally +/// called from. Use a [StatefulBuilder] or a custom [StatefulWidget] if the +/// dialog needs to update dynamically. /// /// The `context` argument is used to look up the [Navigator] and [Theme] for /// the dialog. It is only used when the method is called. Its corresponding /// widget can be safely removed from the tree before the dialog is closed. /// +/// The `child` argument is deprecated, and should be replaced with `builder`. +/// /// Returns a [Future] that resolves to the value (if any) that was passed to /// [Navigator.pop] when the dialog was closed. /// @@ -481,10 +490,16 @@ class _DialogRoute extends PopupRoute { Future showDialog({ @required BuildContext context, bool barrierDismissible: true, - @required Widget child, + @Deprecated( + 'Instead of using the "child" argument, return the child from a closure ' + 'provided to the "builder" argument. This will ensure that the BuildContext ' + 'is appropriate for widgets built in the dialog.' + ) Widget child, + WidgetBuilder builder, }) { + assert(child == null || builder == null); // ignore: deprecated_member_use return Navigator.of(context, rootNavigator: true).push(new _DialogRoute( - child: child, + child: child ?? new Builder(builder: builder), // ignore: deprecated_member_use theme: Theme.of(context, shadowThemeOnly: true), barrierDismissible: barrierDismissible, barrierLabel: MaterialLocalizations.of(context).modalBarrierDismissLabel, diff --git a/packages/flutter/lib/src/material/time_picker.dart b/packages/flutter/lib/src/material/time_picker.dart index 0f8f27cf5f..af2df2ec01 100644 --- a/packages/flutter/lib/src/material/time_picker.dart +++ b/packages/flutter/lib/src/material/time_picker.dart @@ -1645,7 +1645,7 @@ Future showTimePicker({ return await showDialog( context: context, - child: new _TimePickerDialog(initialTime: initialTime), + builder: (BuildContext context) => new _TimePickerDialog(initialTime: initialTime), ); } diff --git a/packages/flutter/test/cupertino/dialog_test.dart b/packages/flutter/test/cupertino/dialog_test.dart index 53c837948a..7ce05167c0 100644 --- a/packages/flutter/test/cupertino/dialog_test.dart +++ b/packages/flutter/test/cupertino/dialog_test.dart @@ -19,23 +19,25 @@ void main() { onPressed: () { showDialog( context: context, - child: new CupertinoAlertDialog( - title: const Text('The title'), - content: const Text('The content'), - actions: [ - const CupertinoDialogAction( - child: const Text('Cancel'), - ), - new CupertinoDialogAction( - isDestructiveAction: true, - onPressed: () { - didDelete = true; - Navigator.pop(context); - }, - child: const Text('Delete'), - ), - ], - ), + builder: (BuildContext context) { + return new CupertinoAlertDialog( + title: const Text('The title'), + content: const Text('The content'), + actions: [ + const CupertinoDialogAction( + child: const Text('Cancel'), + ), + new CupertinoDialogAction( + isDestructiveAction: true, + onPressed: () { + didDelete = true; + Navigator.pop(context); + }, + child: const Text('Delete'), + ), + ], + ); + }, ); }, child: const Text('Go'), @@ -110,7 +112,7 @@ void main() { onPressed: () { showDialog( context: context, - child: new Builder(builder: (BuildContext context) { + builder: (BuildContext context) { return new MediaQuery( data: MediaQuery.of(context).copyWith(textScaleFactor: 3.0), child: new CupertinoAlertDialog( @@ -128,7 +130,7 @@ void main() { scrollController: scrollController, ), ); - }), + }, ); }, child: const Text('Go'), diff --git a/packages/flutter/test/material/dialog_test.dart b/packages/flutter/test/material/dialog_test.dart index a8d08f6b97..80192bf3ad 100644 --- a/packages/flutter/test/material/dialog_test.dart +++ b/packages/flutter/test/material/dialog_test.dart @@ -23,21 +23,23 @@ void main() { onPressed: () { showDialog( context: context, - child: new AlertDialog( - content: new Container( - height: 5000.0, - width: 300.0, - color: Colors.green[500], - ), - actions: [ - new FlatButton( - onPressed: () { - didPressOk = true; - }, - child: const Text('OK') - ) - ] - ) + builder: (BuildContext context) { + return new AlertDialog( + content: new Container( + height: 5000.0, + width: 300.0, + color: Colors.green[500], + ), + actions: [ + new FlatButton( + onPressed: () { + didPressOk = true; + }, + child: const Text('OK') + ) + ], + ); + }, ); } ) @@ -71,11 +73,13 @@ void main() { onPressed: () { showDialog( context: context, - child: const AlertDialog( - title: const Text('Title'), - content: const Text('Y'), - actions: const [ ], - ), + builder: (BuildContext context) { + return const AlertDialog( + title: const Text('Title'), + content: const Text('Y'), + actions: const [ ], + ); + }, ); }, ), @@ -116,20 +120,22 @@ void main() { final Future result = showDialog( context: context, - child: new SimpleDialog( - title: const Text('Title'), - children: [ - new SimpleDialogOption( - onPressed: () { - Navigator.pop(context, 42); - }, - child: const Text('First option'), - ), - const SimpleDialogOption( - child: const Text('Second option'), - ), - ], - ), + builder: (BuildContext context) { + return new SimpleDialog( + title: const Text('Title'), + children: [ + new SimpleDialogOption( + onPressed: () { + Navigator.pop(context, 42); + }, + child: const Text('First option'), + ), + const SimpleDialogOption( + child: const Text('Second option'), + ), + ], + ); + }, ); await tester.pumpAndSettle(const Duration(seconds: 1)); @@ -157,12 +163,14 @@ void main() { showDialog( context: context, - child: new Container( - width: 100.0, - height: 100.0, - alignment: Alignment.center, - child: const Text('Dialog1'), - ), + builder: (BuildContext context) { + return new Container( + width: 100.0, + height: 100.0, + alignment: Alignment.center, + child: const Text('Dialog1'), + ); + }, ); await tester.pumpAndSettle(const Duration(seconds: 1)); @@ -177,12 +185,14 @@ void main() { showDialog( context: context, barrierDismissible: false, - child: new Container( - width: 100.0, - height: 100.0, - alignment: Alignment.center, - child: const Text('Dialog2'), - ), + builder: (BuildContext context) { + return new Container( + width: 100.0, + height: 100.0, + alignment: Alignment.center, + child: const Text('Dialog2'), + ); + }, ); await tester.pumpAndSettle(const Duration(seconds: 1)); @@ -219,7 +229,9 @@ void main() { const String alertText = 'A button in an overlay alert'; showDialog( context: context, - child: const AlertDialog(title: const Text(alertText)), + builder: (BuildContext context) { + return const AlertDialog(title: const Text(alertText)); + }, ); await tester.pumpAndSettle(const Duration(seconds: 1)); @@ -260,12 +272,10 @@ void main() { showDialog( context: outerContext, barrierDismissible: false, - child: new Builder( - builder: (BuildContext context) { - dialogContext = context; - return new Container(); - }, - ), + builder: (BuildContext context) { + dialogContext = context; + return new Container(); + }, ); await tester.pump(); diff --git a/packages/flutter/test/material/text_field_focus_test.dart b/packages/flutter/test/material/text_field_focus_test.dart index e9dc055692..b6b17e2f58 100644 --- a/packages/flutter/test/material/text_field_focus_test.dart +++ b/packages/flutter/test/material/text_field_focus_test.dart @@ -110,7 +110,7 @@ void main() { showDialog( context: context, - child: const SimpleDialog(title: const Text('Dialog')), + builder: (BuildContext context) => const SimpleDialog(title: const Text('Dialog')), ); await tester.pump(); diff --git a/packages/flutter/test/material/theme_test.dart b/packages/flutter/test/material/theme_test.dart index dddc09bf85..cc9487a657 100644 --- a/packages/flutter/test/material/theme_test.dart +++ b/packages/flutter/test/material/theme_test.dart @@ -200,7 +200,7 @@ void main() { onPressed: () { showDialog( context: context, - child: const Text('dialog'), + builder: (BuildContext context) => const Text('dialog'), ); }, child: const Text('SHOW'), @@ -232,12 +232,14 @@ void main() { onTap: () { showDialog( context: context, - child: const Scaffold( - body: const SizedBox( - width: 200.0, - height: 200.0, - ), - ) + builder: (BuildContext context) { + return const Scaffold( + body: const SizedBox( + width: 200.0, + height: 200.0, + ), + ); + }, ); }, child: const Text('SHOW'), diff --git a/packages/flutter/test/material/will_pop_test.dart b/packages/flutter/test/material/will_pop_test.dart index d10283bfd1..4f5c65e4f1 100644 --- a/packages/flutter/test/material/will_pop_test.dart +++ b/packages/flutter/test/material/will_pop_test.dart @@ -86,7 +86,7 @@ void main() { onPressed: () { showDialog( context: context, - child: new SamplePage(), + builder: (BuildContext context) => new SamplePage(), ); }, ), @@ -185,18 +185,20 @@ void main() { Future showYesNoAlert(BuildContext context) { return showDialog( context: context, - child: new AlertDialog( - actions: [ - new FlatButton( - child: const Text('YES'), - onPressed: () { Navigator.of(context).pop(true); }, - ), - new FlatButton( - child: const Text('NO'), - onPressed: () { Navigator.of(context).pop(false); }, - ), - ], - ), + builder: (BuildContext context) { + return new AlertDialog( + actions: [ + new FlatButton( + child: const Text('YES'), + onPressed: () { Navigator.of(context).pop(true); }, + ), + new FlatButton( + child: const Text('NO'), + onPressed: () { Navigator.of(context).pop(false); }, + ), + ], + ); + }, ); }