首页 > 建站教程 > APP开发,混合APP >  Flutter笔记22:Dialog、AboutDialog、SimpleDialog、AlertDialog正文

Flutter笔记22:Dialog、AboutDialog、SimpleDialog、AlertDialog

Dialog是普通弹窗组件,可定制性非常高,子元素可以放任何内容,按钮可以任意自定义。Dialog仅仅是一个弹窗。其实Dialog完全可以实现下面三种Dialog,只不过需要布局,比较麻烦。
AboutDialog、SimpleDialog、AlertDialog不是组件,而是showDialog方法的一个参数值。
AboutDialog常常用在软件介绍上。
SimpleDialog有点类似于下拉框弹出的选项效果
AlertDialog有点类似于js的confirm弹出框或者alert弹出框或者prompt弹出框,比较常用
001class DialogDemo extends StatefulWidget {
002  DialogDemo({Key key}) : super(key: key);
003 
004  @override
005  _DialogDemoState createState() => _DialogDemoState();
006}
007 
008class _DialogDemoState extends State<DialogDemo> {
009  //显示 AboutDialog
010  void showAboutDialog(BuildContext context) {
011    showDialog(
012        context: context,
013        builder: (_) => AboutDialog(
014              applicationName: 'Andriod studio',
015              applicationIcon: Icon(Icons.apps),
016              applicationVersion: 'V3.1.1',
017              children: [Text('这是Android Studio')],
018            ));
019  }
020 
021  //显示 SimpleDialog
022  void showSimpleDialog(BuildContext context) {
023    showDialog(
024        context: context,
025        builder: (_) => SimpleDialog(
026              title: Text('选择'),
027              children: [
028                //选项
029                SimpleDialogOption(
030                  child: Text('选项1'),
031                  onPressed: () {
032                    Navigator.of(context).pop();
033                  },
034                ),
035                SimpleDialogOption(
036                  child: Text('选项2'),
037                  onPressed: () {
038                    Navigator.of(context).pop();
039                  },
040                ),
041              ],
042            ));
043  }
044 
045  //显示 AlertDialog
046  void showAlertDialog(BuildContext context) {
047    showDialog<void>(
048        context: context,
049        builder: (_) => AlertDialog(
050              title: Text('AlertDialog'),
051              //内容,可以是任意组件
052              content: SingleChildScrollView(
053                child: ListBody(
054                  children: [
055                    Text('list-item'),
056                    Text('list-item'),
057                    Text('list-item'),
058                    Text('list-item'),
059                    Text('list-item'),
060                    Text('list-item'),
061                    Text('list-item'),
062                    Text('list-item'),
063                    Text('list-item'),
064                    Text('list-item'),
065                    Text('list-item'),
066                    Text('list-item'),
067                    Text('list-item'),
068                    Text('list-item'),
069                  ],
070                ),
071              ),
072              //AlertDialog的动作按钮,可以放N多个按钮
073              actions: [
074                FlatButton(
075                    onPressed: () {
076                      Navigator.of(context).pop();
077                    },
078                    child: Text('取消')),
079              ],
080            ));
081  }
082 
083  @override
084  Widget build(BuildContext context) {
085    //普通的dialog,可定制性非常强
086    // return Center(
087    //   child: Dialog(
088    //     child: Container(
089    //       height: 200.0,
090    //       child: Column(
091    //         mainAxisAlignment: MainAxisAlignment.spaceAround,
092    //         children: [
093    //           Text('这是一个dialog'),
094    //           RaisedButton(
095    //             child: Text('取消'),
096    //             onPressed: () {
097    //               //关闭dialog,相当于关闭页面
098    //               Navigator.of(context).pop();
099    //             }
100    //           ),
101    //         ],
102    //       ),
103    //     ),
104    //   ),
105    // );
106    //AboutDialog、SimpleDialog、AlertDialog
107    return Column(
108      children: [
109        RaisedButton(
110            child: Text('显示AboutDialog'),
111            onPressed: () {
112              showAboutDialog(context);
113            }),
114        RaisedButton(
115            child: Text('显示SimpleDialog'),
116            onPressed: () {
117              showSimpleDialog(context);
118            }),
119        RaisedButton(
120            child: Text('显示AlertDialog'),
121            onPressed: () {
122              showAlertDialog(context);
123            }),
124      ],
125    );
126  }
127}