对话框在 Flutter 里也是 Widget,但是使用的方式稍有不同,需要使用显示对话框的方法去显示,这样的方法有两个:

  1. showAboutDialog()
  2. showDialog()

这两个方法的使用还有特殊的要求:

showAboutDialog() 和 showDialog() 只能在 MaterialApp 的子 Widget 里使用,因为这两个方法需要使用 MaterialApp 的 context,所以解决问题的办法就和前一节讲 SnackBar 和 Builder 的使用一样,要么使用 Builder,要么将 Widget 单独拆分出来。

本节就使用 Builder 和 showAboutDialog()、showDialog() 来弹对话框。

showAboutDialog()

showAboutDialog() 是用来弹关于对话框的。

showAboutDialog() 的快速上手

showAboutDialog() 需要使用 Builer,使用方法如下:

  1. Builder(
  2. builder: (context) => RaisedButton(
  3. onPressed: () {
  4. showAboutDialog(
  5. context: context,
  6. applicationName: 'lutter UI Widget -- 对话框',
  7. applicationVersion: '1.0.0');
  8. },
  9. child: Text('RaisedButton'))
  10. )

showAboutDialog() 使用在一个页面的完整 Demo 如下:

  1. import 'package:flutter/material.dart';
  2. void main() => runApp(ShowAboutDialogWidget());
  3. class ShowAboutDialogWidget extends StatelessWidget {
  4. @override
  5. Widget build(BuildContext context) {
  6. return MaterialApp(
  7. title: "Flutter Demo",
  8. theme: ThemeData(
  9. primaryColor: Colors.blue,
  10. ),
  11. home: Scaffold(
  12. appBar: AppBar(title: Text("Flutter UI Widget -- 对话框")),
  13. body: Builder(
  14. builder: (context) => RaisedButton(
  15. onPressed: () {
  16. showAboutDialog(
  17. context: context,
  18. applicationName: 'Flutter UI Widget -- 对话框',
  19. applicationVersion: '1.0.0');
  20. },
  21. child: Text('RaisedButton')))),
  22. );
  23. }
  24. }

运行效果如下:

Flutter 学习(十五)基础 Widget - 对话框 - 图1

showAboutDialog() 方法的定义及参数说明

先看 showAboutDialog() 方法的定义:

  1. void showAboutDialog({
  2. @required BuildContext context,
  3. String applicationName,
  4. String applicationVersion,
  5. Widget applicationIcon,
  6. String applicationLegalese,
  7. List<Widget> children,
  8. }) {
  9. ...
  10. }

showAboutDialog() 方法的参数:

参数名字 参数类型 意义 必选 or 可选
context BuildContext 应用上下文 必选
applicationName String 应用的名字 可选
applicationVersion String 应用的版本 可选
applicationIcon Widget 应用的 Icon 可选
applicationLegalese String 应用的法律信息 可选
children List< Widget> 添加在后面的 Widget 可选

showDialog()

showDialog() 可以弹很多不同种类的 Dialog。

showDialog() 的方法定义及参数说明

showDialog() 的方法定义为:

  1. Future<T> showDialog<T>({
  2. @required BuildContext context,
  3. bool barrierDismissible = true,
  4. @Deprecated(
  5. 'Instead of using the "child" argument, return the child from a closure '
  6. 'provided to the "builder" argument. This will ensure that the BuildContext '
  7. 'is appropriate for widgets built in the dialog.'
  8. ) Widget child,
  9. WidgetBuilder builder,
  10. }) {
  11. ...
  12. }

showDialog() 方法的参数:

参数名字 参数类型 意义 必选 or 可选
context BuildContext 应用上下文 必选
barrierDismissible bool 点击背景是否可以关闭 dialog
默认为true,点击背景可以关闭 dialog
可选
child Widget 要显示的 Widget,这个已经废弃了,请使用 builder 可选
builder WidgetBuilder 创建要显示的 Widget 可选

showDialog() 的使用

showDialog() 的使用方法为:

  1. Builder(
  2. builder: (context) {
  3. return RaisedButton(
  4. onPressed: () {
  5. showDialog(context: context,builder: (context) => xxxDialog(...));
  6. },
  7. child: Text('showDialog'),
  8. );
  9. },
  10. )

Builder 里创建 RaisedButton,在 RaisedButton 里在调用 showDialog() 方法。
这里的 xxxDialog 就是 Flutter 里的对话框 Widget,在 Flutter 中,对话框 Widget 有3个:

  1. SimpleDialog
  2. AlertDialog
  3. CupertinoAlertDialog

