光棍节就得写个bug庆祝下!!!

写个自定义的Dialog,可以随意控制UI,同时封装一下,传值控制UI,开个方法回调,控制交互.

先上效果图!!!

dialog封装.gif

再上代码

CustomDialog.dart

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
/*
* 输入框
* */

import 'package:flutter/material.dart';

class CustomDialog extends StatefulWidget {
Function dismissCallback; // 取消回调
Function okCallback; // 确认回调
bool isNeedTag; // 外部传入的参数,控制显示slide还是input

CustomDialog({
Key key,
this.dismissCallback,
this.okCallback,
this.isNeedTag,
}) : super(key: key);
@override
_CustomDialogState createState() => _CustomDialogState();
}

class _CustomDialogState extends State<CustomDialog> {
String commitStr;
double slideValue = 0.0;

/*
* 取消按钮点击事件
* */
_dismissDialog() {
if (widget.dismissCallback != null) {
widget.dismissCallback();
}
Navigator.of(context).pop();
}

/*
* 确定按钮点击事件
* */
_okCallback(String txt) {
if (widget.okCallback != null) {
widget.okCallback(txt);
}
Navigator.of(context).pop();
}

@override
Widget build(BuildContext context) {
return Material(
type: MaterialType.transparency,
child: Center(
child: SizedBox(
width: 240,
height: 240,
child: Container(
decoration: BoxDecoration(
color: Colors.cyanAccent,
borderRadius: BorderRadius.vertical(
top: Radius.circular(120.0),
),
),
child: Column(
children: <Widget>[
widget.isNeedTag == true ? _topWidget() : _topWidget1(),
Spacer(),
_bottomWidget(),
],
),
),
),
),
);
}

/*
* 上方输入UI
* */
Widget _topWidget() {
return Container(
margin: EdgeInsets.only(top: 100),
child: Container(
child: TextField(
onSubmitted: (e) {
setState(() {
commitStr = e;
});
},
decoration: InputDecoration(
border: InputBorder.none,
hintText: "请输入字符",
),
),
),
);
}

/*
* 上方输入UI-1
* */
Widget _topWidget1() {
return Container(
margin: EdgeInsets.only(top: 100),
child: Slider(
value: slideValue,
max: 100.0,
min: 0.0,
activeColor: Colors.blue,
onChanged: (double val) {
setState(() {
slideValue = val;
commitStr = val.toString();
});
},
),
);
}

/*
* 底部选择widget
* */
Widget _bottomWidget() {
return Row(
mainAxisAlignment: MainAxisAlignment.spaceAround,
children: <Widget>[
InkWell(
onTap: () {
_okCallback(commitStr);
},
child: Container(
height: 50,
width: 120,
color: Colors.yellow[800],
child: Center(
child: Text(
'确定',
style: TextStyle(color: Colors.white),
),
),
),
),
InkWell(
onTap: () {
_dismissDialog();
},
child: Container(
height: 50,
width: 120,
color: Colors.red[800],
child: Center(
child: Text(
'取消',
style: TextStyle(color: Colors.white),
),
),
),
),
],
);
}
}

使用如下

main.dart

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
import 'package:flutter/material.dart';
import './custom_dialog.dart';

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: MyHomePage(),
);
}
}

class MyHomePage extends StatefulWidget {
@override
_MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('自定义dialog'),
),
body: ListView(
children: <Widget>[
InkWell(
onTap: () {
showDialog(
context: context,
builder: (BuildContext context) {
return CustomDialog(
isNeedTag: true,
okCallback: (text) {
print(text);
},
dismissCallback: () {
print("input 取消了");
},
);
},
);
},
child: Text('第一个'),
),
SizedBox(height: 20),
InkWell(
onTap: () {
showDialog(
context: context,
builder: (BuildContext context) {
return CustomDialog(
isNeedTag: false,
okCallback: (text) {
print(text);
},
dismissCallback: () {
print("slide 取消了");
},
);
},
);
},
child: Text('第二个'),
)
],
),
);
}
}

最后再讲点废话:

isNeedTag 就是用来给外部用来控制内部UI的值,这个简单控制下内部是显示输入框还是滑块
Function dismissCallbackFunction okCallback 是Dialog内部行为传给外部的方法,控制交互.

ok.光棍节庆祝完毕.下班~~~

源码