Hibok
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

740 lines
26 KiB

  1. // Copyright 2015 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. import 'dart:async';
  5. import 'package:flutter/foundation.dart';
  6. import 'package:flutter/material.dart';
  7. import 'package:flutter/widgets.dart';
  8. // Examples can assume:
  9. // enum Department { treasury, state }
  10. // BuildContext context;
  11. /// A material design dialog.
  12. ///
  13. /// This dialog widget does not have any opinion about the contents of the
  14. /// dialog. Rather than using this widget directly, consider using [AlertDialog]
  15. /// or [SimpleDialog], which implement specific kinds of material design
  16. /// dialogs.
  17. ///
  18. /// See also:
  19. ///
  20. /// * [AlertDialog], for dialogs that have a message and some buttons.
  21. /// * [SimpleDialog], for dialogs that offer a variety of options.
  22. /// * [showDialog], which actually displays the dialog and returns its result.
  23. /// * <https://material.io/design/components/dialogs.html>
  24. class Dialog extends StatelessWidget {
  25. /// Creates a dialog.
  26. ///
  27. /// Typically used in conjunction with [showDialog].
  28. const Dialog({
  29. Key key,
  30. this.backgroundColor,
  31. this.elevation,
  32. this.insetAnimationDuration = const Duration(milliseconds: 100),
  33. this.insetAnimationCurve = Curves.decelerate,
  34. this.shape,
  35. this.child,
  36. }) : super(key: key);
  37. /// {@template flutter.material.dialog.backgroundColor}
  38. /// The background color of the surface of this [Dialog].
  39. ///
  40. /// This sets the [Material.color] on this [Dialog]'s [Material].
  41. ///
  42. /// If `null`, [ThemeData.cardColor] is used.
  43. /// {@endtemplate}
  44. final Color backgroundColor;
  45. /// {@template flutter.material.dialog.elevation}
  46. /// The z-coordinate of this [Dialog].
  47. ///
  48. /// If null then [DialogTheme.elevation] is used, and if that's null then the
  49. /// dialog's elevation is 24.0.
  50. /// {@endtemplate}
  51. /// {@macro flutter.material.material.elevation}
  52. final double elevation;
  53. /// The duration of the animation to show when the system keyboard intrudes
  54. /// into the space that the dialog is placed in.
  55. ///
  56. /// Defaults to 100 milliseconds.
  57. final Duration insetAnimationDuration;
  58. /// The curve to use for the animation shown when the system keyboard intrudes
  59. /// into the space that the dialog is placed in.
  60. ///
  61. /// Defaults to [Curves.fastOutSlowIn].
  62. final Curve insetAnimationCurve;
  63. /// {@template flutter.material.dialog.shape}
  64. /// The shape of this dialog's border.
  65. ///
  66. /// Defines the dialog's [Material.shape].
  67. ///
  68. /// The default shape is a [RoundedRectangleBorder] with a radius of 2.0.
  69. /// {@endtemplate}
  70. final ShapeBorder shape;
  71. /// The widget below this widget in the tree.
  72. ///
  73. /// {@macro flutter.widgets.child}
  74. final Widget child;
  75. static const RoundedRectangleBorder _defaultDialogShape =
  76. RoundedRectangleBorder(
  77. borderRadius: BorderRadius.all(Radius.circular(2.0)));
  78. static const double _defaultElevation = 24.0;
  79. @override
  80. Widget build(BuildContext context) {
  81. final DialogTheme dialogTheme = DialogTheme.of(context);
  82. return AnimatedPadding(
  83. padding: MediaQuery.of(context).viewInsets +
  84. const EdgeInsets.symmetric(horizontal: 40.0, vertical: 24.0),
  85. duration: insetAnimationDuration,
  86. curve: insetAnimationCurve,
  87. child: MediaQuery.removeViewInsets(
  88. removeLeft: true,
  89. removeTop: true,
  90. removeRight: true,
  91. removeBottom: true,
  92. context: context,
  93. child: Center(
  94. child: ConstrainedBox(
  95. constraints: const BoxConstraints(minWidth: 280.0),
  96. child: Material(
  97. color: backgroundColor ??
  98. dialogTheme.backgroundColor ??
  99. Theme.of(context).dialogBackgroundColor,
  100. elevation:
  101. elevation ?? dialogTheme.elevation ?? _defaultElevation,
  102. shape: shape ?? dialogTheme.shape ?? _defaultDialogShape,
  103. type: MaterialType.card,
  104. child: child,
  105. ),
  106. ),
  107. ),
  108. ),
  109. );
  110. }
  111. }
  112. /// A material design alert dialog.
  113. ///
  114. /// An alert dialog informs the user about situations that require
  115. /// acknowledgement. An alert dialog has an optional title and an optional list
  116. /// of actions. The title is displayed above the content and the actions are
  117. /// displayed below the content.
  118. ///
  119. /// If the content is too large to fit on the screen vertically, the dialog will
  120. /// display the title and the actions and let the content overflow, which is
  121. /// rarely desired. Consider using a scrolling widget for [content], such as
  122. /// [SingleChildScrollView], to avoid overflow. (However, be aware that since
  123. /// [AlertDialog] tries to size itself using the intrinsic dimensions of its
  124. /// children, widgets such as [ListView], [GridView], and [CustomScrollView],
  125. /// which use lazy viewports, will not work. If this is a problem, consider
  126. /// using [Dialog] directly.)
  127. ///
  128. /// For dialogs that offer the user a choice between several options, consider
  129. /// using a [SimpleDialog].
  130. ///
  131. /// Typically passed as the child widget to [showDialog], which displays the
  132. /// dialog.
  133. ///
  134. /// {@tool sample}
  135. ///
  136. /// This snippet shows a method in a [State] which, when called, displays a dialog box
  137. /// and returns a [Future] that completes when the dialog is dismissed.
  138. ///
  139. /// ```dart
  140. /// Future<void> _neverSatisfied() async {
  141. /// return showDialog<void>(
  142. /// context: context,
  143. /// barrierDismissible: false, // user must tap button!
  144. /// builder: (BuildContext context) {
  145. /// return AlertDialog(
  146. /// title: Text('Rewind and remember'),
  147. /// content: SingleChildScrollView(
  148. /// child: ListBody(
  149. /// children: <Widget>[
  150. /// Text('You will never be satisfied.'),
  151. /// Text('You\’re like me. I’m never satisfied.'),
  152. /// ],
  153. /// ),
  154. /// ),
  155. /// actions: <Widget>[
  156. /// FlatButton(
  157. /// child: Text('Regret'),
  158. /// onPressed: () {
  159. /// Navigator.of(context).pop();
  160. /// },
  161. /// ),
  162. /// ],
  163. /// );
  164. /// },
  165. /// );
  166. /// }
  167. /// ```
  168. /// {@end-tool}
  169. ///
  170. /// See also:
  171. ///
  172. /// * [SimpleDialog], which handles the scrolling of the contents but has no [actions].
  173. /// * [Dialog], on which [AlertDialog] and [SimpleDialog] are based.
  174. /// * [CupertinoAlertDialog], an iOS-styled alert dialog.
  175. /// * [showDialog], which actually displays the dialog and returns its result.
  176. /// * <https://material.io/design/components/dialogs.html#alert-dialog>
  177. class AlertDialog extends StatelessWidget {
  178. /// Creates an alert dialog.
  179. ///
  180. /// Typically used in conjunction with [showDialog].
  181. ///
  182. /// The [contentPadding] must not be null. The [titlePadding] defaults to
  183. /// null, which implies a default that depends on the values of the other
  184. /// properties. See the documentation of [titlePadding] for details.
  185. const AlertDialog({
  186. Key key,
  187. this.title,
  188. this.titlePadding,
  189. this.titleTextStyle,
  190. this.content,
  191. this.contentPadding = const EdgeInsets.fromLTRB(24.0, 20.0, 24.0, 24.0),
  192. this.contentTextStyle,
  193. this.actions,
  194. this.backgroundColor,
  195. this.elevation,
  196. this.semanticLabel,
  197. this.shape,
  198. }) : assert(contentPadding != null),
  199. super(key: key);
  200. /// The (optional) title of the dialog is displayed in a large font at the top
  201. /// of the dialog.
  202. ///
  203. /// Typically a [Text] widget.
  204. final Widget title;
  205. /// Padding around the title.
  206. ///
  207. /// If there is no title, no padding will be provided. Otherwise, this padding
  208. /// is used.
  209. ///
  210. /// This property defaults to providing 24 pixels on the top, left, and right
  211. /// of the title. If the [content] is not null, then no bottom padding is
  212. /// provided (but see [contentPadding]). If it _is_ null, then an extra 20
  213. /// pixels of bottom padding is added to separate the [title] from the
  214. /// [actions].
  215. final EdgeInsetsGeometry titlePadding;
  216. /// Style for the text in the [title] of this [AlertDialog].
  217. ///
  218. /// If null, [DialogTheme.titleTextStyle] is used, if that's null, defaults to
  219. /// [ThemeData.textTheme.title].
  220. final TextStyle titleTextStyle;
  221. /// The (optional) content of the dialog is displayed in the center of the
  222. /// dialog in a lighter font.
  223. ///
  224. /// Typically this is a [SingleChildScrollView] that contains the dialog's
  225. /// message. As noted in the [AlertDialog] documentation, it's important
  226. /// to use a [SingleChildScrollView] if there's any risk that the content
  227. /// will not fit.
  228. final Widget content;
  229. /// Padding around the content.
  230. ///
  231. /// If there is no content, no padding will be provided. Otherwise, padding of
  232. /// 20 pixels is provided above the content to separate the content from the
  233. /// title, and padding of 24 pixels is provided on the left, right, and bottom
  234. /// to separate the content from the other edges of the dialog.
  235. final EdgeInsetsGeometry contentPadding;
  236. /// Style for the text in the [content] of this [AlertDialog].
  237. ///
  238. /// If null, [DialogTheme.contentTextStyle] is used, if that's null, defaults
  239. /// to [ThemeData.textTheme.subhead].
  240. final TextStyle contentTextStyle;
  241. /// The (optional) set of actions that are displayed at the bottom of the
  242. /// dialog.
  243. ///
  244. /// Typically this is a list of [FlatButton] widgets.
  245. ///
  246. /// These widgets will be wrapped in a [ButtonBar], which introduces 8 pixels
  247. /// of padding on each side.
  248. ///
  249. /// If the [title] is not null but the [content] _is_ null, then an extra 20
  250. /// pixels of padding is added above the [ButtonBar] to separate the [title]
  251. /// from the [actions].
  252. final List<Widget> actions;
  253. /// {@macro flutter.material.dialog.backgroundColor}
  254. final Color backgroundColor;
  255. /// {@macro flutter.material.dialog.elevation}
  256. /// {@macro flutter.material.material.elevation}
  257. final double elevation;
  258. /// The semantic label of the dialog used by accessibility frameworks to
  259. /// announce screen transitions when the dialog is opened and closed.
  260. ///
  261. /// If this label is not provided, a semantic label will be inferred from the
  262. /// [title] if it is not null. If there is no title, the label will be taken
  263. /// from [MaterialLocalizations.alertDialogLabel].
  264. ///
  265. /// See also:
  266. ///
  267. /// * [SemanticsConfiguration.isRouteName], for a description of how this
  268. /// value is used.
  269. final String semanticLabel;
  270. /// {@macro flutter.material.dialog.shape}
  271. final ShapeBorder shape;
  272. @override
  273. Widget build(BuildContext context) {
  274. assert(debugCheckHasMaterialLocalizations(context));
  275. final ThemeData theme = Theme.of(context);
  276. final DialogTheme dialogTheme = DialogTheme.of(context);
  277. final List<Widget> children = <Widget>[];
  278. String label = semanticLabel;
  279. if (title != null) {
  280. children.add(Padding(
  281. padding: titlePadding ??
  282. EdgeInsets.fromLTRB(24.0, 24.0, 24.0, content == null ? 20.0 : 0.0),
  283. child: DefaultTextStyle(
  284. style: titleTextStyle ??
  285. dialogTheme.titleTextStyle ??
  286. theme.textTheme.title,
  287. child: Semantics(
  288. child: title,
  289. namesRoute: true,
  290. container: true,
  291. ),
  292. ),
  293. ));
  294. } else {
  295. switch (defaultTargetPlatform) {
  296. case TargetPlatform.iOS:
  297. label = semanticLabel;
  298. break;
  299. case TargetPlatform.android:
  300. case TargetPlatform.fuchsia:
  301. label = semanticLabel ??
  302. MaterialLocalizations.of(context)?.alertDialogLabel;
  303. }
  304. }
  305. if (content != null) {
  306. children.add(Flexible(
  307. child: Padding(
  308. padding: contentPadding,
  309. child: DefaultTextStyle(
  310. style: contentTextStyle ??
  311. dialogTheme.contentTextStyle ??
  312. theme.textTheme.subhead,
  313. child: content,
  314. ),
  315. ),
  316. ));
  317. }
  318. if (actions != null) {
  319. // children.add(ButtonTheme.bar(
  320. // child: ButtonBar(
  321. // children: actions,
  322. // ),
  323. // ));
  324. children.add(ButtonBarTheme(
  325. data: ButtonBarTheme.of(context),
  326. child: ButtonBar(
  327. children: actions,
  328. ),
  329. ));
  330. }
  331. Widget dialogChild = IntrinsicWidth(
  332. child: Column(
  333. mainAxisSize: MainAxisSize.min,
  334. crossAxisAlignment: CrossAxisAlignment.stretch,
  335. children: children,
  336. ),
  337. );
  338. if (label != null)
  339. dialogChild = Semantics(
  340. namesRoute: true,
  341. label: label,
  342. child: dialogChild,
  343. );
  344. return Dialog(
  345. backgroundColor: backgroundColor,
  346. elevation: elevation,
  347. shape: shape,
  348. child: dialogChild,
  349. );
  350. }
  351. }
  352. /// An option used in a [SimpleDialog].
  353. ///
  354. /// A simple dialog offers the user a choice between several options. This
  355. /// widget is commonly used to represent each of the options. If the user
  356. /// selects this option, the widget will call the [onPressed] callback, which
  357. /// typically uses [Navigator.pop] to close the dialog.
  358. ///
  359. /// The padding on a [SimpleDialogOption] is configured to combine with the
  360. /// default [SimpleDialog.contentPadding] so that each option ends up 8 pixels
  361. /// from the other vertically, with 20 pixels of spacing between the dialog's
  362. /// title and the first option, and 24 pixels of spacing between the last option
  363. /// and the bottom of the dialog.
  364. ///
  365. /// {@tool sample}
  366. ///
  367. /// ```dart
  368. /// SimpleDialogOption(
  369. /// onPressed: () { Navigator.pop(context, Department.treasury); },
  370. /// child: const Text('Treasury department'),
  371. /// )
  372. /// ```
  373. /// {@end-tool}
  374. ///
  375. /// See also:
  376. ///
  377. /// * [SimpleDialog], for a dialog in which to use this widget.
  378. /// * [showDialog], which actually displays the dialog and returns its result.
  379. /// * [FlatButton], which are commonly used as actions in other kinds of
  380. /// dialogs, such as [AlertDialog]s.
  381. /// * <https://material.io/design/components/dialogs.html#simple-dialog>
  382. class SimpleDialogOption extends StatelessWidget {
  383. /// Creates an option for a [SimpleDialog].
  384. const SimpleDialogOption({
  385. Key key,
  386. this.onPressed,
  387. this.child,
  388. }) : super(key: key);
  389. /// The callback that is called when this option is selected.
  390. ///
  391. /// If this is set to null, the option cannot be selected.
  392. ///
  393. /// When used in a [SimpleDialog], this will typically call [Navigator.pop]
  394. /// with a value for [showDialog] to complete its future with.
  395. final VoidCallback onPressed;
  396. /// The widget below this widget in the tree.
  397. ///
  398. /// Typically a [Text] widget.
  399. final Widget child;
  400. @override
  401. Widget build(BuildContext context) {
  402. return InkWell(
  403. onTap: onPressed,
  404. child: Padding(
  405. padding: const EdgeInsets.symmetric(vertical: 8.0, horizontal: 24.0),
  406. child: child,
  407. ),
  408. );
  409. }
  410. }
  411. /// A simple material design dialog.
  412. ///
  413. /// A simple dialog offers the user a choice between several options. A simple
  414. /// dialog has an optional title that is displayed above the choices.
  415. ///
  416. /// Choices are normally represented using [SimpleDialogOption] widgets. If
  417. /// other widgets are used, see [contentPadding] for notes regarding the
  418. /// conventions for obtaining the spacing expected by Material Design.
  419. ///
  420. /// For dialogs that inform the user about a situation, consider using an
  421. /// [AlertDialog].
  422. ///
  423. /// Typically passed as the child widget to [showDialog], which displays the
  424. /// dialog.
  425. ///
  426. /// {@tool sample}
  427. ///
  428. /// In this example, the user is asked to select between two options. These
  429. /// options are represented as an enum. The [showDialog] method here returns
  430. /// a [Future] that completes to a value of that enum. If the user cancels
  431. /// the dialog (e.g. by hitting the back button on Android, or tapping on the
  432. /// mask behind the dialog) then the future completes with the null value.
  433. ///
  434. /// The return value in this example is used as the index for a switch statement.
  435. /// One advantage of using an enum as the return value and then using that to
  436. /// drive a switch statement is that the analyzer will flag any switch statement
  437. /// that doesn't mention every value in the enum.
  438. ///
  439. /// ```dart
  440. /// Future<void> _askedToLead() async {
  441. /// switch (await showDialog<Department>(
  442. /// context: context,
  443. /// builder: (BuildContext context) {
  444. /// return SimpleDialog(
  445. /// title: const Text('Select assignment'),
  446. /// children: <Widget>[
  447. /// SimpleDialogOption(
  448. /// onPressed: () { Navigator.pop(context, Department.treasury); },
  449. /// child: const Text('Treasury department'),
  450. /// ),
  451. /// SimpleDialogOption(
  452. /// onPressed: () { Navigator.pop(context, Department.state); },
  453. /// child: const Text('State department'),
  454. /// ),
  455. /// ],
  456. /// );
  457. /// }
  458. /// )) {
  459. /// case Department.treasury:
  460. /// // Let's go.
  461. /// // ...
  462. /// break;
  463. /// case Department.state:
  464. /// // ...
  465. /// break;
  466. /// }
  467. /// }
  468. /// ```
  469. /// {@end-tool}
  470. ///
  471. /// See also:
  472. ///
  473. /// * [SimpleDialogOption], which are options used in this type of dialog.
  474. /// * [AlertDialog], for dialogs that have a row of buttons below the body.
  475. /// * [Dialog], on which [SimpleDialog] and [AlertDialog] are based.
  476. /// * [showDialog], which actually displays the dialog and returns its result.
  477. /// * <https://material.io/design/components/dialogs.html#simple-dialog>
  478. class SimpleDialog extends StatelessWidget {
  479. /// Creates a simple dialog.
  480. ///
  481. /// Typically used in conjunction with [showDialog].
  482. ///
  483. /// The [titlePadding] and [contentPadding] arguments must not be null.
  484. const SimpleDialog({
  485. Key key,
  486. this.title,
  487. this.titlePadding = const EdgeInsets.fromLTRB(24.0, 24.0, 24.0, 0.0),
  488. this.children,
  489. this.contentPadding = const EdgeInsets.fromLTRB(0.0, 12.0, 0.0, 16.0),
  490. this.backgroundColor,
  491. this.elevation,
  492. this.semanticLabel,
  493. this.shape,
  494. }) : assert(titlePadding != null),
  495. assert(contentPadding != null),
  496. super(key: key);
  497. /// The (optional) title of the dialog is displayed in a large font at the top
  498. /// of the dialog.
  499. ///
  500. /// Typically a [Text] widget.
  501. final Widget title;
  502. /// Padding around the title.
  503. ///
  504. /// If there is no title, no padding will be provided.
  505. ///
  506. /// By default, this provides the recommend Material Design padding of 24
  507. /// pixels around the left, top, and right edges of the title.
  508. ///
  509. /// See [contentPadding] for the conventions regarding padding between the
  510. /// [title] and the [children].
  511. final EdgeInsetsGeometry titlePadding;
  512. /// The (optional) content of the dialog is displayed in a
  513. /// [SingleChildScrollView] underneath the title.
  514. ///
  515. /// Typically a list of [SimpleDialogOption]s.
  516. final List<Widget> children;
  517. /// Padding around the content.
  518. ///
  519. /// By default, this is 12 pixels on the top and 16 pixels on the bottom. This
  520. /// is intended to be combined with children that have 24 pixels of padding on
  521. /// the left and right, and 8 pixels of padding on the top and bottom, so that
  522. /// the content ends up being indented 20 pixels from the title, 24 pixels
  523. /// from the bottom, and 24 pixels from the sides.
  524. ///
  525. /// The [SimpleDialogOption] widget uses such padding.
  526. ///
  527. /// If there is no [title], the [contentPadding] should be adjusted so that
  528. /// the top padding ends up being 24 pixels.
  529. final EdgeInsetsGeometry contentPadding;
  530. /// {@macro flutter.material.dialog.backgroundColor}
  531. final Color backgroundColor;
  532. /// {@macro flutter.material.dialog.elevation}
  533. /// {@macro flutter.material.material.elevation}
  534. final double elevation;
  535. /// The semantic label of the dialog used by accessibility frameworks to
  536. /// announce screen transitions when the dialog is opened and closed.
  537. ///
  538. /// If this label is not provided, a semantic label will be inferred from the
  539. /// [title] if it is not null. If there is no title, the label will be taken
  540. /// from [MaterialLocalizations.dialogLabel].
  541. ///
  542. /// See also:
  543. ///
  544. /// * [SemanticsConfiguration.isRouteName], for a description of how this
  545. /// value is used.
  546. final String semanticLabel;
  547. /// {@macro flutter.material.dialog.shape}
  548. final ShapeBorder shape;
  549. @override
  550. Widget build(BuildContext context) {
  551. assert(debugCheckHasMaterialLocalizations(context));
  552. final List<Widget> body = <Widget>[];
  553. String label = semanticLabel;
  554. if (title != null) {
  555. body.add(Padding(
  556. padding: titlePadding,
  557. child: DefaultTextStyle(
  558. style: Theme.of(context).textTheme.title,
  559. child: Semantics(namesRoute: true, child: title),
  560. ),
  561. ));
  562. } else {
  563. switch (defaultTargetPlatform) {
  564. case TargetPlatform.iOS:
  565. label = semanticLabel;
  566. break;
  567. case TargetPlatform.android:
  568. case TargetPlatform.fuchsia:
  569. label =
  570. semanticLabel ?? MaterialLocalizations.of(context)?.dialogLabel;
  571. }
  572. }
  573. if (children != null) {
  574. body.add(Flexible(
  575. child: SingleChildScrollView(
  576. padding: contentPadding,
  577. child: ListBody(children: children),
  578. ),
  579. ));
  580. }
  581. Widget dialogChild = IntrinsicWidth(
  582. stepWidth: 56.0,
  583. child: ConstrainedBox(
  584. constraints: const BoxConstraints(minWidth: 280.0),
  585. child: Column(
  586. mainAxisSize: MainAxisSize.min,
  587. crossAxisAlignment: CrossAxisAlignment.stretch,
  588. children: body,
  589. ),
  590. ),
  591. );
  592. if (label != null)
  593. dialogChild = Semantics(
  594. namesRoute: true,
  595. label: label,
  596. child: dialogChild,
  597. );
  598. return Dialog(
  599. backgroundColor: backgroundColor,
  600. elevation: elevation,
  601. shape: shape,
  602. child: dialogChild,
  603. );
  604. }
  605. }
  606. Widget _buildMaterialDialogTransitions(
  607. BuildContext context,
  608. Animation<double> animation,
  609. Animation<double> secondaryAnimation,
  610. Widget child) {
  611. return FadeTransition(
  612. opacity: CurvedAnimation(
  613. parent: animation,
  614. curve: Curves.easeOut,
  615. ),
  616. child: child,
  617. );
  618. }
  619. /// Displays a Material dialog above the current contents of the app, with
  620. /// Material entrance and exit animations, modal barrier color, and modal
  621. /// barrier behavior (dialog is dismissible with a tap on the barrier).
  622. ///
  623. /// This function takes a `builder` which typically builds a [Dialog] widget.
  624. /// Content below the dialog is dimmed with a [ModalBarrier]. The widget
  625. /// returned by the `builder` does not share a context with the location that
  626. /// `showDialog` is originally called from. Use a [StatefulBuilder] or a
  627. /// custom [StatefulWidget] if the dialog needs to update dynamically.
  628. ///
  629. /// The `context` argument is used to look up the [Navigator] and [Theme] for
  630. /// the dialog. It is only used when the method is called. Its corresponding
  631. /// widget can be safely removed from the tree before the dialog is closed.
  632. ///
  633. /// The `child` argument is deprecated, and should be replaced with `builder`.
  634. ///
  635. /// Returns a [Future] that resolves to the value (if any) that was passed to
  636. /// [Navigator.pop] when the dialog was closed.
  637. ///
  638. /// The dialog route created by this method is pushed to the root navigator.
  639. /// If the application has multiple [Navigator] objects, it may be necessary to
  640. /// call `Navigator.of(context, rootNavigator: true).pop(result)` to close the
  641. /// dialog rather than just `Navigator.pop(context, result)`.
  642. ///
  643. /// See also:
  644. ///
  645. /// * [AlertDialog], for dialogs that have a row of buttons below a body.
  646. /// * [SimpleDialog], which handles the scrolling of the contents and does
  647. /// not show buttons below its body.
  648. /// * [Dialog], on which [SimpleDialog] and [AlertDialog] are based.
  649. /// * [showCupertinoDialog], which displays an iOS-style dialog.
  650. /// * [showGeneralDialog], which allows for customization of the dialog popup.
  651. /// * <https://material.io/design/components/dialogs.html>
  652. Future<T> showDialog<T>({
  653. @required
  654. BuildContext context,
  655. bool barrierDismissible = true,
  656. @Deprecated(
  657. 'Instead of using the "child" argument, return the child from a closure '
  658. 'provided to the "builder" argument. This will ensure that the BuildContext '
  659. 'is appropriate for widgets built in the dialog.')
  660. Widget child,
  661. WidgetBuilder builder,
  662. }) {
  663. assert(child == null || builder == null);
  664. assert(debugCheckHasMaterialLocalizations(context));
  665. final ThemeData theme = Theme.of(context, shadowThemeOnly: true);
  666. return showGeneralDialog(
  667. context: context,
  668. pageBuilder: (BuildContext buildContext, Animation<double> animation,
  669. Animation<double> secondaryAnimation) {
  670. final Widget pageChild = child ?? Builder(builder: builder);
  671. return WillPopScope(onWillPop: () {
  672. return new Future.value(false);
  673. }, child: SafeArea(
  674. child: Builder(builder: (BuildContext context) {
  675. return theme != null
  676. ? Theme(data: theme, child: pageChild)
  677. : pageChild;
  678. }),
  679. ));
  680. },
  681. barrierDismissible: barrierDismissible,
  682. barrierLabel: MaterialLocalizations.of(context).modalBarrierDismissLabel,
  683. transitionDuration: const Duration(milliseconds: 150),
  684. transitionBuilder: _buildMaterialDialogTransitions,
  685. );
  686. }