|
| 1 | +import 'package:flutter/material.dart'; |
| 2 | +import 'package:fluttertoast/fluttertoast.dart'; |
| 3 | + |
| 4 | +class AlertDialogDemo extends StatefulWidget { |
| 5 | + @override |
| 6 | + _AlertDialogDemoState createState() => new _AlertDialogDemoState(); |
| 7 | +} |
| 8 | + |
| 9 | +class _AlertDialogDemoState extends State<AlertDialogDemo> { |
| 10 | + @override |
| 11 | + Widget build(BuildContext context) { |
| 12 | + return new Scaffold( |
| 13 | + appBar: new AppBar( |
| 14 | + title: new Text( |
| 15 | + 'AlertDialogDemo', |
| 16 | + style: new TextStyle(fontSize: 17.0), |
| 17 | + ), |
| 18 | + ), |
| 19 | + body: new ListView( |
| 20 | + children: <Widget>[ |
| 21 | + setBuilders(context, 'AlertDialogDemo', 1, 'AlertDialogDemo1'), |
| 22 | +// setBuilders(context, 'AlertDialogDemo', 2, 'AlertDialogDemo2'), |
| 23 | +// setBuilders(context, 'AlertDialogDemo', 3, 'AlertDialogDemo3'), |
| 24 | + ], |
| 25 | + ), |
| 26 | + ); |
| 27 | + } |
| 28 | +} |
| 29 | + |
| 30 | +Widget setBuilders( |
| 31 | + BuildContext context, String contents, int type, String clickMsg) { |
| 32 | + // new Builder不能去掉 |
| 33 | + return new Builder(builder: (BuildContext context) { |
| 34 | + // 必须加上return 不加就报错 |
| 35 | + return buildClicks( |
| 36 | + buildContents(contents: contents), context, type, clickMsg); |
| 37 | + }); |
| 38 | +} |
| 39 | + |
| 40 | +Widget buildClicks( |
| 41 | + Widget child, BuildContext context, int type, String clickMsg) { |
| 42 | + return new InkWell( |
| 43 | + child: child, |
| 44 | + onTap: () { |
| 45 | + Fluttertoast.showToast( |
| 46 | + msg: '点击了 $clickMsg', |
| 47 | + toastLength: Toast.LENGTH_SHORT, |
| 48 | + gravity: ToastGravity.CENTER); |
| 49 | +// 创建SimpleDialog |
| 50 | + buildDialogs(context, type); |
| 51 | + }, |
| 52 | + ); |
| 53 | +} |
| 54 | + |
| 55 | +Widget buildContents({var contents, Color bgColor}) { |
| 56 | + return new Container( |
| 57 | + margin: new EdgeInsets.all(5.0), |
| 58 | + padding: new EdgeInsets.all(5.0), |
| 59 | + alignment: Alignment.center, |
| 60 | + constraints: new BoxConstraints.expand(height: 40.0), |
| 61 | + decoration: new BoxDecoration( |
| 62 | + color: bgColor == null ? Colors.teal[300] : bgColor, |
| 63 | + borderRadius: new BorderRadius.all( |
| 64 | + //让矩形四个角都变成圆角 |
| 65 | + const Radius.circular(8.0), |
| 66 | + ), |
| 67 | + // 阴影 |
| 68 | + boxShadow: <BoxShadow>[ |
| 69 | + new BoxShadow( |
| 70 | + color: Colors.teal[100], |
| 71 | + offset: new Offset(0.0, 5.0), |
| 72 | + blurRadius: 8.0, |
| 73 | + ), |
| 74 | + new BoxShadow( |
| 75 | + color: Colors.grey, |
| 76 | + offset: new Offset(0.0, 6.0), |
| 77 | + blurRadius: 8.0, |
| 78 | + ), |
| 79 | + ], |
| 80 | + ), |
| 81 | + child: buildButton(contents), |
| 82 | + ); |
| 83 | +} |
| 84 | + |
| 85 | +Widget buildButton(var contents) { |
| 86 | + return new Text( |
| 87 | + contents, |
| 88 | + style: new TextStyle( |
| 89 | + color: Colors.white, |
| 90 | + fontSize: 16.0, |
| 91 | + ), |
| 92 | + ); |
| 93 | +} |
| 94 | + |
| 95 | +// 创建AlertDialog |
| 96 | +// 这个不能直接写,正确的使用方式:showDialog<Null>(builder: (BuildContext context) {}); |
| 97 | +// 在builder里面去声明dialog对象 |
| 98 | +// 这里封装的type是类型,如果是第一种 就显示默认的风格 |
| 99 | +void buildDialogs(BuildContext context, int type) { |
| 100 | + showDialog<Null>( |
| 101 | + context: context, |
| 102 | + builder: (BuildContext context) { |
| 103 | + if (type == 1) { |
| 104 | + return _showAlertDialog1(context, type); |
| 105 | + } |
| 106 | +// else if (type == 2) { |
| 107 | +// return _showAlertDialog2(context, type); |
| 108 | +// } |
| 109 | +// else if (type == 3) { |
| 110 | +// return _showAlertDialog3(context, type); |
| 111 | +// } |
| 112 | + }, |
| 113 | + ); |
| 114 | +} |
| 115 | + |
| 116 | +AlertDialog _showAlertDialog1(BuildContext context, int type) { |
| 117 | + AlertDialog alertDialog = new AlertDialog( |
| 118 | + title: new Text('标题栏'), |
| 119 | +// titlePadding: EdgeInsets.fromLTRB(20.0, 20.0, 20.0, 20.0), |
| 120 | +// contentPadding: EdgeInsets.fromLTRB(5.0, 5.0, 5.0, 5.0), |
| 121 | +// 路由的名字 当点击dialog和其他页面交互的时候,就可以使用 |
| 122 | +// semanticLabel: '/routers_name', |
| 123 | + // AlertDialog的内容 |
| 124 | + content: new Text('正文部分,简单的文本描述'), |
| 125 | + actions: <Widget>[ |
| 126 | + buildClicks( |
| 127 | + new Text( |
| 128 | + '中立', |
| 129 | + style: new TextStyle(color: Colors.pinkAccent), |
| 130 | + ), |
| 131 | + context, |
| 132 | + type, |
| 133 | + '中立'), |
| 134 | + buildClicks( |
| 135 | + new Text( |
| 136 | + '取消', |
| 137 | + style: new TextStyle(color: Theme.of(context).primaryColorDark), |
| 138 | + ), |
| 139 | + context, |
| 140 | + type, |
| 141 | + '取消'), |
| 142 | + buildClicks( |
| 143 | + new Text( |
| 144 | + '确定', |
| 145 | + style: new TextStyle(color: Theme.of(context).primaryColor), |
| 146 | + ), |
| 147 | + context, |
| 148 | + type, |
| 149 | + '确定'), |
| 150 | + ], |
| 151 | + ); |
| 152 | + return alertDialog; |
| 153 | +} |
| 154 | + |
| 155 | +//AlertDialog _showAlertDialog2(BuildContext context, int type) { |
| 156 | +// SimpleDialog simpleDialog = new SimpleDialog( |
| 157 | +// title: new Text( |
| 158 | +// 'AlertDialog示例2', |
| 159 | +// // 标题颜色使用主题色 |
| 160 | +// style: new TextStyle(color: Theme.of(context).primaryColor), |
| 161 | +// ), |
| 162 | +//// titlePadding: EdgeInsets.fromLTRB(20.0, 20.0, 20.0, 20.0), |
| 163 | +//// contentPadding: EdgeInsets.fromLTRB(5.0, 5.0, 5.0, 5.0), |
| 164 | +// // 路由的名字 当点击dialog和其他页面交互的时候,就可以使用 |
| 165 | +//// semanticLabel: /routers_name', |
| 166 | +// // children里面是dialog的内容 可以自定义 |
| 167 | +// children: <Widget>[ |
| 168 | +// buildClicks(buildContents(contents: 'item1'), context, type, 'item1'), |
| 169 | +// buildClicks(buildContents(contents: 'item2'), context, type, 'item2'), |
| 170 | +// buildClicks(buildContents(contents: 'item3'), context, type, 'item3'), |
| 171 | +// buildClicks(buildContents(contents: 'item4'), context, type, 'item4'), |
| 172 | +// buildClicks(buildContents(contents: 'item5'), context, type, 'item5'), |
| 173 | +// ], |
| 174 | +// ); |
| 175 | +// return simpleDialog; |
| 176 | +//} |
| 177 | +// |
| 178 | +//AlertDialog _showAlertDialog3(BuildContext context, int type) { |
| 179 | +// SimpleDialog simpleDialog = new SimpleDialog( |
| 180 | +// title: new Text( |
| 181 | +// 'AlertDialog示例3', |
| 182 | +// style: new TextStyle(color: Colors.deepOrange), |
| 183 | +// ), |
| 184 | +// titlePadding: EdgeInsets.fromLTRB(20.0, 20.0, 20.0, 20.0), |
| 185 | +// contentPadding: EdgeInsets.fromLTRB(5.0, 5.0, 5.0, 5.0), |
| 186 | +// // 路由的名字 当点击dialog和其他页面交互的时候,就可以使用 |
| 187 | +//// semanticLabel: /routers_name', |
| 188 | +// // children里面是dialog的内容 可以自定义 |
| 189 | +// children: <Widget>[ |
| 190 | +// buildClicks( |
| 191 | +// new ListTile( |
| 192 | +// leading: new Icon( |
| 193 | +// Icons.photo_album, |
| 194 | +// color: Colors.redAccent, |
| 195 | +// ), |
| 196 | +// title: new Text( |
| 197 | +// '相册', |
| 198 | +// style: new TextStyle(color: Colors.deepOrangeAccent), |
| 199 | +// ), |
| 200 | +// ), |
| 201 | +// context, |
| 202 | +// type, |
| 203 | +// '相册'), |
| 204 | +// buildClicks( |
| 205 | +// new ListTile( |
| 206 | +// leading: new Icon( |
| 207 | +// Icons.add, |
| 208 | +// color: Colors.redAccent, |
| 209 | +// ), |
| 210 | +// title: new Text( |
| 211 | +// '添加', |
| 212 | +// style: new TextStyle(color: Colors.deepOrangeAccent), |
| 213 | +// ), |
| 214 | +// ), |
| 215 | +// context, |
| 216 | +// type, |
| 217 | +// '添加'), |
| 218 | +// buildClicks( |
| 219 | +// new ListTile( |
| 220 | +// leading: new Icon( |
| 221 | +// Icons.mic, |
| 222 | +// color: Colors.redAccent, |
| 223 | +// ), |
| 224 | +// title: new Text( |
| 225 | +// '录音', |
| 226 | +// style: new TextStyle(color: Colors.deepOrangeAccent), |
| 227 | +// ), |
| 228 | +// ), |
| 229 | +// context, |
| 230 | +// type, |
| 231 | +// '录音'), |
| 232 | +// buildClicks( |
| 233 | +// new ListTile( |
| 234 | +// leading: new Icon( |
| 235 | +// Icons.mail, |
| 236 | +// color: Colors.redAccent, |
| 237 | +// ), |
| 238 | +// title: new Text( |
| 239 | +// '邮件', |
| 240 | +// style: new TextStyle(color: Colors.deepOrangeAccent), |
| 241 | +// ), |
| 242 | +// ), |
| 243 | +// context, |
| 244 | +// type, |
| 245 | +// '邮件'), |
| 246 | +// buildClicks( |
| 247 | +// new ListTile( |
| 248 | +// leading: new Icon( |
| 249 | +// Icons.search, |
| 250 | +// color: Colors.redAccent, |
| 251 | +// ), |
| 252 | +// title: new Text( |
| 253 | +// '搜索', |
| 254 | +// style: new TextStyle(color: Colors.deepOrangeAccent), |
| 255 | +// ), |
| 256 | +// ), |
| 257 | +// context, |
| 258 | +// type, |
| 259 | +// '搜索'), |
| 260 | +// ]); |
| 261 | +// return simpleDialog; |
| 262 | +//} |
| 263 | + |
| 264 | +void showToast(String msg) { |
| 265 | + Fluttertoast.showToast( |
| 266 | + msg: '点击了 $msg', |
| 267 | + toastLength: Toast.LENGTH_SHORT, |
| 268 | + gravity: ToastGravity.BOTTOM); |
| 269 | +} |
0 commit comments