接下来会依次讲每一个 Widget 的使用。

SimpleDialog

SimpleDialog 是有一个标题和多个选项的简单对话框。

SimpleDialog 的快速上手

实现一个 SimpleDialog 的代码为:

  1. SimpleDialog(
  2. title: Text('SimpleDialog Demo'),
  3. children: <Widget>[
  4. SimpleDialogOption(
  5. child: Text('OK'),
  6. onPressed: () {
  7. Navigator.of(context).pop();
  8. },
  9. ),
  10. SimpleDialogOption(
  11. child: Text('CANCEL'),
  12. onPressed: () {
  13. Navigator.of(context).pop();
  14. },
  15. )
  16. ],
  17. )

SimpleDialog 有一个 Text 参数,children 是两个 SimpleDialogOption,SimpleDialogOption 是按钮的描述。
把 SimpleDialog 实现在一个页面的完整 Demo 如下:

  1. import 'package:flutter/material.dart';
  2. main() => runApp(new ShowSimpleDialogWidget());
  3. class ShowSimpleDialogWidget extends StatelessWidget {
  4. @override
  5. Widget build(BuildContext context) {
  6. return new MaterialApp(
  7. title: 'Test',
  8. home: new Scaffold(
  9. appBar: new AppBar(title: new Text('Test')),
  10. body: Builder(
  11. builder: (context) {
  12. return RaisedButton(
  13. onPressed: () {
  14. showDialog(
  15. context: context,
  16. builder: (context) => SimpleDialog(
  17. title: Text('SimpleDialog Demo'),
  18. children: <Widget>[
  19. SimpleDialogOption(
  20. child: Text('OK'),
  21. onPressed: () {
  22. Navigator.of(context).pop();
  23. },
  24. ),
  25. SimpleDialogOption(
  26. child: Text('CANCEL'),
  27. onPressed: () {
  28. Navigator.of(context).pop();
  29. },
  30. )
  31. ],
  32. ));
  33. },
  34. child: Text('showDialog'),
  35. );
  36. },
  37. )),
  38. );
  39. }
  40. }
  • SimpleDialog 的关闭
    使用 Navigator.of(context).pop() 来关闭 SimpleDialog。

SimpleDialog 的 SimpleDialogOption 的 onPressed() 事件,必须要写 Navigator.of(context).pop() ,不然弹窗不会关闭。

运行效果为:

Flutter 学习(十五)基础 Widget - 对话框 - 图2

SimpleDialog 的构造函数及参数说明

SimpleDialog 的构造函数为:

  1. class SimpleDialog extends StatelessWidget {
  2. const SimpleDialog({
  3. Key key,
  4. this.title,
  5. this.titlePadding = const EdgeInsets.fromLTRB(24.0, 24.0, 24.0, 0.0),
  6. this.children,
  7. this.contentPadding = const EdgeInsets.fromLTRB(0.0, 12.0, 0.0, 16.0),
  8. this.backgroundColor,
  9. this.elevation,
  10. this.semanticLabel,
  11. this.shape,
  12. }) : assert(titlePadding != null),
  13. assert(contentPadding != null),
  14. super(key: key);
  15. ...
  16. }
参数名字 参数类型 意义 必选 or 可选
key Key Widget 的标识 可选
title Widget 对话框的标题
通常是 Text
可选
titlePadding EdgeInsetsGeometry 标题的边距 可选
children List< Widget> 对话框的按钮,显示在对话框标题的下面
通常是一组 SimpleDialogOption
可选
contentPadding EdgeInsetsGeometry 内容的边距 可选
backgroundColor Color 对话框的背景 可选
elevation double Button 相对于其父级放置的z坐标,这可以控制 Button 下的阴影大小
该值必须>=0
可选
semanticLabel String 给文本加上一个语义标签,用于盲人辅助模式下 可选
shape ShapeBorder Widget 的形状 可选
  • SimpleDialogOption 的构造函数及参数说明
    SimpleDialogOption 是 SimapleDialog 的选项按钮。 先看 SimpleDialogOption 的构造函数:
  1. class SimpleDialogOption extends StatelessWidget {
  2. const SimpleDialogOption({
  3. Key key,
  4. this.onPressed,
  5. this.child,
  6. }) : super(key: key);
  7. ...
  8. }
