Flutter Widget之Dialog

Widget:https://flutter.io/docs/development/ui/widgets
SimpleDialog:https://docs.flutter.io/flutter/material/SimpleDialog-class.html
AlertDialog:https://docs.flutter.io/flutter/material/AlertDialog-class.html
BottomSheet:https://docs.flutter.io/flutter/material/BottomSheet-class.html

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
import 'package:flutter/material.dart';

class DialogDemoPage extends StatefulWidget {
@override
State<StatefulWidget> createState() {
return _DialogDemoPageState();
}
}

class _DialogDemoPageState extends State<DialogDemoPage> {
@override
Widget build(BuildContext context) {
return new Scaffold(
appBar: AppBar(
title: Text('Dialog Demo'),
),
floatingActionButton: new Builder(builder: (BuildContext context) {
return new FloatingActionButton(
onPressed: () {
// DialogBuilder.showSimpleDialog(context);
// DialogBuilder.showAlertDialog(context);
// DialogBuilder.showAboutDialog(context);
DialogBuilder.showCustomBottomSheet(context);
// DialogBuilder.showCustomModalBottomSheet(context);
// DialogBuilder.showSimpleDialogAsync(context);
// DialogBuilder.showAlertDialogAsync(context);
},
child: new Icon(Icons.add),
);
}));
}
}

class DialogBuilder {
static void showSimpleDialog(BuildContext context) {
showDialog(
context: context,
child: new SimpleDialog(
contentPadding: EdgeInsets.all(10.0),
title: new Text('我是标题'),
children: <Widget>[
SimpleDialogOption(
onPressed: () {
Navigator.of(context).pop();
print('AppBlog.CN');
},
child: const Text('AppBlog.CN'),
),
SimpleDialogOption(
onPressed: () {
Navigator.of(context).pop();
print('Android');
},
child: const Text('Android'),
),
SimpleDialogOption(
onPressed: () {
Navigator.of(context).pop();
print('iOS');
},
child: const Text('iOS'),
),
],
)
);
}

static void showAlertDialog(BuildContext context) {
showDialog(
context: context,
child: new AlertDialog(
contentPadding: EdgeInsets.all(10.0),
title: new Text('我是标题'),
content: new Text('我是内容'),
actions: <Widget>[
FlatButton(
child: Text('确定'),
onPressed: () {
Navigator.of(context).pop();
},
),
FlatButton(
child: Text('取消'),
onPressed: () {
Navigator.of(context).pop();
},
),
],
)
);
}

static void showAboutDialog(BuildContext context) {
showDialog(
context: context,
child: new AboutDialog(
applicationName: '名称',
applicationIcon: new Icon(Icons.ac_unit),
applicationVersion: 'V1.0',
children: <Widget>[
new Text("http://www.appblog.cn")
],
)
);
}

static void showCustomBottomSheet(BuildContext context) {
showBottomSheet(
context: context,
builder: (BuildContext context) {
return Container(
margin: const EdgeInsets.all(5.0),
color: Colors.teal.shade400,
width: double.infinity,
height: 220.0,
child: Column(
crossAxisAlignment: CrossAxisAlignment.center,
children: <Widget>[
Padding(
padding: const EdgeInsets.only(left: 0, top: 16, right: 0, bottom: 4),
child: new Text("杭州", style: TextStyle(color: Colors.white),),
),
Divider(),
Padding(
padding: const EdgeInsets.all(4.0),
child: new Text("上海", style: TextStyle(color: Colors.white),),
),
Divider(),
Padding(
padding: const EdgeInsets.all(4.0),
child: new Text("北京", style: TextStyle(color: Colors.white),),
),
Divider(),
Padding(
padding: const EdgeInsets.all(4.0),
child: new Text("广州", style: TextStyle(color: Colors.white),),
),
Divider(),
Padding(
padding: const EdgeInsets.all(4.0),
child: new Text("深圳", style: TextStyle(color: Colors.white),),
),
],
),
);
}
);
}

static void showCustomModalBottomSheet(BuildContext context) {
showModalBottomSheet(
// showBottomSheet(
context: context,
builder: (BuildContext context) {
return new Container(
child: new Padding(
padding: const EdgeInsets.all(10.0),
child: new Column(
children: <Widget>[
new ListTile(
leading: new Icon(Icons.chat),
title: new Text("开始会话"),
),
new ListTile(
leading: new Icon(Icons.help),
title: new Text("操作说明"),
),
new ListTile(
leading: new Icon(Icons.settings),
title: new Text("系统设置"),
),
new ListTile(
leading: new Icon(Icons.more),
title: new Text("更多设置"),
),
],
)
)
);
}
);
}

static Future<void> showSimpleDialogAsync(BuildContext context) async {
switch (await showDialog<Department>(
context: context,
builder: (BuildContext context) {
return SimpleDialog(
title: const Text('Select assignment'),
children: <Widget>[
SimpleDialogOption(
onPressed: () { Navigator.pop(context, Department.treasury); },
child: const Text('Treasury department'),
),
SimpleDialogOption(
onPressed: () { Navigator.pop(context, Department.state); },
child: const Text('State department'),
),
],
);
}
)) {
case Department.treasury:
print('Department.treasury');
break;
case Department.state:
print('Department.state');
break;
}
}

static Future<void> showAlertDialogAsync(BuildContext context) async {
return showDialog<void>(
context: context,
barrierDismissible: false, // user must tap button!
builder: (BuildContext context) {
return AlertDialog(
title: Text('Rewind and remember'),
content: SingleChildScrollView(
child: ListBody(
children: <Widget>[
Text('You will never be satisfied.'),
Text('You\' re like me. I\' m never satisfied.'),
],
),
),
actions: <Widget>[
FlatButton(
child: Text('Regret'),
onPressed: () {
Navigator.of(context).pop();
},
),
],
);
},
);
}
}

enum Department {
treasury,
state
}

Flutter Widget SimpleDialog

Flutter Widget AlertDialog

Flutter Widget BottomSheet

Flutter Widget ModalBottomSheet

Powered by AppBlog.CN     浙ICP备14037229号

Copyright © 2012 - 2020 APP开发技术博客 All Rights Reserved.

访客数 : | 访问量 :