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
This commit is contained in:
Jonah Williams
2018-03-14 10:40:24 -07:00
committed by GitHub
parent 9a8b460209
commit 231170a7c5
18 changed files with 258 additions and 213 deletions

View File

@@ -18,8 +18,8 @@ class _CupertinoDialogDemoState extends State<CupertinoDialogDemo> {
void showDemoDialog<T>({ BuildContext context, Widget child }) {
showDialog<T>(
context: context,
child: child,
barrierDismissible: false,
builder: (BuildContext context) => child,
)
.then<Null>((T value) { // The value passed to Navigator.pop() or null.
if (value != null) {

View File

@@ -68,7 +68,7 @@ class DialogDemoState extends State<DialogDemo> {
void showDemoDialog<T>({ BuildContext context, Widget child }) {
showDialog<T>(
context: context,
child: child,
builder: (BuildContext context) => child,
)
.then<Null>((T value) { // The value passed to Navigator.pop() or null.
if (value != null) {

View File

@@ -233,17 +233,19 @@ class _DrawerDemoState extends State<DrawerDemo> with TickerProviderStateMixin {
void _onOtherAccountsTap(BuildContext context) {
showDialog<Null>(
context: context,
child: new AlertDialog(
title: const Text('Account switching not implemented.'),
actions: <Widget>[
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: <Widget>[
new FlatButton(
child: const Text('OK'),
onPressed: () {
Navigator.of(context).pop();
},
),
],
);
},
);
}
}

View File

@@ -116,26 +116,28 @@ class FullScreenDialogDemoState extends State<FullScreenDialogDemo> {
return await showDialog<bool>(
context: context,
child: new AlertDialog(
content: new Text(
'Discard new event?',
style: dialogTextStyle
),
actions: <Widget>[
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: <Widget>[
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;
}

View File

@@ -56,15 +56,19 @@ class _PersistentBottomSheetDemoState extends State<PersistentBottomSheetDemo> {
void _showMessage() {
showDialog<Null>(
context: context,
child: new AlertDialog(
content: const Text('You tapped the floating action button.'),
actions: <Widget>[
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: <Widget>[
new FlatButton(
onPressed: () {
Navigator.pop(context);
},
child: const Text('OK')
)
],
);
},
);
}

View File

@@ -85,20 +85,22 @@ class TextFormFieldDemoState extends State<TextFormFieldDemo> {
return await showDialog<bool>(
context: context,
child: new AlertDialog(
title: const Text('This form has errors'),
content: const Text('Really leave this form?'),
actions: <Widget> [
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: <Widget> [
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;
}

View File

@@ -41,13 +41,13 @@ class UpdaterState extends State<Updater> {
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);

View File

@@ -95,7 +95,7 @@ class StockHomeState extends State<StockHome> {
case _StockMenuItem.refresh:
showDialog<Null>(
context: context,
child: new _NotImplementedDialog()
builder: (BuildContext context) => new _NotImplementedDialog(),
);
break;
case _StockMenuItem.speedUp:

View File

@@ -67,24 +67,26 @@ class StockSettingsState extends State<StockSettings> {
case StockMode.pessimistic:
showDialog<bool>(
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: <Widget>[
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: <Widget>[
new FlatButton(
child: const Text('NO THANKS'),
onPressed: () {
Navigator.pop(context, false);
}
),
new FlatButton(
child: const Text('AGREE'),
onPressed: () {
Navigator.pop(context, true);
}
),
],
);
},
).then<void>(_handleOptimismChanged);
break;
}