dialog.dart 24.5 KB
Newer Older
1 2 3 4
// Copyright 2015 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

5 6
import 'dart:async';

xster's avatar
xster committed
7
import 'package:flutter/foundation.dart';
8
import 'package:flutter/widgets.dart';
9

10
import 'button_bar.dart';
11
import 'button_theme.dart';
12
import 'colors.dart';
13
import 'debug.dart';
14
import 'dialog_theme.dart';
15
import 'ink_well.dart';
16
import 'material.dart';
17
import 'material_localizations.dart';
18
import 'theme.dart';
19

20 21
// Examples can assume:
// enum Department { treasury, state }
22
// BuildContext context;
23

24
/// A material design dialog.
25
///
26 27 28 29
/// This dialog widget does not have any opinion about the contents of the
/// dialog. Rather than using this widget directly, consider using [AlertDialog]
/// or [SimpleDialog], which implement specific kinds of material design
/// dialogs.
30 31
///
/// See also:
Ian Hickson's avatar
Ian Hickson committed
32
///
33 34 35
///  * [AlertDialog], for dialogs that have a message and some buttons.
///  * [SimpleDialog], for dialogs that offer a variety of options.
///  * [showDialog], which actually displays the dialog and returns its result.
36
///  * <https://material.google.com/components/dialogs.html>
37
class Dialog extends StatelessWidget {
38 39 40
  /// Creates a dialog.
  ///
  /// Typically used in conjunction with [showDialog].
41
  const Dialog({
42
    Key key,
43 44
    this.backgroundColor,
    this.elevation,
45 46
    this.insetAnimationDuration = const Duration(milliseconds: 100),
    this.insetAnimationCurve = Curves.decelerate,
47
    this.shape,
48
    this.child,
49 50
  }) : super(key: key);

51 52
  /// {@template flutter.material.dialog.backgroundColor}
  /// The background color of the surface of this [Dialog].
53
  ///
54 55 56 57 58 59 60 61 62 63 64 65 66 67
  /// This sets the [Material.color] on this [Dialog]'s [Material].
  ///
  /// If `null`, [ThemeData.cardColor] is used.
  /// {@endtemplate}
  final Color backgroundColor;

  /// {@template flutter.material.dialog.elevation}
  /// The z-coordinate of this [Dialog].
  ///
  /// If null then [DialogTheme.elevation] is used, and if that's null then the
  /// dialog's elevation is 24.0.
  /// {@endtemplate}
  /// {@macro flutter.material.material.elevation}
  final double elevation;
68

69 70 71 72 73 74 75 76 77 78 79 80
  /// The duration of the animation to show when the system keyboard intrudes
  /// into the space that the dialog is placed in.
  ///
  /// Defaults to 100 milliseconds.
  final Duration insetAnimationDuration;

  /// The curve to use for the animation shown when the system keyboard intrudes
  /// into the space that the dialog is placed in.
  ///
  /// Defaults to [Curves.fastOutSlowIn].
  final Curve insetAnimationCurve;

81 82 83 84 85 86 87 88 89
  /// {@template flutter.material.dialog.shape}
  /// The shape of this dialog's border.
  ///
  /// Defines the dialog's [Material.shape].
  ///
  /// The default shape is a [RoundedRectangleBorder] with a radius of 2.0.
  /// {@endtemplate}
  final ShapeBorder shape;

90 91 92 93
  /// The widget below this widget in the tree.
  ///
  /// {@macro flutter.widgets.child}
  final Widget child;
94

95 96 97
  // TODO(johnsonmh): Update default dialog border radius to 4.0 to match material spec.
  static const RoundedRectangleBorder _defaultDialogShape =
    RoundedRectangleBorder(borderRadius: BorderRadius.all(Radius.circular(2.0)));
98
  static const double _defaultElevation = 24.0;
99

100 101
  @override
  Widget build(BuildContext context) {
102
    final DialogTheme dialogTheme = DialogTheme.of(context);
103
    return AnimatedPadding(
104 105 106
      padding: MediaQuery.of(context).viewInsets + const EdgeInsets.symmetric(horizontal: 40.0, vertical: 24.0),
      duration: insetAnimationDuration,
      curve: insetAnimationCurve,
107
      child: MediaQuery.removeViewInsets(
108 109 110 111 112
        removeLeft: true,
        removeTop: true,
        removeRight: true,
        removeBottom: true,
        context: context,
113 114
        child: Center(
          child: ConstrainedBox(
115
            constraints: const BoxConstraints(minWidth: 280.0),
116
            child: Material(
117 118 119
              color: backgroundColor ?? dialogTheme.backgroundColor ?? Theme.of(context).dialogBackgroundColor,
              elevation: elevation ?? dialogTheme.elevation ?? _defaultElevation,
              shape: shape ?? dialogTheme.shape ?? _defaultDialogShape,
120 121 122 123 124 125
              type: MaterialType.card,
              child: child,
            ),
          ),
        ),
      ),
126 127 128 129 130 131 132 133 134 135 136 137
    );
  }
}