参数名字 参数类型 意义 必选 or 可选
key Key Widget 的标识 可选
onPressed VoidCallback 点击事件,当手指松开时才触发 可选
child Widget 显示的内容一般是 Text 可选
  • SimpleDialogOption 的使用
    SimpleDialogOption 的使用的方法如下:
  1. Builder(
  2. builder: (context) {
  3. return RaisedButton(
  4. onPressed: () {
  5. showDialog(
  6. context: context,
  7. builder: (context) => SimpleDialog(
  8. title: Text('SimpleDialog Demo'),
  9. children: <Widget>[
  10. SimpleDialogOption(
  11. child: Text('OK'),
  12. onPressed: () {},
  13. ),
  14. SimpleDialogOption(
  15. child: Text('CANCEL'),
  16. onPressed: () {},
  17. )
  18. ],
  19. ));
  20. },
  21. child: Text('showDialog'),
  22. );
  23. },
  24. )

AlertDialog

AlertDialog 是警报对话框,具有标题和选型,可以让用户选择。

AlertDialog 的快速上手

AlertDialog 使用的代码为:

  1. AlertDialog(
  2. title: Text('AlertDialog'),
  3. content: SingleChildScrollView(
  4. child: ListBody(
  5. children: <Widget>[
  6. Text('This is an alert dialog'),
  7. Text('add two options.'),
  8. ],
  9. ),
  10. ),
  11. actions: <Widget>[
  12. FlatButton(
  13. child: Text('Ok'),
  14. onPressed: () {
  15. Navigator.of(context).pop();
  16. },
  17. ),
  18. FlatButton(
  19. child: Text('Cancel'),
  20. onPressed: () {
  21. Navigator.of(context).pop();
  22. },
  23. )
  24. ],
  25. )

AlertDialog 有一个 title 参数是标题,content 参数是内容,actions 是按钮数组。
AlertDialog 使用在一个页面的完整 Demo 如下:

  1. import 'package:flutter/material.dart';
  2. main() => runApp(new ShowAlertDialogWidget());
  3. class ShowAlertDialogWidget extends StatelessWidget {
  4. @override
  5. Widget build(BuildContext context) {
  6. return new MaterialApp(
  7. title: 'Test',
  8. home: new Scaffold(
  9. appBar: new AppBar(title: new Text('Flutter UI Widget -- 对话框')),
  10. body: Builder(
  11. builder: (context) {
  12. return RaisedButton(
  13. onPressed: () {
  14. showDialog(
  15. context: context,
  16. builder: (context) => AlertDialog(
  17. title: Text('AlertDialog'),
  18. content: SingleChildScrollView(
  19. child: ListBody(
  20. children: <Widget>[
  21. Text('This is an alert dialog'),
  22. Text('add two options.'),
  23. ],
  24. ),
  25. ),
  26. actions: <Widget>[
  27. FlatButton(
  28. child: Text('Ok'),
  29. onPressed: () {
  30. Navigator.of(context).pop();
  31. },
  32. ),
  33. FlatButton(
  34. child: Text('Cancel'),
  35. onPressed: () {
  36. Navigator.of(context).pop();
  37. },
  38. )
  39. ],
  40. ));
  41. },
  42. child: Text('showDialog'),
  43. );
  44. },
  45. )),
  46. );
  47. }
  48. }
  • AlertDialog 的关闭
    使用 Navigator.of(context).pop() 来关闭 AlertDialog。

AlertDialog 的 actions 的 onPressed() 事件,必须要写 Navigator.of(context).pop() ,不然弹窗不会关闭。

运行效果如下:

Flutter 学习(十五)基础 Widget - 对话框 - 图3

AlertDialog 的构造函数及参数说明

AlertDialog 的构造函数为:

  1. class AlertDialog extends StatelessWidget {
  2. const AlertDialog({
  3. Key key,
  4. this.title,
  5. this.titlePadding,
  6. this.titleTextStyle,
  7. this.content,
  8. this.contentPadding = const EdgeInsets.fromLTRB(24.0, 20.0, 24.0, 24.0),
  9. this.contentTextStyle,
  10. this.actions,
  11. this.backgroundColor,
  12. this.elevation,
  13. this.semanticLabel,
  14. this.shape,
  15. }) : assert(contentPadding != null),
  16. super(key: key);
  17. ...
  18. }
