Skip to content

Commit a192f46

Browse files
committed
optimize 'keepSingle', 'displayTime', 'tag'
1 parent 0eb7719 commit a192f46

File tree

10 files changed

+98
-115
lines changed

10 files changed

+98
-115
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
- Compatible with flutter_boost
77
- Add 'SmartAttachAlignmentType'
88
- Add 'SmartInitType'
9+
- Optimize 'keepSingle', 'displayTime', 'tag'
910

1011

1112
# [4.6.x]

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ Language: English | [中文](https://juejin.cn/post/7026150456673959943)
44

55
Migrate doc:[3.x migrate 4.0](https://github.com/fluttercandies/flutter_smart_dialog/blob/master/docs/3.x%20migrate%204.0.md) | [3.x 迁移 4.0](https://juejin.cn/post/7093867453012246565)
66

7-
Flutter 2:Please use `flutter_smart_dialog: 4.2.2`
7+
Flutter 2:Please use `flutter_smart_dialog: 4.2.3`
88

99
Flutter 3:Please use the latest version
1010

lib/src/custom/custom_dialog.dart

Lines changed: 58 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,6 @@ enum DialogType {
2929
class CustomDialog extends BaseDialog {
3030
CustomDialog({required SmartOverlayEntry overlayEntry}) : super(overlayEntry);
3131

32-
DateTime? clickMaskLastTime;
33-
34-
var _timeRandom = Random().nextInt(666666) + Random().nextDouble();
35-
3632
Future<T?> show<T>({
3733
required Widget widget,
3834
required AlignmentGeometry alignment,
@@ -58,8 +54,10 @@ class CustomDialog extends BaseDialog {
5854
required BuildContext? bindWidget,
5955
required Rect? ignoreArea,
6056
}) {
61-
if (!_handleMustOperate(
62-
tag: displayTime != null ? _getTimeKey(displayTime) : tag,
57+
if (!_checkDebounce(debounce, DialogType.custom)) return Future.value(null);
58+
59+
final dialogInfo = _handleMustOperate(
60+
tag: tag,
6361
backDismiss: backDismiss,
6462
keepSingle: keepSingle,
6563
debounce: debounce,
@@ -68,7 +66,8 @@ class CustomDialog extends BaseDialog {
6866
useSystem: useSystem,
6967
bindPage: bindPage,
7068
bindWidget: bindWidget,
71-
)) return Future.value(null);
69+
displayTime: displayTime,
70+
);
7271
return mainDialog.show<T>(
7372
widget: widget,
7473
alignment: alignment,
@@ -80,7 +79,7 @@ class CustomDialog extends BaseDialog {
8079
animationBuilder: animationBuilder,
8180
maskColor: maskColor,
8281
maskWidget: maskWidget,
83-
onDismiss: _handleDismiss(onDismiss, displayTime),
82+
onDismiss: _handleDismiss(onDismiss, displayTime, dialogInfo),
8483
useSystem: useSystem,
8584
reuse: true,
8685
awaitOverType: SmartDialog.config.custom.awaitOverType,
@@ -124,8 +123,10 @@ class CustomDialog extends BaseDialog {
124123
required bool bindPage,
125124
required BuildContext? bindWidget,
126125
}) {
127-
if (!_handleMustOperate(
128-
tag: displayTime != null ? _getTimeKey(displayTime) : tag,
126+
if (!_checkDebounce(debounce, DialogType.attach)) return Future.value(null);
127+
128+
final dialogInfo = _handleMustOperate(
129+
tag: tag,
129130
backDismiss: backDismiss,
130131
keepSingle: keepSingle,
131132
debounce: debounce,
@@ -134,7 +135,8 @@ class CustomDialog extends BaseDialog {
134135
useSystem: useSystem,
135136
bindPage: bindPage,
136137
bindWidget: bindWidget,
137-
)) return Future.value(null);
138+
displayTime: displayTime,
139+
);
138140
return mainDialog.showAttach<T>(
139141
targetContext: targetContext,
140142
widget: widget,
@@ -152,7 +154,7 @@ class CustomDialog extends BaseDialog {
152154
maskColor: maskColor,
153155
maskWidget: maskWidget,
154156
maskIgnoreArea: maskIgnoreArea,
155-
onDismiss: _handleDismiss(onDismiss, displayTime),
157+
onDismiss: _handleDismiss(onDismiss, displayTime, dialogInfo),
156158
useSystem: useSystem,
157159
awaitOverType: SmartDialog.config.attach.awaitOverType,
158160
maskTriggerType: SmartDialog.config.attach.maskTriggerType,
@@ -164,10 +166,20 @@ class CustomDialog extends BaseDialog {
164166
);
165167
}
166168

167-
VoidCallback _handleDismiss(VoidCallback? onDismiss, Duration? displayTime) {
169+
VoidCallback _handleDismiss(
170+
VoidCallback? onDismiss,
171+
Duration? displayTime,
172+
DialogInfo dialogInfo,
173+
) {
174+
if (dialogInfo.tag == SmartTag.keepSingle) {
175+
dialogInfo.displayTimer?.cancel();
176+
}
177+
168178
Timer? timer;
169-
if (displayTime != null) {
170-
timer = Timer(displayTime, () => dismiss(tag: _getTimeKey(displayTime)));
179+
final tag = dialogInfo.tag;
180+
if (displayTime != null && tag != null) {
181+
timer = Timer(displayTime, () => dismiss(tag: tag));
182+
dialogInfo.displayTimer = timer;
171183
}
172184

173185
return () {
@@ -176,9 +188,7 @@ class CustomDialog extends BaseDialog {
176188
};
177189
}
178190

179-
String _getTimeKey(Duration time) => '${time.hashCode + _timeRandom}';
180-
181-
bool _handleMustOperate({
191+
DialogInfo _handleMustOperate({
182192
required String? tag,
183193
required bool backDismiss,
184194
required bool keepSingle,
@@ -188,45 +198,16 @@ class CustomDialog extends BaseDialog {
188198
required bool useSystem,
189199
required bool bindPage,
190200
required BuildContext? bindWidget,
201+
required Duration? displayTime,
191202
}) {
192-
// debounce
193-
if (!_checkDebounce(debounce, type)) return false;
194-
195-
//handle dialog stack
196-
_handleDialogStack(
197-
tag: tag,
198-
backDismiss: backDismiss,
199-
keepSingle: keepSingle,
200-
type: type,
201-
permanent: permanent,
202-
useSystem: useSystem,
203-
bindPage: bindPage,
204-
bindWidget: bindWidget,
205-
);
206-
207203
SmartDialog.config.custom.isExist = DialogType.custom == type;
208204
SmartDialog.config.attach.isExist = DialogType.attach == type;
209-
return true;
210-
}
211-
212-
void _handleDialogStack({
213-
required String? tag,
214-
required bool backDismiss,
215-
required bool keepSingle,
216-
required DialogType type,
217-
required bool permanent,
218-
required bool useSystem,
219-
required bool bindPage,
220-
required BuildContext? bindWidget,
221-
}) {
222-
if (bindWidget != null) {
223-
tag = tag ?? "${bindPage.hashCode}";
224-
}
225205

206+
DialogInfo dialogInfo;
226207
if (keepSingle) {
227-
DialogInfo? dialogInfo = _getDialog(tag: tag ?? SmartTag.keepSingle);
228-
if (dialogInfo == null) {
229-
dialogInfo = DialogInfo(
208+
var singleDialogInfo = _getDialog(tag: tag ?? SmartTag.keepSingle);
209+
if (singleDialogInfo == null) {
210+
singleDialogInfo = DialogInfo(
230211
dialog: this,
231212
backDismiss: backDismiss,
232213
type: type,
@@ -237,26 +218,33 @@ class CustomDialog extends BaseDialog {
237218
route: RouteRecord.curRoute,
238219
bindWidget: bindWidget,
239220
);
240-
_pushDialog(dialogInfo);
221+
_pushDialog(singleDialogInfo);
222+
}
223+
mainDialog = singleDialogInfo.dialog.mainDialog;
224+
dialogInfo = singleDialogInfo;
225+
} else {
226+
if (displayTime != null) {
227+
tag = tag ?? '${displayTime.hashCode + Random().nextInt(666666) + Random().nextDouble()}';
228+
} else if (bindWidget != null) {
229+
tag = tag ?? "${bindPage.hashCode}";
241230
}
242231

243-
mainDialog = dialogInfo.dialog.mainDialog;
244-
return;
232+
// handle dialog stack
233+
dialogInfo = DialogInfo(
234+
dialog: this,
235+
backDismiss: backDismiss,
236+
type: type,
237+
tag: tag,
238+
permanent: permanent,
239+
useSystem: useSystem,
240+
bindPage: bindPage,
241+
route: RouteRecord.curRoute,
242+
bindWidget: bindWidget,
243+
);
244+
_pushDialog(dialogInfo);
245245
}
246246

247-
// handle dialog stack
248-
var dialogInfo = DialogInfo(
249-
dialog: this,
250-
backDismiss: backDismiss,
251-
type: type,
252-
tag: tag,
253-
permanent: permanent,
254-
useSystem: useSystem,
255-
bindPage: bindPage,
256-
route: RouteRecord.curRoute,
257-
bindWidget: bindWidget,
258-
);
259-
_pushDialog(dialogInfo);
247+
return dialogInfo;
260248
}
261249

262250
void _pushDialog(DialogInfo dialogInfo) {
@@ -310,6 +298,8 @@ class CustomDialog extends BaseDialog {
310298
return true;
311299
}
312300

301+
DateTime? clickMaskLastTime;
302+
313303
bool _clickMaskDebounce() {
314304
var now = DateTime.now();
315305
var isShake = clickMaskLastTime != null &&

lib/src/custom/custom_toast.dart

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -177,16 +177,18 @@ class CustomToast extends BaseDialog {
177177
if (_toastQueue.isNotEmpty) _toastQueue.clear();
178178

179179
if (_onlyDialogScope == null) {
180-
var dialogScope = (widget as ToastHelper).child as DialogScope;
181-
_onlyDialogScope = dialogScope;
182-
if (dialogScope.controller != null) {
183-
_toastController = dialogScope.controller;
184-
} else {
185-
dialogScope.dialogScopeState.setController(_toastController = SmartDialogController());
186-
}
180+
_onlyDialogScope = (widget as ToastHelper).child as DialogScope;
187181
onShowToast();
188182
} else {
189-
_onlyDialogScope!.dialogScopeState.replaceBuilder(widget);
183+
var scope = _onlyDialogScope!;
184+
if (_toastController == null) {
185+
if (scope.controller != null) {
186+
_toastController = scope.controller;
187+
} else {
188+
scope.info.state?.setController(_toastController = SmartDialogController());
189+
}
190+
}
191+
scope.info.state?.replaceBuilder(widget);
190192
_toastController?.refresh();
191193
}
192194

@@ -195,6 +197,7 @@ class CustomToast extends BaseDialog {
195197
_realDismiss();
196198
_onlyTime = null;
197199
_onlyDialogScope = null;
200+
_toastController = null;
198201
});
199202
}
200203

lib/src/data/dialog_info.dart

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import 'dart:async';
2+
13
import 'package:flutter/material.dart';
24
import 'package:flutter_smart_dialog/src/custom/custom_dialog.dart';
35

@@ -33,4 +35,6 @@ class DialogInfo {
3335
final Route<dynamic>? route;
3436

3537
final BuildContext? bindWidget;
38+
39+
Timer? displayTimer;
3640
}

lib/src/helper/dialog_proxy.dart

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,17 +56,18 @@ class DialogProxy {
5656

5757
DialogProxy._internal() {
5858
config = SmartConfig();
59-
6059
dialogQueue = ListQueue();
61-
}
6260

63-
void initialize() {
6461
entryLoading = SmartOverlayEntry(builder: (_) => _loading.getWidget());
6562
_loading = CustomLoading(overlayEntry: entryLoading);
6663
entryToast = SmartOverlayEntry(builder: (_) => _toast.getWidget());
6764
_toast = CustomToast(overlayEntry: entryToast);
6865
}
6966

67+
void initialize() {
68+
69+
}
70+
7071
Future<T?> show<T>({
7172
required Widget widget,
7273
required AlignmentGeometry alignment,

lib/src/init_dialog.dart

Lines changed: 12 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -97,20 +97,6 @@ class _FlutterSmartDialogState extends State<FlutterSmartDialog> {
9797
} catch (e) {}
9898
});
9999

100-
var proxy = DialogProxy.instance;
101-
// solve Flutter Inspector -> select widget mode function failure problem
102-
proxy.initialize();
103-
defaultToast(String msg) {
104-
return ToastWidget(msg: msg);
105-
}
106-
107-
defaultLoading(String msg) {
108-
return LoadingWidget(msg: msg);
109-
}
110-
111-
proxy.toastBuilder = widget.toastBuilder ?? defaultToast;
112-
proxy.loadingBuilder = widget.loadingBuilder ?? defaultLoading;
113-
114100
// init param
115101
styleBuilder = widget.styleBuilder ??
116102
(Widget child) {
@@ -119,6 +105,16 @@ class _FlutterSmartDialogState extends State<FlutterSmartDialog> {
119105
initType = widget.initType ??
120106
{SmartInitType.custom, SmartInitType.attach, SmartInitType.loading, SmartInitType.toast};
121107

108+
// default toast / loading
109+
if (initType.contains(SmartInitType.toast)){
110+
DialogProxy.instance.toastBuilder =
111+
widget.toastBuilder ?? (String msg) => ToastWidget(msg: msg);
112+
}
113+
if (initType.contains(SmartInitType.loading)){
114+
DialogProxy.instance.loadingBuilder =
115+
widget.loadingBuilder ?? (String msg) => LoadingWidget(msg: msg);
116+
}
117+
122118
super.initState();
123119
}
124120

@@ -131,27 +127,19 @@ class _FlutterSmartDialogState extends State<FlutterSmartDialog> {
131127
),
132128

133129
//provided separately for custom dialog
134-
if (initType.contains(SmartInitType.custom) && !initType.contains(SmartInitType.attach))
130+
if (initType.contains(SmartInitType.custom))
135131
OverlayEntry(builder: (BuildContext context) {
136132
DialogProxy.contextCustom = context;
137133
return Container();
138134
}),
139135

140136
//provided separately for attach dialog
141-
if (initType.contains(SmartInitType.attach) && !initType.contains(SmartInitType.custom))
137+
if (initType.contains(SmartInitType.attach))
142138
OverlayEntry(builder: (BuildContext context) {
143139
DialogProxy.contextAttach = context;
144140
return Container();
145141
}),
146142

147-
//provided separately for custom dialog and attach dialog
148-
if (initType.contains(SmartInitType.custom) && initType.contains(SmartInitType.attach))
149-
OverlayEntry(builder: (BuildContext context) {
150-
DialogProxy.contextCustom = context;
151-
DialogProxy.contextAttach = context;
152-
return Container();
153-
}),
154-
155143
//provided separately for loading
156144
if (initType.contains(SmartInitType.loading)) DialogProxy.instance.entryLoading,
157145

lib/src/smart_dialog.dart

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -180,10 +180,6 @@ class SmartDialog {
180180
(useSystem == null || useSystem == false),
181181
'useSystem is true; tag, keepSingle and permanent prohibit setting values',
182182
);
183-
assert(
184-
displayTime == null || tag == null,
185-
'displayTime is used, tag prohibit setting values',
186-
);
187183

188184
return DialogProxy.instance.show<T>(
189185
widget: DialogScope(
@@ -417,10 +413,6 @@ class SmartDialog {
417413
(useSystem == null || useSystem == false),
418414
'useSystem is true; tag, keepSingle and permanent prohibit setting values',
419415
);
420-
assert(
421-
displayTime == null || tag == null,
422-
'displayTime is used, tag prohibit setting values',
423-
);
424416

425417
return DialogProxy.instance.showAttach<T>(
426418
targetContext: targetContext,

0 commit comments

Comments
 (0)