/// A material design alert dialog.
///
/// An alert dialog informs the user about situations that require
/// acknowledgement. An alert dialog has an optional title and an optional list
/// of actions. The title is displayed above the content and the actions are
/// displayed below the content.
///
/// If the content is too large to fit on the screen vertically, the dialog will
Ian Hickson's avatar
Ian Hickson committed
138 139 140 141 142 143 144
/// display the title and the actions and let the content overflow, which is
/// rarely desired. Consider using a scrolling widget for [content], such as
/// [SingleChildScrollView], to avoid overflow. (However, be aware that since
/// [AlertDialog] tries to size itself using the intrinsic dimensions of its
/// children, widgets such as [ListView], [GridView], and [CustomScrollView],
/// which use lazy viewports, will not work. If this is a problem, consider
/// using [Dialog] directly.)
145 146 147 148 149 150 151
///
/// For dialogs that offer the user a choice between several options, consider
/// using a [SimpleDialog].
///
/// Typically passed as the child widget to [showDialog], which displays the
/// dialog.
///
152
/// {@tool sample}
153 154 155 156 157
///
/// This snippet shows a method in a [State] which, when called, displays a dialog box
/// and returns a [Future] that completes when the dialog is dismissed.
///
/// ```dart
158 159
/// Future<void> _neverSatisfied() async {
///   return showDialog<void>(
160 161
///     context: context,
///     barrierDismissible: false, // user must tap button!
162
///     builder: (BuildContext context) {
163 164 165 166
///       return AlertDialog(
///         title: Text('Rewind and remember'),
///         content: SingleChildScrollView(
///           child: ListBody(
167
///             children: <Widget>[
168 169
///               Text('You will never be satisfied.'),
///               Text('You\’re like me. I’m never satisfied.'),
170 171
///             ],
///           ),
172
///         ),
173
///         actions: <Widget>[
174 175
///           FlatButton(
///             child: Text('Regret'),
176 177 178 179 180 181 182
///             onPressed: () {
///               Navigator.of(context).pop();
///             },
///           ),
///         ],
///       );
///     },
183 184 185
///   );
/// }
/// ```
186
/// {@end-tool}
187
///
188 189
/// See also:
///
190 191 192
///  * [SimpleDialog], which handles the scrolling of the contents but has no [actions].
///  * [Dialog], on which [AlertDialog] and [SimpleDialog] are based.
///  * [showDialog], which actually displays the dialog and returns its result.
193 194 195 196 197
///  * <https://material.google.com/components/dialogs.html#dialogs-alerts>
class AlertDialog extends StatelessWidget {
  /// Creates an alert dialog.
  ///
  /// Typically used in conjunction with [showDialog].
198 199 200 201
  ///
  /// The [contentPadding] must not be null. The [titlePadding] defaults to
  /// null, which implies a default that depends on the values of the other
  /// properties. See the documentation of [titlePadding] for details.
202
  const AlertDialog({
203
    Key key,
204
    this.title,
205
    this.titlePadding,
206
    this.titleTextStyle,
207
    this.content,
208
    this.contentPadding = const EdgeInsets.fromLTRB(24.0, 20.0, 24.0, 24.0),
209
    this.contentTextStyle,
210
    this.actions,
211 212
    this.backgroundColor,
    this.elevation,
213
    this.semanticLabel,
214
    this.shape,
215 216
  }) : assert(contentPadding != null),
       super(key: key);
217 218 219

