Skip to content

Commit f4d2108

Browse files
committed
finish showNotify #111
1 parent 1c5398b commit f4d2108

21 files changed

+328
-97
lines changed

example/lib/main.dart

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import 'dart:math';
2+
13
import 'package:flutter/material.dart';
24
import 'package:flutter_smart_dialog/flutter_smart_dialog.dart';
35

@@ -35,6 +37,12 @@ class SmartDialogPage extends StatelessWidget {
3537
child: Text('showLoading'),
3638
),
3739

40+
//notify
41+
ElevatedButton(
42+
onPressed: () => _showNotify(),
43+
child: Text('showNotify'),
44+
),
45+
3846
//dialog
3947
ElevatedButton(
4048
onPressed: () => _show(),
@@ -58,15 +66,10 @@ class SmartDialogPage extends StatelessWidget {
5866
}
5967

6068
void _showToast() async {
61-
for (var i = 0; i < 9; i++) {
62-
SmartDialog.showToast(
63-
'test toast ---- $i',
64-
displayType: SmartToastType.normal,
65-
);
66-
await Future.delayed(Duration(milliseconds: 500));
67-
}
68-
69-
SmartDialog.dismiss(status: SmartStatus.allToast);
69+
SmartDialog.showToast(
70+
'test toast ---- ${Random().nextInt(999)}',
71+
displayType: SmartToastType.multi,
72+
);
7073
}
7174

7275
void _show() async {
@@ -159,4 +162,12 @@ class SmartDialogPage extends StatelessWidget {
159162
await Future.delayed(Duration(seconds: 2));
160163
SmartDialog.dismiss();
161164
}
165+
166+
void _showNotify() async {
167+
SmartDialog.showNotify(
168+
msg: '请求成功',
169+
notifyType: NotifyType.failure,
170+
backType: SmartBackType.block,
171+
);
172+
}
162173
}

lib/flutter_smart_dialog.dart

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ export 'src/config/smart_config_custom.dart';
77
export 'src/config/smart_config_loading.dart';
88
export 'src/config/smart_config_toast.dart';
99
export 'src/data/animation_param.dart' show AnimationParam;
10+
export 'src/data/notify_style.dart';
11+
1012
/// dialog
1113
export 'src/init_dialog.dart';
1214
export 'src/smart_dialog.dart';

lib/src/config/enum_config.dart

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -242,15 +242,36 @@ enum SmartInitType {
242242
toast,
243243
}
244244

245-
enum SmartNotifyType {
245+
enum SmartBackType {
246+
/// Interception returns the event, closes the dialog,
247+
/// but does not close the page (if the dialog exists on the interface)
248+
///
249+
/// 拦截返回事件, 关闭dialog, 但不会关闭页面 (如果dialog存在在界面上)
250+
normal,
251+
252+
/// Interception returns the event without closing the dialog or closing the page
253+
///
254+
/// 拦截返回事件, 不关闭dialog, 也不关闭页面
255+
block,
256+
257+
/// Do not intercept the return event, the return event will not close the dialog
258+
///
259+
/// 不拦截返回事件, 返回事件将不会关闭dialog
260+
ignore,
261+
}
262+
263+
/// Prompt dialog to distinguish different types of showNotify
264+
///
265+
/// 用以区别showNotify不同类型的提示弹窗
266+
enum NotifyType {
246267
/// success
247268
success,
248269

249270
/// failure
250271
failure,
251272

252-
/// warn
253-
warn,
273+
/// warning
274+
warning,
254275

255276
/// error
256277
error,

lib/src/config/smart_config_notify.dart

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ class SmartConfigNotify {
2121
this.awaitOverType = SmartAwaitOverType.dialogDismiss,
2222
this.maskTriggerType = SmartMaskTriggerType.up,
2323
this.nonAnimationTypes = const [],
24+
this.backType = SmartBackType.ignore,
2425
this.isExist = false,
2526
});
2627

@@ -123,6 +124,12 @@ class SmartConfigNotify {
123124
/// 对于不同的场景, 可动态关闭弹窗动画, 具体请参照[SmartNonAnimationType]
124125
final List<SmartNonAnimationType> nonAnimationTypes;
125126

127+
/// For different processing types of return events,
128+
/// please refer to the description of [SmartBackType] for details
129+
///
130+
/// 对于返回事件不同的处理类型, 具体可参照[SmartBackType]说明
131+
final SmartBackType backType;
132+
126133
/// whether custom dialog(show()) exist on the screen
127134
///
128135
/// 自定义dialog(show()),是否存在在界面上

lib/src/custom/custom_notify.dart

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ class CustomNotify extends BaseDialog {
3636
required Duration? displayTime,
3737
required String? tag,
3838
required bool keepSingle,
39+
required SmartBackType backType,
3940
}) {
4041
if (DebounceUtils.instance.banContinue(DebounceType.notify, debounce)) {
4142
return Future.value(null);
@@ -46,6 +47,7 @@ class CustomNotify extends BaseDialog {
4647
keepSingle: keepSingle,
4748
debounce: debounce,
4849
displayTime: displayTime,
50+
backType: backType,
4951
);
5052
return mainDialog.show<T>(
5153
widget: widget,
@@ -102,6 +104,7 @@ class CustomNotify extends BaseDialog {
102104
required bool keepSingle,
103105
required bool debounce,
104106
required Duration? displayTime,
107+
required SmartBackType backType,
105108
}) {
106109
SmartDialog.config.notify.isExist = true;
107110

@@ -112,6 +115,7 @@ class CustomNotify extends BaseDialog {
112115
singleNotifyInfo = NotifyInfo(
113116
dialog: this,
114117
tag: tag ?? SmartTag.keepSingle,
118+
backType: backType,
115119
);
116120
_pushDialog(singleNotifyInfo);
117121
}
@@ -121,7 +125,7 @@ class CustomNotify extends BaseDialog {
121125
tag = tag ?? '${hashCode + Random().nextDouble()}';
122126

123127
// handle dialog stack
124-
notifyInfo = NotifyInfo(dialog: this, tag: tag);
128+
notifyInfo = NotifyInfo(dialog: this, tag: tag, backType: backType);
125129
_pushDialog(notifyInfo);
126130
}
127131

@@ -146,7 +150,7 @@ class CustomNotify extends BaseDialog {
146150
}
147151

148152
static Future<void>? dismiss<T>({
149-
DialogType type = DialogType.dialog,
153+
DialogType type = DialogType.notify,
150154
String? tag,
151155
T? result,
152156
bool force = false,

lib/src/custom/toast/custom_toast.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ class CustomToast extends BaseDialog {
110110

111111
if (newToast) {
112112
toastQueue.addLast(ToastInfo(
113-
type: SmartToastType.last,
113+
type: SmartToastType.normal,
114114
mainDialog: mainDialog,
115115
time: time,
116116
onShowToast: onShowToast,

lib/src/data/notify_info.dart

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,22 @@ import 'dart:async';
33
import 'package:flutter/material.dart';
44
import 'package:flutter_smart_dialog/src/custom/custom_dialog.dart';
55

6+
import '../../flutter_smart_dialog.dart';
67
import '../helper/dialog_proxy.dart';
78
import 'base_dialog.dart';
89

910
class NotifyInfo {
1011
NotifyInfo({
1112
required this.dialog,
1213
required this.tag,
14+
required this.backType,
1315
});
1416

1517
final BaseDialog dialog;
1618

1719
String? tag;
1820

21+
SmartBackType backType;
22+
1923
Timer? displayTimer;
2024
}

lib/src/data/notify_style.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,14 @@ class FlutterSmartNotifyStyle {
44
const FlutterSmartNotifyStyle({
55
this.successBuilder,
66
this.failureBuilder,
7-
this.warnBuilder,
7+
this.warningBuilder,
88
this.alertBuilder,
99
this.errorBuilder,
1010
});
1111

1212
final Widget Function(String msg)? successBuilder;
1313
final Widget Function(String msg)? failureBuilder;
14-
final Widget Function(String msg)? warnBuilder;
14+
final Widget Function(String msg)? warningBuilder;
1515
final Widget Function(String msg)? alertBuilder;
1616
final Widget Function(String msg)? errorBuilder;
1717
}

lib/src/helper/dialog_proxy.dart

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,7 @@ class DialogProxy {
166166
required Duration? displayTime,
167167
required String? tag,
168168
required bool keepSingle,
169+
required SmartBackType backType,
169170
}) {
170171
CustomNotify? dialog;
171172
var entry = SmartOverlayEntry(
@@ -190,6 +191,7 @@ class DialogProxy {
190191
displayTime: displayTime,
191192
tag: tag,
192193
keepSingle: keepSingle,
194+
backType: backType,
193195
);
194196
}
195197

lib/src/helper/pop_monitor/monitor_pop_route.dart

Lines changed: 63 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@ import 'package:flutter_smart_dialog/src/helper/dialog_proxy.dart';
33
import 'package:flutter_smart_dialog/src/helper/route_record.dart';
44
import 'package:flutter_smart_dialog/src/util/view_utils.dart';
55

6+
import '../../../flutter_smart_dialog.dart';
67
import '../../config/enum_config.dart';
8+
import '../../data/smart_tag.dart';
79

810
class MonitorPopRoute with WidgetsBindingObserver {
911
factory MonitorPopRoute() => instance;
@@ -20,15 +22,74 @@ class MonitorPopRoute with WidgetsBindingObserver {
2022

2123
@override
2224
Future<bool> didPopRoute() async {
25+
//loading
26+
if (SmartDialog.config.isExistLoading) {
27+
DialogProxy.instance.dismiss(
28+
status: SmartStatus.loading,
29+
closeType: CloseType.back,
30+
);
31+
return true;
32+
}
33+
34+
//notify
35+
if (SmartDialog.config.notify.isExist) {
36+
var notifyQueue = DialogProxy.instance.notifyQueue;
37+
for (var i = notifyQueue.length - 1; i >= 0; i--) {
38+
var item = notifyQueue.elementAt(i);
39+
if (item.backType == SmartBackType.normal) {
40+
DialogProxy.instance.dismiss(
41+
status: SmartStatus.notify,
42+
closeType: CloseType.back,
43+
tag: item.tag,
44+
);
45+
return true;
46+
} else if (item.backType == SmartBackType.block) {
47+
return true;
48+
}
49+
}
50+
}
51+
2352
// handle contain system dialog and common condition
24-
if (RouteRecord.instance.handleSmartDialog()) {
53+
if (handleSmartDialog()) {
2554
DialogProxy.instance.dismiss(
26-
status: SmartStatus.smart,
55+
status: SmartStatus.dialog,
2756
closeType: CloseType.back,
2857
);
2958
return true;
3059
}
3160

3261
return super.didPopRoute();
3362
}
63+
64+
bool handleSmartDialog() {
65+
bool shouldHandle = true;
66+
var routeQueue = RouteRecord.instance.routeQueue;
67+
try {
68+
//handle dialog
69+
if (DialogProxy.instance.dialogQueue.isEmpty) {
70+
if (routeQueue.isNotEmpty) routeQueue.clear();
71+
shouldHandle = false;
72+
} else {
73+
var info = DialogProxy.instance.dialogQueue.last;
74+
75+
//deal with system dialog
76+
if (info.useSystem && routeQueue.isNotEmpty) {
77+
var route = routeQueue.last;
78+
if (route.settings.name != SmartTag.systemDialog) {
79+
shouldHandle = false;
80+
}
81+
} else {
82+
// deal with bindPage and permanent dialog
83+
if (!info.dialog.mainDialog.visible || info.permanent) {
84+
shouldHandle = false;
85+
}
86+
}
87+
}
88+
} catch (e) {
89+
shouldHandle = false;
90+
print('SmartDialog back event error:${e.toString()}');
91+
}
92+
93+
return shouldHandle;
94+
}
3495
}

lib/src/helper/route_record.dart

Lines changed: 0 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -32,42 +32,6 @@ class RouteRecord {
3232
routeQueue.remove(route);
3333
}
3434

35-
bool handleSmartDialog() {
36-
bool shouldHandle = true;
37-
try {
38-
//handle loading
39-
if (SmartDialog.config.isExistLoading) {
40-
return true;
41-
}
42-
43-
//handle dialog
44-
if (DialogProxy.instance.dialogQueue.isEmpty) {
45-
if (routeQueue.isNotEmpty) routeQueue.clear();
46-
shouldHandle = false;
47-
} else {
48-
var info = DialogProxy.instance.dialogQueue.last;
49-
50-
//deal with system dialog
51-
if (info.useSystem && routeQueue.isNotEmpty) {
52-
var route = routeQueue.last;
53-
if (route.settings.name != SmartTag.systemDialog) {
54-
shouldHandle = false;
55-
}
56-
} else {
57-
// deal with bindPage and permanent dialog
58-
if (!info.dialog.mainDialog.visible || info.permanent) {
59-
shouldHandle = false;
60-
}
61-
}
62-
}
63-
} catch (e) {
64-
shouldHandle = false;
65-
print('SmartDialog back event error:${e.toString()}');
66-
}
67-
68-
return shouldHandle;
69-
}
70-
7135
void _hideDialog(Route<dynamic>? curRoute) {
7236
if (curRoute == null || DialogProxy.instance.dialogQueue.isEmpty) return;
7337
for (var item in DialogProxy.instance.dialogQueue) {

0 commit comments

Comments
 (0)