Skip to content

Commit 05281d8

Browse files
Update simple_dialog_demo.dart
1 parent e6aa0d0 commit 05281d8

File tree

1 file changed

+133
-159
lines changed

1 file changed

+133
-159
lines changed

projects/flutter-demo/lib/all_samples/dialog/simple_dialog_demo.dart

Lines changed: 133 additions & 159 deletions
Original file line numberDiff line numberDiff line change
@@ -18,49 +18,32 @@ class _SimpleDialogDemoState extends State<SimpleDialogDemo> {
1818
),
1919
body: new ListView(
2020
children: <Widget>[
21-
setBuilders(context, 'SimpleDialogDemo1', 1, 'SimpleDialogDemo1'),
22-
setBuilders(context, 'SimpleDialogDemo2', 2, 'SimpleDialogDemo2'),
23-
setBuilders(context, 'SimpleDialogDemo3', 3, 'SimpleDialogDemo3'),
21+
buildContents(context, 'SimpleDialog Demo1', 'SimpleDialog Demo1', 1),
22+
buildContents(context, 'SimpleDialog Demo2', 'SimpleDialog Demo2', 2),
23+
buildContents(context, 'SimpleDialog Demo3', 'SimpleDialog Demo3', 3),
2424
],
2525
),
2626
);
2727
}
2828
}
2929

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) {
30+
// 主体内容
31+
Widget buildContents(
32+
BuildContext context, String contents, String title, int type) {
4233
return new InkWell(
43-
child: child,
44-
// onTapDown: (details) {
45-
// Fluttertoast.showToast(
46-
// msg: '点击了 $clickMsg',
47-
// toastLength: Toast.LENGTH_SHORT,
48-
// gravity: ToastGravity.BOTTOM);
49-
// // 创建SimpleDialog
50-
// buildDialogs(context, type);
51-
// },
34+
// 给每一个item一个点击事件
35+
child: _buildListItemContent(contents: contents),
5236
onTap: () {
53-
Fluttertoast.showToast(
54-
msg: '点击了 $clickMsg',
55-
toastLength: Toast.LENGTH_SHORT,
56-
gravity: ToastGravity.CENTER);
57-
// 创建SimpleDialog
58-
buildDialogs(context, type);
37+
print("onTap");
38+
_showToast('onTap');
39+
// 点击了item 就会打开 SimpleDialog
40+
buildDialogs(context, title, type);
5941
},
6042
);
6143
}
6244

63-
Widget buildContents({var contents, Color bgColor}) {
45+
// ListView的 Item布局内容
46+
Widget _buildListItemContent({var contents, Color bgColor}) {
6447
return new Container(
6548
margin: new EdgeInsets.all(5.0),
6649
padding: new EdgeInsets.all(5.0),
@@ -86,11 +69,11 @@ Widget buildContents({var contents, Color bgColor}) {
8669
),
8770
],
8871
),
89-
child: buildButton(contents),
72+
child: _buildButton(contents),
9073
);
9174
}
9275