  /// The (optional) title of the dialog is displayed in a large font at the top
  /// of the dialog.
Ian Hickson's avatar
Ian Hickson committed
220 221
  ///
  /// Typically a [Text] widget.
222 223
  final Widget title;

224 225
  /// Padding around the title.
  ///
226 227 228 229 230 231 232 233
  /// If there is no title, no padding will be provided. Otherwise, this padding
  /// is used.
  ///
  /// This property defaults to providing 24 pixels on the top, left, and right
  /// of the title. If the [content] is not null, then no bottom padding is
  /// provided (but see [contentPadding]). If it _is_ null, then an extra 20
  /// pixels of bottom padding is added to separate the [title] from the
  /// [actions].
234
  final EdgeInsetsGeometry titlePadding;
235

236 237 238 239 240 241
  /// Style for the text in the [title] of this [AlertDialog].
  ///
  /// If null, [DialogTheme.titleTextStyle] is used, if that's null, defaults to
  /// [ThemeData.textTheme.title].
  final TextStyle titleTextStyle;

242 243
  /// The (optional) content of the dialog is displayed in the center of the
  /// dialog in a lighter font.
Ian Hickson's avatar
Ian Hickson committed
244
  ///
245 246
  /// Typically, this is a [ListView] containing the contents of the dialog.
  /// Using a [ListView] ensures that the contents can scroll if they are too
247
  /// big to fit on the display.
248 249
  final Widget content;

250 251
  /// Padding around the content.
  ///
252 253 254 255
  /// If there is no content, no padding will be provided. Otherwise, padding of
  /// 20 pixels is provided above the content to separate the content from the
  /// title, and padding of 24 pixels is provided on the left, right, and bottom
  /// to separate the content from the other edges of the dialog.
256
  final EdgeInsetsGeometry contentPadding;
257

258 259 260 261 262 263
  /// Style for the text in the [content] of this [AlertDialog].
  ///
  /// If null, [DialogTheme.contentTextStyle] is used, if that's null, defaults
  /// to [ThemeData.textTheme.subhead].
  final TextStyle contentTextStyle;

264 265
  /// The (optional) set of actions that are displayed at the bottom of the
  /// dialog.
Ian Hickson's avatar
Ian Hickson committed
266 267 268
  ///
  /// Typically this is a list of [FlatButton] widgets.
  ///
269 270 271 272 273 274
  /// These widgets will be wrapped in a [ButtonBar], which introduces 8 pixels
  /// of padding on each side.
  ///
  /// If the [title] is not null but the [content] _is_ null, then an extra 20
  /// pixels of padding is added above the [ButtonBar] to separate the [title]
  /// from the [actions].
275 276
  final List<Widget> actions;

277 278 279 280 281 282 283
  /// {@macro flutter.material.dialog.backgroundColor}
  final Color backgroundColor;

