Skip to content

Commit e6aa0d0

Browse files
Update alert_dialog_demo.dart
1 parent 181e5ec commit e6aa0d0

File tree

1 file changed

+102
-250
lines changed

1 file changed

+102
-250
lines changed
Lines changed: 102 additions & 250 deletions
Original file line numberDiff line numberDiff line change
@@ -1,269 +1,121 @@
11
import 'package:flutter/material.dart';
22
import 'package:fluttertoast/fluttertoast.dart';
33

4-
class AlertDialogDemo extends StatefulWidget {
5-
@override
6-
_AlertDialogDemoState createState() => new _AlertDialogDemoState();
4+
import 'alert_dialog_demo.dart';
5+
import 'simple_dialog_demo.dart';
6+
import 'snackbar_demo.dart';
7+
8+
void main() {
9+
runApp(
10+
new MaterialApp(
11+
title: 'AlertDialogDemo',
12+
theme: new ThemeData(
13+
primarySwatch: Colors.blue,
14+
),
15+
home: new DialogWidgetsDemo(),
16+
),
17+
);
718
}
819

9-
class _AlertDialogDemoState extends State<AlertDialogDemo> {
20+
class DialogWidgetsDemo extends StatelessWidget {
1021
@override
1122
Widget build(BuildContext context) {
23+
List<Widget> widgets = <Widget>[
24+
buildClicks(buildContents('SnackBar的使用'), context, new SnackBarDemo()),
25+
buildClicks(
26+
buildContents('SimpleDialog的使用'), context, new SimpleDialogDemo()),
27+
buildClicks(
28+
buildContents('AlertDialog的使用'), context, new AlertDialogDemo()),
29+
// buildClicks(buildContents('BottomSheet的使用'), context, new SnackBarDemo()),
30+
// buildClicks(
31+
// buildContents('ExpansionPanel的使用'), context, new SnackBarDemo()),
32+
];
33+
1234
return new Scaffold(
1335
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-
],
36+
leading: new IconButton(
37+
icon: new Icon(Icons.keyboard_arrow_left),
38+
onPressed: () {
39+
// 返回上一个页面
40+
Navigator.of(context).pop();
41+
}),
42+
title: new Text('各种弹窗&提示控件用法'),
2543
),
44+
body: new ListView(children: widgets),
2645
);
2746
}
28-
}
2947

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-
}
48+
Widget buildClicks(Widget child, BuildContext context, Widget page) {
49+
return new InkWell(
50+
child: child,
51+
onTapDown: (details) {
52+
print('onTapDown');
53+
Fluttertoast.showToast(
54+
msg: 'onTapDown',
55+
toastLength: Toast.LENGTH_SHORT,
56+
gravity: ToastGravity.BOTTOM);
57+
// 发送路由消息
58+
Navigator.push(context,
59+
new MaterialPageRoute(builder: (BuildContext context) => page));
60+
},
61+
onTap: () {
62+
Fluttertoast.showToast(
63+
msg: 'onTap',
64+
toastLength: Toast.LENGTH_SHORT,
65+
gravity: ToastGravity.BOTTOM);
66+
},
67+
onLongPress: () {
68+
Fluttertoast.showToast(
69+
msg: 'onLongPress',
70+
toastLength: Toast.LENGTH_SHORT,
71+
gravity: ToastGravity.BOTTOM);
72+
},
73+
onDoubleTap: () {
74+
Fluttertoast.showToast(
75+
msg: 'onDoubleTap',
76+
toastLength: Toast.LENGTH_SHORT,
77+
gravity: ToastGravity.BOTTOM);
78+
},
79+
);
80+
}
5481

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,
82+
Widget buildContents(var text) {
83+
return new Container(
84+
margin: new EdgeInsets.all(5.0),
85+
padding: new EdgeInsets.all(5.0),
86+
alignment: Alignment.center,
87+
constraints: new BoxConstraints.expand(height: 40.0),
88+
decoration: new BoxDecoration(
89+
color: Colors.teal[300],
90+
borderRadius: new BorderRadius.all(
91+
//让矩形四个角都变成圆角
92+
const Radius.circular(8.0),
7893
),
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),
94+
// 阴影
95+
boxShadow: <BoxShadow>[
96+
new BoxShadow(
97+
color: Colors.teal[100],
98+
offset: new Offset(0.0, 5.0),
99+
blurRadius: 8.0,
138100
),
139-
context,
140-
type,
141-
'取消'),
142-
buildClicks(
143-
new Text(
144-
'确定',
145-
style: new TextStyle(color: Theme.of(context).primaryColor),
101+
new BoxShadow(
102+
color: Colors.grey,
103+
offset: new Offset(0.0, 6.0),
104+
blurRadius: 8.0,
146105
),
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-
//}
106+
],
107+
),
108+
child: buildButton(text),
109+
);
110+
}
263111

264-
void showToast(String msg) {
265-
Fluttertoast.showToast(
266-
msg: '点击了 $msg',
267-
toastLength: Toast.LENGTH_SHORT,
268-
gravity: ToastGravity.BOTTOM);
112+
Widget buildButton(var text) {
113+
return new Text(
114+
text,
115+
style: new TextStyle(
116+
color: Colors.white,
117+
fontSize: 16.0,
118+
),
119+
);
120+
}
269121
}

0 commit comments

Comments
 (0)