参数名字 参数类型 意义 必选 or 可选
key Key Widget 的标识 可选
title Widget 对话框的标题
通常是 Text
可选
titlePadding EdgeInsetsGeometry 标题的边距 可选
titleTextStyle TextStyle 标题的文本格式 可选
content Widget 对话框的内容 可选
contentPadding EdgeInsetsGeometry 内容的边距 可选
contentTextStyle TextStyle 内容的文本格式 可选
actions List< Widget> 对话框的选型按钮
通常是一组 FlatButton
可选
backgroundColor Color 对话框的背景 可选
elevation double Button 相对于其父级放置的z坐标,这可以控制 Button 下的阴影大小
该值必须>=0
可选
semanticLabel String 给文本加上一个语义标签,用于盲人辅助模式下 可选
shape ShapeBorder Widget 的形状 可选

CupertinoAlertDialog

CupertinoAlertDialog 是 iOS 风格的 AlertDialog。

CupertinoAlertDialog 的使用

CupertinoAlertDialog 使用的代码为:

  1. CupertinoAlertDialog(title: Text('CupertinoAlertDialog'),
  2. content: Text('This is a CupertinoAlertDialog'),
  3. actions: <Widget>[
  4. CupertinoDialogAction(
  5. child: Text('Ok'),
  6. onPressed: () {
  7. Navigator.of(context).pop();
  8. },
  9. ),
  10. CupertinoDialogAction(
  11. child: Text('Cancel'),
  12. onPressed: () {
  13. Navigator.of(context).pop();
  14. },
  15. )
  16. ],
  17. )

CupertinoAlertDialog 使用的完整 Demo 为:

  1. import 'package:flutter/cupertino.dart';
  2. import 'package:flutter/material.dart';
  3. main() => runApp(new ShowCupertinoAlertDialogWidget());
  4. class ShowCupertinoAlertDialogWidget extends StatelessWidget {
  5. @override
  6. Widget build(BuildContext context) {
  7. return new MaterialApp(
  8. title: 'Test',
  9. home: new Scaffold(
  10. appBar: new AppBar(title: new Text('Flutter UI Widget -- 对话框')),
  11. body: Builder(
  12. builder: (context) {
  13. return RaisedButton(
  14. onPressed: () {
  15. showDialog(
  16. context: context,
  17. builder: (context) => CupertinoAlertDialog(
  18. title: Text('CupertinoAlertDialog'),
  19. content: Text('This is a CupertinoAlertDialog'),
  20. actions: <Widget>[
  21. CupertinoDialogAction(
  22. child: Text('Ok'),
  23. onPressed: () {
  24. Navigator.of(context).pop();
  25. },
  26. ),
  27. CupertinoDialogAction(
  28. child: Text('Cancel'),
  29. onPressed: () {
  30. Navigator.of(context).pop();
  31. },
  32. )
  33. ],
  34. ));
  35. },
  36. child: Text('showDialog'),
  37. );
  38. },
  39. )),
  40. );
  41. }
  42. }
  • CupertinoAlertDialog 的关闭
    使用 Navigator.of(context).pop() 来关闭 CupertinoAlertDialog。

CupertinoAlertDialog 的 CupertinoDialogAction 的 onPressed() 事件,必须要写 Navigator.of(context).pop() ,不然弹窗不会关闭。

运行效果为:
Flutter 学习(十五)基础 Widget - 对话框 - 图4

CupertinoAlertDialog 的构造函数及参数说明

CupertinoAlertDialog 的构造函数为:

  1. class CupertinoAlertDialog extends StatelessWidget {
  2. const CupertinoAlertDialog({
  3. Key key,
  4. this.title,
  5. this.content,
  6. this.actions = const <Widget>[],
  7. this.scrollController,
  8. this.actionScrollController,
  9. }) : assert(actions != null),
  10. super(key: key);
  11. ...
  12. }
参数名字 参数类型 意义 必选 or 可选
key Key Widget 的标识 可选
title Widget 对话框的标题
通常是 Text
可选
content Widget 对话框的内容
通常是 Text
可选
actions List< Widget> 对话框的选型按钮
通常是一组 CupertinoDialogAction
可选
scrollController ScrollController 可用于控制对话框中内容的滚动控制器 可选
actionScrollController ScrollController 可用于控制对话框中 actions 的滚动控制器 可选

参考

【1】Flutter 实战
【2】Flutter 中文文档
【3】Flutter 完全手册