  /// {@macro flutter.material.dialog.elevation}
  /// {@macro flutter.material.material.elevation}
  final double elevation;

284
  /// The semantic label of the dialog used by accessibility frameworks to
285
  /// announce screen transitions when the dialog is opened and closed.
286
  ///
287 288 289
  /// If this label is not provided, a semantic label will be infered from the
  /// [title] if it is not null.  If there is no title, the label will be taken
  /// from [MaterialLocalizations.alertDialogLabel].
290
  ///
291
  /// See also:
292
  ///
293 294 295 296
  ///  * [SemanticsConfiguration.isRouteName], for a description of how this
  ///    value is used.
  final String semanticLabel;

297 298 299
  /// {@macro flutter.material.dialog.shape}
  final ShapeBorder shape;

300
  @override
301
  Widget build(BuildContext context) {
302
    assert(debugCheckHasMaterialLocalizations(context));
303 304
    final ThemeData theme = Theme.of(context);
    final DialogTheme dialogTheme = DialogTheme.of(context);
305
    final List<Widget> children = <Widget>[];
306
    String label = semanticLabel;
307 308

    if (title != null) {
309 310 311
      children.add(Padding(
        padding: titlePadding ?? EdgeInsets.fromLTRB(24.0, 24.0, 24.0, content == null ? 20.0 : 0.0),
        child: DefaultTextStyle(
312
          style: titleTextStyle ?? dialogTheme.titleTextStyle ?? theme.textTheme.title,
313
          child: Semantics(child: title, namesRoute: true),
314
        ),
315
      ));
316 317 318 319 320 321 322 323 324
    } else {
      switch (defaultTargetPlatform) {
        case TargetPlatform.iOS:
          label = semanticLabel;
          break;
        case TargetPlatform.android:
        case TargetPlatform.fuchsia:
          label = semanticLabel ?? MaterialLocalizations.of(context)?.alertDialogLabel;
      }
325 326 327
    }

    if (content != null) {
328 329
      children.add(Flexible(
        child: Padding(
330
          padding: contentPadding,
331
          child: DefaultTextStyle(
332
            style: contentTextStyle ?? dialogTheme.contentTextStyle ?? theme.textTheme.subhead,
333 334 335
            child: content,
          ),
        ),
336 337 338
      ));
    }

339
    if (actions != null) {
340 341
      children.add(ButtonTheme.bar(
        child: ButtonBar(
342 343
          children: actions,
        ),
344
      ));
345
    }
346

347 348
    Widget dialogChild = IntrinsicWidth(
      child: Column(
349 350 351
        mainAxisSize: MainAxisSize.min,
        crossAxisAlignment: CrossAxisAlignment.stretch,
        children: children,
352
      ),
353
    );
354 355

    if (label != null)
356
      dialogChild = Semantics(
357 358 359 360 361
        namesRoute: true,
        label: label,
        child: dialogChild
      );

362 363 364 365 366 367
    return Dialog(
      backgroundColor: backgroundColor,
      elevation: elevation,
      shape: shape,
      child: dialogChild,
    );
368 369 370
  }
}

371 372 373 374 375 376 377
/// An option used in a [SimpleDialog].
///
/// A simple dialog offers the user a choice between several options. This
/// widget is commonly used to represent each of the options. If the user
/// selects this option, the widget will call the [onPressed] callback, which
/// typically uses [Navigator.pop] to close the dialog.
///
378 379 380 381 382 383
/// The padding on a [SimpleDialogOption] is configured to combine with the
/// default [SimpleDialog.contentPadding] so that each option ends up 8 pixels
/// from the other vertically, with 20 pixels of spacing between the dialog's
/// title and the first option, and 24 pixels of spacing between the last option
/// and the bottom of the dialog.
///
384
/// {@tool sample}
385 386
///
/// ```dart
387
/// SimpleDialogOption(
388 389 390 391
///   onPressed: () { Navigator.pop(context, Department.treasury); },
///   child: const Text('Treasury department'),
/// )
/// ```
392
/// {@end-tool}
393
///
394 395 396 397 398 399 400 401 402
/// See also:
///
///  * [SimpleDialog], for a dialog in which to use this widget.
///  * [showDialog], which actually displays the dialog and returns its result.
///  * [FlatButton], which are commonly used as actions in other kinds of
///    dialogs, such as [AlertDialog]s.
///  * <https://material.google.com/components/dialogs.html#dialogs-simple-dialogs>
class SimpleDialogOption extends StatelessWidget {
  /// Creates an option for a [SimpleDialog].
403
  const SimpleDialogOption({
404 405 406 407 408 409 410 411
    Key key,
    this.onPressed,
    this.child,
  }) : super(key: key);

