Skip to content

Commit 946f31c

Browse files
update
1 parent 966cc92 commit 946f31c

File tree

4 files changed

+741
-0
lines changed

4 files changed

+741
-0
lines changed
Lines changed: 269 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,269 @@
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+
}
Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
import 'package:flutter/material.dart';
2+
import 'package:fluttertoast/fluttertoast.dart';
3+
4+
import 'alert_dialog_demo.dart';
5+
import 'simple_dialog_demo.dart';
6+
import 'snackbar_demo.dart';
7+
8+
class DialogWidgetsDemo extends StatelessWidget {
9+
@override
10+
Widget build(BuildContext context) {
11+
List<Widget> widgets = <Widget>[
12+
buildClicks(buildContents('SnackBar的使用'), context, new SnackBarDemo()),
13+
buildClicks(
14+
buildContents('SimpleDialog的使用'), context, new SimpleDialogDemo()),
15+
buildClicks(
16+
buildContents('AlertDialog的使用'), context, new AlertDialogDemo()),
17+
// buildClicks(buildContents('BottomSheet的使用'), context, new SnackBarDemo()),
18+
// buildClicks(
19+
// buildContents('ExpansionPanel的使用'), context, new SnackBarDemo()),
20+
];
21+
22+
return new Scaffold(
23+
appBar: new AppBar(
24+
leading: new IconButton(
25+
icon: new Icon(Icons.keyboard_arrow_left),
26+
onPressed: () {
27+
// 返回上一个页面
28+
Navigator.of(context).pop();
29+
}),
30+
title: new Text('各种弹窗&提示控件用法'),
31+
),
32+
body: new ListView(children: widgets),
33+
);
34+
}
35+
36+
Widget buildClicks(Widget child, BuildContext context, Widget page) {
37+
return new InkWell(
38+
child: child,
39+
onTapDown: (details) {
40+
print('onTapDown');
41+
Fluttertoast.showToast(
42+
msg: 'onTapDown',
43+
toastLength: Toast.LENGTH_SHORT,
44+
gravity: ToastGravity.BOTTOM);
45+
// 发送路由消息
46+
Navigator.push(context,
47+
new MaterialPageRoute(builder: (BuildContext context) => page));
48+
},
49+
onTap: () {
50+
Fluttertoast.showToast(
51+
msg: 'onTap',
52+
toastLength: Toast.LENGTH_SHORT,
53+
gravity: ToastGravity.BOTTOM);
54+
},
55+
onLongPress: () {
56+
Fluttertoast.showToast(
57+
msg: 'onLongPress',
58+
toastLength: Toast.LENGTH_SHORT,
59+
gravity: ToastGravity.BOTTOM);
60+
},
61+
onDoubleTap: () {
62+
Fluttertoast.showToast(
63+
msg: 'onDoubleTap',
64+
toastLength: Toast.LENGTH_SHORT,
65+
gravity: ToastGravity.BOTTOM);
66+
},
67+
);
68+
}
69+
70+
Widget buildContents(var text) {
71+
return new Container(
72+
margin: new EdgeInsets.all(5.0),
73+
padding: new EdgeInsets.all(5.0),
74+
alignment: Alignment.center,
75+
constraints: new BoxConstraints.expand(height: 40.0),
76+
decoration: new BoxDecoration(
77+
color: Colors.teal[300],
78+
borderRadius: new BorderRadius.all(
79+
//让矩形四个角都变成圆角
80+
const Radius.circular(8.0),
81+
),
82+
// 阴影
83+
boxShadow: <BoxShadow>[
84+
new BoxShadow(
85+
color: Colors.teal[100],
86+
offset: new Offset(0.0, 5.0),
87+
blurRadius: 8.0,
88+
),
89+
new BoxShadow(
90+
color: Colors.grey,
91+
offset: new Offset(0.0, 6.0),
92+
blurRadius: 8.0,
93+
),
94+
],
95+
),
96+
child: buildButton(text),
97+
);
98+
}
99+
100+
Widget buildButton(var text) {
101+
return new Text(
102+
text,
103+
style: new TextStyle(
104+
color: Colors.white,
105+
fontSize: 16.0,
106+
),
107+
);
108+
}
109+
}

0 commit comments

Comments
 (0)