93-
Widget buildButton(var contents) {
76+
Widget _buildButton(var contents) {
9477
return new Text(
9578
contents,
9679
style: new TextStyle(
@@ -100,153 +83,144 @@ Widget buildButton(var contents) {
10083
);
10184
}
10285

103-
// 创建SimpleDialog
86+
// 创建 SimpleDialog
10487
// 这个不能直接写,正确的使用方式:showDialog<Null>(builder: (BuildContext context) {});
10588
// 在builder里面去声明dialog对象
106-
// 这里封装的type是类型,如果是第一种 就显示默认的风格
107-
void buildDialogs(BuildContext context, int type) {
89+
void buildDialogs(BuildContext context, String title, int type) {
10890
showDialog<Null>(
10991
context: context,
11092
builder: (BuildContext context) {
111-
if (type == 1) {
112-
return _showSimpleDialog1(context, type);
113-
} else if (type == 2) {
114-
return _showSimpleDialog2(context, type);
115-
} else if (type == 3) {
116-
return _showSimpleDialog3(context, type);
117-
}
93+
return showSimpleDialog(context, title, type);
11894
},
11995
);
12096
}
12197

122-
SimpleDialog _showSimpleDialog1(BuildContext context, int type) {
123-
SimpleDialog simpleDialog = new SimpleDialog(
124-
// 标题内容
125-
title: new Text('SimpleDialog示例1'),
126-
// children里面是dialog的内容 可以自定义
127-
children: <Widget>[
128-
buildClicks(new Text('item1'), context, type, 'item1'),
129-
buildClicks(new Text('item2'), context, type, 'item2'),
130-
buildClicks(new Text('item3'), context, type, 'item3'),
131-
buildClicks(new Text('item4'), context, type, 'item4'),
132-
buildClicks(new Text('item5'), context, type, 'item5'),
133-
],
98+
// 创建 SimpleDialog
99+
// title是SimpleDialog的标题 type是dialog item创建不同的内容
100+
SimpleDialog showSimpleDialog(BuildContext context, String title, int type) {
101+
Text text = new Text(
102+
title,
103+
style: type == 1
104+
? (new TextStyle(color: Theme.of(context).primaryColor))
105+
: (new TextStyle(color: Colors.deepOrange)),
134106
);
135-
return simpleDialog;
136-
}
137-
138-
SimpleDialog _showSimpleDialog2(BuildContext context, int type) {
139107
SimpleDialog simpleDialog = new SimpleDialog(
140-
title: new Text(
141-
'SimpleDialog示例2',
142-
// 标题颜色使用主题色
143-
style: new TextStyle(color: Theme.of(context).primaryColor),
144-
),
145-
// titlePadding: EdgeInsets.fromLTRB(20.0, 20.0, 20.0, 20.0),
146-
// contentPadding: EdgeInsets.fromLTRB(5.0, 5.0, 5.0, 5.0),
147-
// 路由的名字 当点击dialog和其他页面交互的时候,就可以使用
148-
// semanticLabel: '/routers_name',
149-
// children里面是dialog的内容 可以自定义
150-
children: <Widget>[
151-
buildClicks(buildContents(contents: 'item1'), context, type, 'item1'),
152-
buildClicks(buildContents(contents: 'item2'), context, type, 'item2'),
153-
buildClicks(buildContents(contents: 'item3'), context, type, 'item3'),
154-
buildClicks(buildContents(contents: 'item4'), context, type, 'item4'),
155-
buildClicks(buildContents(contents: 'item5'), context, type, 'item5'),
156-
],
108+
title: text,
109+
children: buildDialogItems(type, context),
157110
);
158111
return simpleDialog;
159112
}
160113

161-
SimpleDialog _showSimpleDialog3(BuildContext context, int type) {
162-
SimpleDialog simpleDialog = new SimpleDialog(
163-
title: new Text(
164-
'SimpleDialog示例3',
165-
style: new TextStyle(color: Colors.deepOrange),
166-
),
167-
titlePadding: EdgeInsets.fromLTRB(20.0, 20.0, 20.0, 20.0),
168-
contentPadding: EdgeInsets.fromLTRB(5.0, 5.0, 5.0, 5.0),
169-
// 路由的名字 当点击dialog和其他页面交互的时候,就可以使用
170-
// semanticLabel: /routers_name',
171-
// children里面是dialog的内容 可以自定义
172-
children: <Widget>[
173-
buildClicks(
174-
new ListTile(
175-
leading: new Icon(
176-
Icons.photo_album,
177-
color: Colors.redAccent,
178-
),
179-
title: new Text(
180-
'相册',
181-
style: new TextStyle(color: Colors.deepOrangeAccent),
182-
),
114+
// 3种不同的内容,所以这里定义了3种类型
115+
List<Widget> buildDialogItems(int type, BuildContext context) {
116+
List<Widget> list = null;
117+
// 第一种类型 简单的ListView
118+
if (type == 1) {
119+
list = <Widget>[
120+
showDialogItemsClick(new Text('item1'), 'item1', context),
121+
showDialogItemsClick(new Text('item2'), 'item2', context),
122+
showDialogItemsClick(new Text('item3'), 'item3', context),
123+
showDialogItemsClick(new Text('item4'), 'item4', context),
124+
showDialogItemsClick(new Text('item5'), 'item5', context),
125+
];
126+
} else if (type == 2) {
127+
list = <Widget>[
128+
showDialogItemsClick(
129+
_buildListItemContent(contents: 'item1'), 'item1', context),
130+
showDialogItemsClick(
131+
_buildListItemContent(contents: 'item2'), 'item2', context),
132+
showDialogItemsClick(
133+
_buildListItemContent(contents: 'item3'), 'item3', context),
134+
showDialogItemsClick(
135+
_buildListItemContent(contents: 'item4'), 'item4', context),
136+
showDialogItemsClick(
137+
_buildListItemContent(contents: 'item5'), 'item5', context),
138+
];
139+
} else if (type == 3) {
140+
list = <Widget>[
141+
showDialogItemsClick(
142+
new ListTile(
143+
leading: new Icon(
144+
Icons.photo_album,
145+
color: Colors.redAccent,
183146
),
184-
context,
185-
type,
186-
'相册'),
187-
buildClicks(
188-
new ListTile(
189-
leading: new Icon(
190-
Icons.add,
191-
color: Colors.redAccent,
192-
),
193-
title: new Text(
194-
'添加',
195-
style: new TextStyle(color: Colors.deepOrangeAccent),
196-
),
147+
title: new Text(
148+
'相册',
149+
style: new TextStyle(color: Colors.deepOrangeAccent),
197150
),
198-
context,
199-
type,
200-
'添加'),
201-
buildClicks(
202-
new ListTile(
203-
leading: new Icon(
204-
Icons.mic,
205-
color: Colors.redAccent,
206-
),
207-
title: new Text(
208-
'录音',
209-
style: new TextStyle(color: Colors.deepOrangeAccent),
210-
),
151+
),
152+
'相册',
153+
context),
154+
showDialogItemsClick(
155+
new ListTile(
156+
leading: new Icon(
157+
Icons.add,
158+
color: Colors.redAccent,
211159
),
212-
context,
213-
type,
214-
'录音'),
215-
buildClicks(
216-
new ListTile(
217-
leading: new Icon(
218-
Icons.mail,
219-
color: Colors.redAccent,
220-
),
221-
title: new Text(
222-
'邮件',
223-
style: new TextStyle(color: Colors.deepOrangeAccent),
224-
),
160+
title: new Text(
161+
'添加',
162+
style: new TextStyle(color: Colors.deepOrangeAccent),
225163
),
226-
context,
227-
type,
228-
'邮件'),
229-
buildClicks(
230-
new ListTile(
231-
leading: new Icon(
232-
Icons.search,
233-
color: Colors.redAccent,
234-
),
235-
title: new Text(
236-
'搜索',
237-
style: new TextStyle(color: Colors.deepOrangeAccent),
238-
),
164+
),
165+
'添加',
166+
context),
167+
showDialogItemsClick(
168+
new ListTile(
169+
leading: new Icon(
170+
Icons.mic,
171+
color: Colors.redAccent,
239172
),
240-
context,
241-
type,
242-
'搜索'),
243-
]);
244-
return simpleDialog;
173+
title: new Text(
174+
'录音',
175+
style: new TextStyle(color: Colors.deepOrangeAccent),
176+
),
177+
),
178+
'录音',
179+
context),
180+
showDialogItemsClick(
181+
new ListTile(
182+
leading: new Icon(
183+
Icons.mail,
184+
color: Colors.redAccent,
185+
),
186+
title: new Text(
187+
'邮件',
188+
style: new TextStyle(color: Colors.deepOrangeAccent),
189+
),
190+
),
191+
'邮件',
192+
context),
193+
showDialogItemsClick(
194+
new ListTile(
195+
leading: new Icon(
196+
Icons.search,
197+
color: Colors.redAccent,
198+
),
199+
title: new Text(
200+
'搜索',
201+
style: new TextStyle(color: Colors.deepOrangeAccent),
202+
),
203+
),
204+
'搜索',
205+
context),
206+
];
207+
}
208+
return list;
209+
}
210+
211+
// 点击弹窗里面的每一个item时的事件
212+
// 参数1:要点击的item 参数2:toast的内容
213+
Widget showDialogItemsClick(Widget child, String msg, BuildContext context) {
214+
return new InkWell(
215+
child: child,
216+
onTap: () {
217+
Navigator.pop(context);
218+
_showToast("关闭了Dialog" + msg);
219+
},
220+
);
245221
}
246222

247-
void showToast(String msg) {
223+
void _showToast(String msg) {
248224
Fluttertoast.showToast(
249-
msg: '点击了 $msg',
250-
toastLength: Toast.LENGTH_SHORT,
251-
gravity: ToastGravity.BOTTOM);
225+
msg: msg, toastLength: Toast.LENGTH_SHORT, gravity: ToastGravity.BOTTOM);
252226
}

0 commit comments

Comments
 (0)