  /// The callback that is called when this option is selected.
  ///
  /// If this is set to null, the option cannot be selected.
412 413 414
  ///
  /// When used in a [SimpleDialog], this will typically call [Navigator.pop]
  /// with a value for [showDialog] to complete its future with.
415 416 417 418 419 420 421 422 423
  final VoidCallback onPressed;

  /// The widget below this widget in the tree.
  ///
  /// Typically a [Text] widget.
  final Widget child;

  @override
  Widget build(BuildContext context) {
424
    return InkWell(
425
      onTap: onPressed,
426
      child: Padding(
427 428 429 430 431 432 433
        padding: const EdgeInsets.symmetric(vertical: 8.0, horizontal: 24.0),
        child: child
      ),
    );
  }
}

434 435 436 437 438
/// A simple material design dialog.
///
/// A simple dialog offers the user a choice between several options. A simple
/// dialog has an optional title that is displayed above the choices.
///
439 440 441 442
/// Choices are normally represented using [SimpleDialogOption] widgets. If
/// other widgets are used, see [contentPadding] for notes regarding the
/// conventions for obtaining the spacing expected by Material Design.
///
443 444 445 446 447 448
/// For dialogs that inform the user about a situation, consider using an
/// [AlertDialog].
///
/// Typically passed as the child widget to [showDialog], which displays the
/// dialog.
///
449
/// {@tool sample}
450 451 452 453 454 455 456 457 458 459 460 461 462
///
/// In this example, the user is asked to select between two options. These
/// options are represented as an enum. The [showDialog] method here returns
/// a [Future] that completes to a value of that enum. If the user cancels
/// the dialog (e.g. by hitting the back button on Android, or tapping on the
/// mask behind the dialog) then the future completes with the null value.
///
/// The return value in this example is used as the index for a switch statement.
/// One advantage of using an enum as the return value and then using that to
/// drive a switch statement is that the analyzer will flag any switch statement
/// that doesn't mention every value in the enum.
///
/// ```dart
463
/// Future<void> _askedToLead() async {
464 465
///   switch (await showDialog<Department>(
///     context: context,
466
///     builder: (BuildContext context) {
467
///       return SimpleDialog(
468 469
///         title: const Text('Select assignment'),
///         children: <Widget>[
470
///           SimpleDialogOption(
471 472 473
///             onPressed: () { Navigator.pop(context, Department.treasury); },
///             child: const Text('Treasury department'),
///           ),
474
///           SimpleDialogOption(
475 476 477 478 479 480
///             onPressed: () { Navigator.pop(context, Department.state); },
///             child: const Text('State department'),
///           ),
///         ],
///       );
///     }
481 482 483 484 485 486 487 488 489 490 491
///   )) {
///     case Department.treasury:
///       // Let's go.
///       // ...
///     break;
///     case Department.state:
///       // ...
///     break;
///   }
/// }
/// ```
492
/// {@end-tool}
493
///
494 495
/// See also:
///
496
///  * [SimpleDialogOption], which are options used in this type of dialog.
497 498 499
///  * [AlertDialog], for dialogs that have a row of buttons below the body.
///  * [Dialog], on which [SimpleDialog] and [AlertDialog] are based.
///  * [showDialog], which actually displays the dialog and returns its result.
500 501 502 503 504
///  * <https://material.google.com/components/dialogs.html#dialogs-simple-dialogs>
class SimpleDialog extends StatelessWidget {
  /// Creates a simple dialog.
  ///
  /// Typically used in conjunction with [showDialog].
505 506
  ///
  /// The [titlePadding] and [contentPadding] arguments must not be null.
507
  const SimpleDialog({
508 509
    Key key,
    this.title,
510
    this.titlePadding = const EdgeInsets.fromLTRB(24.0, 24.0, 24.0, 0.0),
511
    this.children,
512
    this.contentPadding = const EdgeInsets.fromLTRB(0.0, 12.0, 0.0, 16.0),
513 514
    this.backgroundColor,
    this.elevation,
515
    this.semanticLabel,
516
    this.shape,
517 518 519
  }) : assert(titlePadding != null),
       assert(contentPadding != null),
       super(key: key);
520 521 522 523 524 525 526 527 528

