dialog.dart 24.8 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 'colors.dart';
12
import 'debug.dart';
13
import 'dialog_theme.dart';
14
import 'ink_well.dart';
15
import 'material.dart';
16
import 'material_localizations.dart';
17
import 'theme.dart';
18

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

23
/// A material design dialog.
24
///
25 26 27 28
/// 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.
29 30
///
/// See also:
Ian Hickson's avatar
Ian Hickson committed
31
///
32 33 34
///  * [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.
35
///  * <https://material.io/design/components/dialogs.html>
36
class Dialog extends StatelessWidget {
37 38 39
  /// Creates a dialog.
  ///
  /// Typically used in conjunction with [showDialog].
40
  const Dialog({
41
    Key key,
42 43
    this.backgroundColor,
    this.elevation,
44 45
    this.insetAnimationDuration = const Duration(milliseconds: 100),
    this.insetAnimationCurve = Curves.decelerate,
46
    this.shape,
47
    this.child,
48 49
  }) : super(key: key);

50 51
  /// {@template flutter.material.dialog.backgroundColor}
  /// The background color of the surface of this [Dialog].
52
  ///
53 54 55 56 57 58 59 60 61 62 63 64 65 66
  /// 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;
67

68 69 70 71 72 73 74 75 76 77 78 79
  /// 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;

80 81 82 83 84 85 86 87 88
  /// {@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;

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

94 95 96
  // 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)));
97
  static const double _defaultElevation = 24.0;
98

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

/// 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
137 138 139 140 141 142 143
/// 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.)
144 145 146 147 148 149 150
///
/// 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.
///
151
/// {@tool sample}
152 153 154 155 156
///
/// 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
157 158
/// Future<void> _neverSatisfied() async {
///   return showDialog<void>(
159 160
///     context: context,
///     barrierDismissible: false, // user must tap button!
161
///     builder: (BuildContext context) {
162 163 164 165
///       return AlertDialog(
///         title: Text('Rewind and remember'),
///         content: SingleChildScrollView(
///           child: ListBody(
166
///             children: <Widget>[
167 168
///               Text('You will never be satisfied.'),
///               Text('You\’re like me. I’m never satisfied.'),
169 170
///             ],
///           ),
171
///         ),
172
///         actions: <Widget>[
173 174
///           FlatButton(
///             child: Text('Regret'),
175 176 177 178 179 180 181
///             onPressed: () {
///               Navigator.of(context).pop();
///             },
///           ),
///         ],
///       );
///     },
182 183 184
///   );
/// }
/// ```
185
/// {@end-tool}
186
///
187 188
/// See also:
///
189 190
///  * [SimpleDialog], which handles the scrolling of the contents but has no [actions].
///  * [Dialog], on which [AlertDialog] and [SimpleDialog] are based.
191
///  * [CupertinoAlertDialog], an iOS-styled alert dialog.
192
///  * [showDialog], which actually displays the dialog and returns its result.
193
///  * <https://material.io/design/components/dialogs.html#alert-dialog>
194 195 196 197
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 247 248
  /// Typically this is a [SingleChildScrollView] that contains the dialog's
  /// message. As noted in the [AlertDialog] documentation, it's important
  /// to use a [SingleChildScrollView] if there's any risk that the content
  /// will not fit.
249 250
  final Widget content;

251 252
  /// Padding around the content.
  ///
253 254 255 256
  /// 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.
257
  final EdgeInsetsGeometry contentPadding;
258

259 260 261 262 263 264
  /// 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;

265 266
  /// The (optional) set of actions that are displayed at the bottom of the
  /// dialog.
Ian Hickson's avatar
Ian Hickson committed
267 268 269
  ///
  /// Typically this is a list of [FlatButton] widgets.
  ///
270 271 272 273 274 275
  /// 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].
276 277
  final List<Widget> actions;

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

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

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

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

301
  @override
302
  Widget build(BuildContext context) {
303
    assert(debugCheckHasMaterialLocalizations(context));
304 305
    final ThemeData theme = Theme.of(context);
    final DialogTheme dialogTheme = DialogTheme.of(context);
306

307 308
    String label = semanticLabel;
    if (title == null) {
309
      switch (theme.platform) {
310 311 312 313 314 315 316
        case TargetPlatform.iOS:
          label = semanticLabel;
          break;
        case TargetPlatform.android:
        case TargetPlatform.fuchsia:
          label = semanticLabel ?? MaterialLocalizations.of(context)?.alertDialogLabel;
      }
317 318
    }

319 320
    Widget dialogChild = IntrinsicWidth(
      child: Column(
321 322
        mainAxisSize: MainAxisSize.min,
        crossAxisAlignment: CrossAxisAlignment.stretch,
323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350
        children: <Widget>[
          if (title != null)
            Padding(
              padding: titlePadding ?? EdgeInsets.fromLTRB(24.0, 24.0, 24.0, content == null ? 20.0 : 0.0),
              child: DefaultTextStyle(
                style: titleTextStyle ?? dialogTheme.titleTextStyle ?? theme.textTheme.title,
                child: Semantics(
                  child: title,
                  namesRoute: true,
                  container: true,
                ),
              ),
            ),
          if (content != null)
            Flexible(
              child: Padding(
                padding: contentPadding,
                child: DefaultTextStyle(
                  style: contentTextStyle ?? dialogTheme.contentTextStyle ?? theme.textTheme.subhead,
                  child: content,
                ),
              ),
            ),
          if (actions != null)
            ButtonBar(
              children: actions,
            ),
        ],
351
      ),
352
    );
353 354

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

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

370 371 372 373 374 375 376
/// 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.
///
377 378 379 380 381 382
/// 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.
///
383
/// {@tool sample}
384 385
///
/// ```dart
386
/// SimpleDialogOption(
387 388 389 390
///   onPressed: () { Navigator.pop(context, Department.treasury); },
///   child: const Text('Treasury department'),
/// )
/// ```
391
/// {@end-tool}
392
///
393 394 395 396 397 398
/// 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.
399
///  * <https://material.io/design/components/dialogs.html#simple-dialog>
400 401
class SimpleDialogOption extends StatelessWidget {
  /// Creates an option for a [SimpleDialog].
402
  const SimpleDialogOption({
403 404 405 406 407 408 409 410
    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.
411 412 413
  ///
  /// When used in a [SimpleDialog], this will typically call [Navigator.pop]
  /// with a value for [showDialog] to complete its future with.
414 415 416 417 418 419 420 421 422
  final VoidCallback onPressed;

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

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

433 434 435 436 437
/// 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.
///
438 439 440 441
/// 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.
///
442 443 444 445 446 447
/// 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.
///
448
/// {@tool sample}
449 450 451 452 453 454 455 456 457 458 459 460 461
///
/// 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
462
/// Future<void> _askedToLead() async {
463 464
///   switch (await showDialog<Department>(
///     context: context,
465
///     builder: (BuildContext context) {
466
///       return SimpleDialog(
467 468
///         title: const Text('Select assignment'),
///         children: <Widget>[
469
///           SimpleDialogOption(
470 471 472
///             onPressed: () { Navigator.pop(context, Department.treasury); },
///             child: const Text('Treasury department'),
///           ),
473
///           SimpleDialogOption(
474 475 476 477 478 479
///             onPressed: () { Navigator.pop(context, Department.state); },
///             child: const Text('State department'),
///           ),
///         ],
///       );
///     }
480 481 482 483 484 485 486 487 488 489 490
///   )) {
///     case Department.treasury:
///       // Let's go.
///       // ...
///     break;
///     case Department.state:
///       // ...
///     break;
///   }
/// }
/// ```
491
/// {@end-tool}
492
///
493 494
/// See also:
///
495
///  * [SimpleDialogOption], which are options used in this type of dialog.
496 497 498
///  * [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.
499
///  * <https://material.io/design/components/dialogs.html#simple-dialog>
500 501 502 503
class SimpleDialog extends StatelessWidget {
  /// Creates a simple dialog.
  ///
  /// Typically used in conjunction with [showDialog].
504 505
  ///
  /// The [titlePadding] and [contentPadding] arguments must not be null.
506
  const SimpleDialog({
507 508
    Key key,
    this.title,
509
    this.titlePadding = const EdgeInsets.fromLTRB(24.0, 24.0, 24.0, 0.0),
510
    this.children,
511
    this.contentPadding = const EdgeInsets.fromLTRB(0.0, 12.0, 0.0, 16.0),
512 513
    this.backgroundColor,
    this.elevation,
514
    this.semanticLabel,
515
    this.shape,
516 517 518
  }) : assert(titlePadding != null),
       assert(contentPadding != null),
       super(key: key);
519 520 521 522 523 524 525 526 527

  /// 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.
  ///
528 529 530 531 532 533 534
  /// 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].
535
  final EdgeInsetsGeometry titlePadding;
536

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

  /// Padding around the content.
  ///
545 546 547 548 549 550 551 552 553 554
  /// 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.
555
  final EdgeInsetsGeometry contentPadding;
556

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

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

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

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

580 581
  @override
  Widget build(BuildContext context) {
582
    assert(debugCheckHasMaterialLocalizations(context));
583 584
    final ThemeData theme = Theme.of(context);

585 586
    String label = semanticLabel;
    if (title == null) {
587
      switch (theme.platform) {
588 589 590 591 592 593 594
        case TargetPlatform.iOS:
          label = semanticLabel;
          break;
        case TargetPlatform.android:
        case TargetPlatform.fuchsia:
          label = semanticLabel ?? MaterialLocalizations.of(context)?.dialogLabel;
      }
595 596
    }

597
    Widget dialogChild = IntrinsicWidth(
598
      stepWidth: 56.0,
599
      child: ConstrainedBox(
600
        constraints: const BoxConstraints(minWidth: 280.0),
601
        child: Column(
602 603
          mainAxisSize: MainAxisSize.min,
          crossAxisAlignment: CrossAxisAlignment.stretch,
604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620
          children: <Widget>[
            if (title != null)
              Padding(
                padding: titlePadding,
                child: DefaultTextStyle(
                  style: theme.textTheme.title,
                  child: Semantics(namesRoute: true, child: title),
                ),
              ),
            if (children != null)
              Flexible(
                child: SingleChildScrollView(
                  padding: contentPadding,
                  child: ListBody(children: children),
                ),
              ),
          ],
621 622
        ),
      ),
623
    );
624 625

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

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

650 651 652
/// 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).
653
///
654
/// This function takes a `builder` which typically builds a [Dialog] widget.
655 656 657 658
/// 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.
659
///
Ian Hickson's avatar
Ian Hickson committed
660 661 662 663
/// 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.
///
664 665
/// The `child` argument is deprecated, and should be replaced with `builder`.
///
666
/// Returns a [Future] that resolves to the value (if any) that was passed to
667 668
/// [Navigator.pop] when the dialog was closed.
///
669 670 671
/// 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
672
/// dialog rather than just `Navigator.pop(context, result)`.
673
///
674
/// See also:
675
///
676 677 678
///  * [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.
679
///  * [Dialog], on which [SimpleDialog] and [AlertDialog] are based.
680 681
///  * [showCupertinoDialog], which displays an iOS-style dialog.
///  * [showGeneralDialog], which allows for customization of the dialog popup.
682
///  * <https://material.io/design/components/dialogs.html>
683
Future<T> showDialog<T>({
684
  @required BuildContext context,
685
  bool barrierDismissible = true,
686 687 688 689 690 691
  @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,
692
}) {
693
  assert(child == null || builder == null);
694
  assert(debugCheckHasMaterialLocalizations(context));
695 696

  final ThemeData theme = Theme.of(context, shadowThemeOnly: true);
697 698 699
  return showGeneralDialog(
    context: context,
    pageBuilder: (BuildContext buildContext, Animation<double> animation, Animation<double> secondaryAnimation) {
700
      final Widget pageChild = child ?? Builder(builder: builder);
701 702
      return SafeArea(
        child: Builder(
703 704
          builder: (BuildContext context) {
            return theme != null
705
                ? Theme(data: theme, child: pageChild)
706 707 708 709 710
                : pageChild;
          }
        ),
      );
    },
711
    barrierDismissible: barrierDismissible,
712
    barrierLabel: MaterialLocalizations.of(context).modalBarrierDismissLabel,
713 714 715 716
    barrierColor: Colors.black54,
    transitionDuration: const Duration(milliseconds: 150),
    transitionBuilder: _buildMaterialDialogTransitions,
  );
717
}