|
@@ -1,9 +1,60 @@
|
1
|
1
|
|
|
2
|
+import 'package:flutter/cupertino.dart';
|
2
|
3
|
import 'package:flutter/material.dart';
|
3
|
4
|
import 'package:flutter/widgets.dart';
|
4
|
5
|
import 'package:flutter_screenutil/flutter_screenutil.dart';
|
|
6
|
+import 'package:get/get.dart';
|
5
|
7
|
|
6
|
|
-class Dialog extends StatelessWidget {
|
|
8
|
+typedef ModalFunc = dynamic Function();
|
|
9
|
+
|
|
10
|
+/// func 函数如果返回 bool 值, 则只有 true 的情况下才默认关闭弹窗
|
|
11
|
+/// 如果返回 Future , 则只有正常结束的时候 才关闭弹窗
|
|
12
|
+/// 其余情况, 都默认关闭弹窗
|
|
13
|
+void _handleFunc(ModalFunc? func) {
|
|
14
|
+ if (null != func) {
|
|
15
|
+ dynamic res = func();
|
|
16
|
+ if (res.runtimeType == bool) {
|
|
17
|
+ if (res) {
|
|
18
|
+ Get.back(closeOverlays: true);
|
|
19
|
+ }
|
|
20
|
+ } else if (res is Future) {
|
|
21
|
+ res.then((_) => Get.back(closeOverlays: true));
|
|
22
|
+ } else {
|
|
23
|
+ Get.back(closeOverlays: true);
|
|
24
|
+ }
|
|
25
|
+ } else {
|
|
26
|
+ Get.back(closeOverlays: true);
|
|
27
|
+ }
|
|
28
|
+}
|
|
29
|
+
|
|
30
|
+/// 打开一个 dialog
|
|
31
|
+void showDialog({ required String title, String? message, Widget? content, ModalFunc? onConfirm, ModalFunc? onCancel }) {
|
|
32
|
+ Get.dialog(
|
|
33
|
+ Modal(
|
|
34
|
+ type: 'dialog',
|
|
35
|
+ title: title,
|
|
36
|
+ message: message,
|
|
37
|
+ content: content,
|
|
38
|
+ onConfirm: () => _handleFunc(onConfirm),
|
|
39
|
+ onCancel: () => _handleFunc(onCancel),
|
|
40
|
+ )
|
|
41
|
+ );
|
|
42
|
+}
|
|
43
|
+
|
|
44
|
+/// 打开一个 alert
|
|
45
|
+void showAlert({ required String title, String? message, Widget? content, ModalFunc? onConfirm }) {
|
|
46
|
+ Get.dialog(
|
|
47
|
+ Modal(
|
|
48
|
+ type: 'alert',
|
|
49
|
+ title: title,
|
|
50
|
+ message: message,
|
|
51
|
+ content: content,
|
|
52
|
+ onConfirm: () => _handleFunc(onConfirm),
|
|
53
|
+ )
|
|
54
|
+ );
|
|
55
|
+}
|
|
56
|
+
|
|
57
|
+class Modal extends StatelessWidget {
|
7
|
58
|
|
8
|
59
|
String title;
|
9
|
60
|
String type;
|
|
@@ -12,7 +63,27 @@ class Dialog extends StatelessWidget {
|
12
|
63
|
VoidCallback? onConfirm;
|
13
|
64
|
VoidCallback? onCancel;
|
14
|
65
|
|
15
|
|
- Dialog({Key? key,
|
|
66
|
+ final _titleStyle = TextStyle(color: const Color(0xFF222222), fontSize: 20.sp, fontWeight: FontWeight.bold);
|
|
67
|
+ final _messageStyle = TextStyle(color: const Color(0xFF666666), fontSize: 18.sp);
|
|
68
|
+ final _primaryColor = const Color(0xFFFF703B);
|
|
69
|
+ final _btnSize = Size(90.w, 36.w);
|
|
70
|
+ final _btnShape = RoundedRectangleBorder(
|
|
71
|
+ side: const BorderSide(color: Color(0xFFFF703B), width: 2.0),
|
|
72
|
+ borderRadius: BorderRadius.all(Radius.circular(24.5.w)),
|
|
73
|
+ );
|
|
74
|
+
|
|
75
|
+ void _handleConfirm() {
|
|
76
|
+ if (null != onConfirm) {
|
|
77
|
+ onConfirm!();
|
|
78
|
+ }
|
|
79
|
+ }
|
|
80
|
+ void _handleCancel() {
|
|
81
|
+ if (null != onCancel) {
|
|
82
|
+ onCancel!();
|
|
83
|
+ }
|
|
84
|
+ }
|
|
85
|
+
|
|
86
|
+ Modal({Key? key,
|
16
|
87
|
this.type = 'alert',
|
17
|
88
|
required this.title,
|
18
|
89
|
this.message,
|
|
@@ -23,66 +94,74 @@ class Dialog extends StatelessWidget {
|
23
|
94
|
|
24
|
95
|
Widget _confirmBtn() {
|
25
|
96
|
return ElevatedButton(
|
26
|
|
- child: const Text("确定"),
|
|
97
|
+ child: Text("确定", style: TextStyle(color: Colors.white, fontSize: 18.sp, letterSpacing: 5.sp)),
|
27
|
98
|
style: ElevatedButton.styleFrom(
|
28
|
|
- primary: const Color(0xFFFF703B),
|
29
|
|
- textStyle: TextStyle(color: Colors.white, fontSize: 20.sp, letterSpacing: 5.sp),
|
|
99
|
+ primary: _primaryColor,
|
30
|
100
|
elevation: 0,
|
31
|
|
- minimumSize: Size(90.w, 49.w),
|
32
|
|
- shape: RoundedRectangleBorder(
|
33
|
|
- borderRadius: BorderRadius.all(Radius.circular(24.5.w)),
|
34
|
|
- )
|
|
101
|
+ minimumSize: _btnSize,
|
|
102
|
+ shape: _btnShape,
|
35
|
103
|
),
|
36
|
|
- onPressed: onConfirm,
|
|
104
|
+ onPressed: _handleConfirm,
|
37
|
105
|
);
|
38
|
106
|
}
|
39
|
107
|
|
40
|
108
|
Widget _cancelBtn() {
|
41
|
109
|
return ElevatedButton(
|
42
|
|
- child: const Text("取消"),
|
|
110
|
+ child: Text("取消", style: TextStyle(color: _primaryColor, fontSize: 18.sp, letterSpacing: 5.sp)),
|
43
|
111
|
style: ElevatedButton.styleFrom(
|
44
|
|
- primary: Colors.transparent,
|
45
|
|
- textStyle: TextStyle(color: const Color(0xFFFF703B), fontSize: 20.sp, letterSpacing: 5.sp),
|
|
112
|
+ primary: Colors.white,
|
46
|
113
|
elevation: 0,
|
47
|
|
- minimumSize: Size(90.w, 49.w),
|
48
|
|
- shape: RoundedRectangleBorder(
|
49
|
|
- side: const BorderSide(color: Color(0xFFFF703B), width: 2.0),
|
50
|
|
- borderRadius: BorderRadius.all(Radius.circular(24.5.w)),
|
51
|
|
- )
|
|
114
|
+ minimumSize: _btnSize,
|
|
115
|
+ shape: _btnShape,
|
52
|
116
|
),
|
53
|
|
- onPressed: onCancel,
|
|
117
|
+ onPressed: _handleCancel,
|
54
|
118
|
);
|
55
|
119
|
}
|
56
|
120
|
|
57
|
121
|
Widget _alert() {
|
58
|
122
|
return SimpleDialog(
|
59
|
|
- title: Text(title),
|
|
123
|
+ shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(24.w)),
|
|
124
|
+ titleTextStyle: _titleStyle,
|
|
125
|
+ title: Text(title, textAlign: TextAlign.center),
|
60
|
126
|
children: [
|
61
|
|
- if (null != message) Text(message!),
|
|
127
|
+ if (null != message) Text(message!, textAlign: TextAlign.center, style: _messageStyle),
|
62
|
128
|
if (null != content) content!,
|
|
129
|
+ SizedBox(height: 16.w),
|
63
|
130
|
Center(
|
64
|
131
|
child: _confirmBtn(),
|
65
|
|
- )
|
|
132
|
+ ),
|
|
133
|
+ SizedBox(height: 10.w),
|
66
|
134
|
]
|
67
|
135
|
);
|
68
|
136
|
}
|
69
|
137
|
|
70
|
138
|
Widget _dialog() {
|
|
139
|
+ Widget hSpacer = SizedBox(width: 16.w);
|
|
140
|
+
|
71
|
141
|
return SimpleDialog(
|
72
|
|
- title: Text(title),
|
|
142
|
+ shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(24.w)),
|
|
143
|
+ titleTextStyle: _titleStyle,
|
|
144
|
+ title: Text(title, textAlign: TextAlign.center),
|
|
145
|
+ alignment: Alignment.center,
|
73
|
146
|
children: [
|
74
|
|
- if (null != message) Text(message!),
|
|
147
|
+ if (null != message) Text(message!, textAlign: TextAlign.center, style: _messageStyle),
|
75
|
148
|
if (null != content) content!,
|
|
149
|
+ SizedBox(height: 16.w),
|
76
|
150
|
Row(
|
|
151
|
+ mainAxisAlignment: MainAxisAlignment.center,
|
77
|
152
|
children: [
|
|
153
|
+ hSpacer,
|
78
|
154
|
Center(
|
79
|
155
|
child: _confirmBtn(),
|
80
|
156
|
),
|
|
157
|
+ hSpacer,
|
81
|
158
|
Center(
|
82
|
159
|
child: _cancelBtn(),
|
83
|
|
- )
|
|
160
|
+ ),
|
|
161
|
+ hSpacer,
|
84
|
162
|
],
|
85
|
|
- )
|
|
163
|
+ ),
|
|
164
|
+ SizedBox(height: 10.w),
|
86
|
165
|
]
|
87
|
166
|
);
|
88
|
167
|
}
|