  /// The (optional) title of the dialog is displayed in a large font at the top
  /// of the dialog.
  ///
  /// Typically a [Text] widget.
  final Widget title;

  /// Padding around the title.
  ///
529 530 531 532 533 534 535
  /// If there is no title, no padding will be provided.
  ///
  /// By default, this provides the recommend Material Design padding of 24
  /// pixels around the left, top, and right edges of the title.
  ///
  /// See [contentPadding] for the conventions regarding padding between the
  /// [title] and the [children].
536
  final EdgeInsetsGeometry titlePadding;
537

538 539
  /// The (optional) content of the dialog is displayed in a
  /// [SingleChildScrollView] underneath the title.
540
  ///
541
  /// Typically a list of [SimpleDialogOption]s.
542 543 544 545
  final List<Widget> children;

  /// Padding around the content.
  ///
546 547 548 549 550 551 552 553 554 555
  /// By default, this is 12 pixels on the top and 16 pixels on the bottom. This
  /// is intended to be combined with children that have 24 pixels of padding on
  /// the left and right, and 8 pixels of padding on the top and bottom, so that
  /// the content ends up being indented 20 pixels from the title, 24 pixels
  /// from the bottom, and 24 pixels from the sides.
  ///
  /// The [SimpleDialogOption] widget uses such padding.
  ///
  /// If there is no [title], the [contentPadding] should be adjusted so that
  /// the top padding ends up being 24 pixels.
556
  final EdgeInsetsGeometry contentPadding;
557

558 559 560 561 562 563 564
  /// {@macro flutter.material.dialog.backgroundColor}
  final Color backgroundColor;

  /// {@macro flutter.material.dialog.elevation}
  /// {@macro flutter.material.material.elevation}
  final double elevation;

565
  /// The semantic label of the dialog used by accessibility frameworks to
566
  /// announce screen transitions when the dialog is opened and closed.
567
  ///
568 569 570
  /// If this label is not provided, a semantic label will be infered from the
  /// [title] if it is not null.  If there is no title, the label will be taken
  /// from [MaterialLocalizations.dialogLabel].
571
  ///
572
  /// See also:
573
  ///
574 575 576 577
  ///  * [SemanticsConfiguration.isRouteName], for a description of how this
  ///    value is used.
  final String semanticLabel;

578 579 580
  /// {@macro flutter.material.dialog.shape}
  final ShapeBorder shape;

581 582
  @override
  Widget build(BuildContext context) {
583
    assert(debugCheckHasMaterialLocalizations(context));
584
    final List<Widget> body = <Widget>[];
585
    String label = semanticLabel;
586 587

    if (title != null) {
588
      body.add(Padding(
589
        padding: titlePadding,
590
        child: DefaultTextStyle(
591
          style: Theme.of(context).textTheme.title,
592
          child: Semantics(namesRoute: true, child: title),
593 594
        )
      ));
595 596 597 598 599 600 601 602 603
    } else {
      switch (defaultTargetPlatform) {
        case TargetPlatform.iOS:
          label = semanticLabel;
          break;
        case TargetPlatform.android:
        case TargetPlatform.fuchsia:
          label = semanticLabel ?? MaterialLocalizations.of(context)?.dialogLabel;
      }
604 605 606
    }

    if (children != null) {
607 608
      body.add(Flexible(
        child: SingleChildScrollView(
609
          padding: contentPadding,
610
          child: ListBody(children: children),
611 612 613 614
        )
      ));
    }

615
    Widget dialogChild = IntrinsicWidth(
616
      stepWidth: 56.0,
617
      child: ConstrainedBox(
618
        constraints: const BoxConstraints(minWidth: 280.0),
619
        child: Column(
620 621 622 623 624
          mainAxisSize: MainAxisSize.min,
          crossAxisAlignment: CrossAxisAlignment.stretch,
          children: body,
        ),
      ),
625
    );
626 627

    if (label != null)
628
      dialogChild = Semantics(
629 630 631 632
        namesRoute: true,
        label: label,
        child: dialogChild,
      );
633 634 635 636 637 638
    return Dialog(
      backgroundColor: backgroundColor,
      elevation: elevation,
      shape: shape,
      child: dialogChild,
    );
639 640
  }
}
641

642
Widget _buildMaterialDialogTransitions(BuildContext context, Animation<double> animation, Animation<double> secondaryAnimation, Widget child) {
643 644
  return FadeTransition(
    opacity: CurvedAnimation(
645 646 647 648 649
      parent: animation,
      curve: Curves.easeOut,
    ),
    child: child,
  );
650 651
}

652 653 654
/// Displays a Material dialog above the current contents of the app, with
/// Material entrance and exit animations, modal barrier color, and modal
/// barrier behavior (dialog is dismissible with a tap on the barrier).
655
///
656
/// This function takes a `builder` which typically builds a [Dialog] widget.
657 658 659 660
/// Content below the dialog is dimmed with a [ModalBarrier]. The widget
/// returned by the `builder` 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.
661
///
Ian Hickson's avatar
Ian Hickson committed
662 663 664 665
/// 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.
///
666 667
/// The `child` argument is deprecated, and should be replaced with `builder`.
///
668
/// Returns a [Future] that resolves to the value (if any) that was passed to
669 670
/// [Navigator.pop] when the dialog was closed.
///
671 672 673
/// The dialog route created by this method is pushed to the root navigator.
/// If the application has multiple [Navigator] objects, it may be necessary to
/// call `Navigator.of(context, rootNavigator: true).pop(result)` to close the
674
/// dialog rather than just `Navigator.pop(context, result)`.
675
///
676
/// See also:
677
///
678 679 680
///  * [AlertDialog], for dialogs that have a row of buttons below a body.
///  * [SimpleDialog], which handles the scrolling of the contents and does
///    not show buttons below its body.
681
///  * [Dialog], on which [SimpleDialog] and [AlertDialog] are based.
682 683
///  * [showCupertinoDialog], which displays an iOS-style dialog.
///  * [showGeneralDialog], which allows for customization of the dialog popup.
684
///  * <https://material.google.com/components/dialogs.html>
685
Future<T> showDialog<T>({
686
  @required BuildContext context,
687
  bool barrierDismissible = true,
688 689 690 691 692 693
  @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,
694
}) {
695
  assert(child == null || builder == null);
696
  assert(debugCheckHasMaterialLocalizations(context));
697 698 699 700
  return showGeneralDialog(
    context: context,
    pageBuilder: (BuildContext buildContext, Animation<double> animation, Animation<double> secondaryAnimation) {
      final ThemeData theme = Theme.of(context, shadowThemeOnly: true);
701 702 703
      final Widget pageChild =  child ?? Builder(builder: builder);
      return SafeArea(
        child: Builder(
704 705
          builder: (BuildContext context) {
            return theme != null
706
                ? Theme(data: theme, child: pageChild)
707 708 709 710 711
                : pageChild;
          }
        ),
      );
    },
712
    barrierDismissible: barrierDismissible,
713
    barrierLabel: MaterialLocalizations.of(context).modalBarrierDismissLabel,
714 715 716 717
    barrierColor: Colors.black54,
    transitionDuration: const Duration(milliseconds: 150),
    transitionBuilder: _buildMaterialDialogTransitions,
  );
718
}