Skip to content

Commit

Permalink
optimize 'keepSingle', 'displayTime', 'tag'
Browse files Browse the repository at this point in the history
  • Loading branch information
xdd666t committed Dec 21, 2022
1 parent 0eb7719 commit a192f46
Show file tree
Hide file tree
Showing 10 changed files with 98 additions and 115 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
- Compatible with flutter_boost
- Add 'SmartAttachAlignmentType'
- Add 'SmartInitType'
- Optimize 'keepSingle', 'displayTime', 'tag'


# [4.6.x]
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ Language: English | [中文](https://juejin.cn/post/7026150456673959943)

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)

Flutter 2:Please use `flutter_smart_dialog: 4.2.2`
Flutter 2:Please use `flutter_smart_dialog: 4.2.3`

Flutter 3:Please use the latest version

Expand Down
126 changes: 58 additions & 68 deletions lib/src/custom/custom_dialog.dart
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,6 @@ enum DialogType {
class CustomDialog extends BaseDialog {
CustomDialog({required SmartOverlayEntry overlayEntry}) : super(overlayEntry);

DateTime? clickMaskLastTime;

var _timeRandom = Random().nextInt(666666) + Random().nextDouble();

Future<T?> show<T>({
required Widget widget,
required AlignmentGeometry alignment,
Expand All @@ -58,8 +54,10 @@ class CustomDialog extends BaseDialog {
required BuildContext? bindWidget,
required Rect? ignoreArea,
}) {
if (!_handleMustOperate(
tag: displayTime != null ? _getTimeKey(displayTime) : tag,
if (!_checkDebounce(debounce, DialogType.custom)) return Future.value(null);

final dialogInfo = _handleMustOperate(
tag: tag,
backDismiss: backDismiss,
keepSingle: keepSingle,
debounce: debounce,
Expand All @@ -68,7 +66,8 @@ class CustomDialog extends BaseDialog {
useSystem: useSystem,
bindPage: bindPage,
bindWidget: bindWidget,
)) return Future.value(null);
displayTime: displayTime,
);
return mainDialog.show<T>(
widget: widget,
alignment: alignment,
Expand All @@ -80,7 +79,7 @@ class CustomDialog extends BaseDialog {
animationBuilder: animationBuilder,
maskColor: maskColor,
maskWidget: maskWidget,
onDismiss: _handleDismiss(onDismiss, displayTime),
onDismiss: _handleDismiss(onDismiss, displayTime, dialogInfo),
useSystem: useSystem,
reuse: true,
awaitOverType: SmartDialog.config.custom.awaitOverType,
Expand Down Expand Up @@ -124,8 +123,10 @@ class CustomDialog extends BaseDialog {
required bool bindPage,
required BuildContext? bindWidget,
}) {
if (!_handleMustOperate(
tag: displayTime != null ? _getTimeKey(displayTime) : tag,
if (!_checkDebounce(debounce, DialogType.attach)) return Future.value(null);

final dialogInfo = _handleMustOperate(
tag: tag,
backDismiss: backDismiss,
keepSingle: keepSingle,
debounce: debounce,
Expand All @@ -134,7 +135,8 @@ class CustomDialog extends BaseDialog {
useSystem: useSystem,
bindPage: bindPage,
bindWidget: bindWidget,
)) return Future.value(null);
displayTime: displayTime,
);
return mainDialog.showAttach<T>(
targetContext: targetContext,
widget: widget,
Expand All @@ -152,7 +154,7 @@ class CustomDialog extends BaseDialog {
maskColor: maskColor,
maskWidget: maskWidget,
maskIgnoreArea: maskIgnoreArea,
onDismiss: _handleDismiss(onDismiss, displayTime),
onDismiss: _handleDismiss(onDismiss, displayTime, dialogInfo),
useSystem: useSystem,
awaitOverType: SmartDialog.config.attach.awaitOverType,
maskTriggerType: SmartDialog.config.attach.maskTriggerType,
Expand All @@ -164,10 +166,20 @@ class CustomDialog extends BaseDialog {
);
}

VoidCallback _handleDismiss(VoidCallback? onDismiss, Duration? displayTime) {
VoidCallback _handleDismiss(
VoidCallback? onDismiss,
Duration? displayTime,
DialogInfo dialogInfo,
) {
if (dialogInfo.tag == SmartTag.keepSingle) {
dialogInfo.displayTimer?.cancel();
}

Timer? timer;
if (displayTime != null) {
timer = Timer(displayTime, () => dismiss(tag: _getTimeKey(displayTime)));
final tag = dialogInfo.tag;
if (displayTime != null && tag != null) {
timer = Timer(displayTime, () => dismiss(tag: tag));
dialogInfo.displayTimer = timer;
}

return () {
Expand All @@ -176,9 +188,7 @@ class CustomDialog extends BaseDialog {
};
}

String _getTimeKey(Duration time) => '${time.hashCode + _timeRandom}';

bool _handleMustOperate({
DialogInfo _handleMustOperate({
required String? tag,
required bool backDismiss,
required bool keepSingle,
Expand All @@ -188,45 +198,16 @@ class CustomDialog extends BaseDialog {
required bool useSystem,
required bool bindPage,
required BuildContext? bindWidget,
required Duration? displayTime,
}) {
// debounce
if (!_checkDebounce(debounce, type)) return false;

//handle dialog stack
_handleDialogStack(
tag: tag,
backDismiss: backDismiss,
keepSingle: keepSingle,
type: type,
permanent: permanent,
useSystem: useSystem,
bindPage: bindPage,
bindWidget: bindWidget,
);

SmartDialog.config.custom.isExist = DialogType.custom == type;
SmartDialog.config.attach.isExist = DialogType.attach == type;
return true;
}

void _handleDialogStack({
required String? tag,
required bool backDismiss,
required bool keepSingle,
required DialogType type,
required bool permanent,
required bool useSystem,
required bool bindPage,
required BuildContext? bindWidget,
}) {
if (bindWidget != null) {
tag = tag ?? "${bindPage.hashCode}";
}

DialogInfo dialogInfo;
if (keepSingle) {
DialogInfo? dialogInfo = _getDialog(tag: tag ?? SmartTag.keepSingle);
if (dialogInfo == null) {
dialogInfo = DialogInfo(
var singleDialogInfo = _getDialog(tag: tag ?? SmartTag.keepSingle);
if (singleDialogInfo == null) {
singleDialogInfo = DialogInfo(
dialog: this,
backDismiss: backDismiss,
type: type,
Expand All @@ -237,26 +218,33 @@ class CustomDialog extends BaseDialog {
route: RouteRecord.curRoute,
bindWidget: bindWidget,
);
_pushDialog(dialogInfo);
_pushDialog(singleDialogInfo);
}
mainDialog = singleDialogInfo.dialog.mainDialog;
dialogInfo = singleDialogInfo;
} else {
if (displayTime != null) {
tag = tag ?? '${displayTime.hashCode + Random().nextInt(666666) + Random().nextDouble()}';
} else if (bindWidget != null) {
tag = tag ?? "${bindPage.hashCode}";
}

mainDialog = dialogInfo.dialog.mainDialog;
return;
// handle dialog stack
dialogInfo = DialogInfo(
dialog: this,
backDismiss: backDismiss,
type: type,
tag: tag,
permanent: permanent,
useSystem: useSystem,
bindPage: bindPage,
route: RouteRecord.curRoute,
bindWidget: bindWidget,
);
_pushDialog(dialogInfo);
}

// handle dialog stack
var dialogInfo = DialogInfo(
dialog: this,
backDismiss: backDismiss,
type: type,
tag: tag,
permanent: permanent,
useSystem: useSystem,
bindPage: bindPage,
route: RouteRecord.curRoute,
bindWidget: bindWidget,
);
_pushDialog(dialogInfo);
return dialogInfo;
}

void _pushDialog(DialogInfo dialogInfo) {
Expand Down Expand Up @@ -310,6 +298,8 @@ class CustomDialog extends BaseDialog {
return true;
}

DateTime? clickMaskLastTime;

bool _clickMaskDebounce() {
var now = DateTime.now();
var isShake = clickMaskLastTime != null &&
Expand Down
19 changes: 11 additions & 8 deletions lib/src/custom/custom_toast.dart
Original file line number Diff line number Diff line change
Expand Up @@ -177,16 +177,18 @@ class CustomToast extends BaseDialog {
if (_toastQueue.isNotEmpty) _toastQueue.clear();

if (_onlyDialogScope == null) {
var dialogScope = (widget as ToastHelper).child as DialogScope;
_onlyDialogScope = dialogScope;
if (dialogScope.controller != null) {
_toastController = dialogScope.controller;
} else {
dialogScope.dialogScopeState.setController(_toastController = SmartDialogController());
}
_onlyDialogScope = (widget as ToastHelper).child as DialogScope;
onShowToast();
} else {
_onlyDialogScope!.dialogScopeState.replaceBuilder(widget);
var scope = _onlyDialogScope!;
if (_toastController == null) {
if (scope.controller != null) {
_toastController = scope.controller;
} else {
scope.info.state?.setController(_toastController = SmartDialogController());
}
}
scope.info.state?.replaceBuilder(widget);
_toastController?.refresh();
}

Expand All @@ -195,6 +197,7 @@ class CustomToast extends BaseDialog {
_realDismiss();
_onlyTime = null;
_onlyDialogScope = null;
_toastController = null;
});
}

Expand Down
4 changes: 4 additions & 0 deletions lib/src/data/dialog_info.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import 'dart:async';

import 'package:flutter/material.dart';
import 'package:flutter_smart_dialog/src/custom/custom_dialog.dart';

Expand Down Expand Up @@ -33,4 +35,6 @@ class DialogInfo {
final Route<dynamic>? route;

final BuildContext? bindWidget;

Timer? displayTimer;
}
7 changes: 4 additions & 3 deletions lib/src/helper/dialog_proxy.dart
Original file line number Diff line number Diff line change
Expand Up @@ -56,17 +56,18 @@ class DialogProxy {

DialogProxy._internal() {
config = SmartConfig();

dialogQueue = ListQueue();
}

void initialize() {
entryLoading = SmartOverlayEntry(builder: (_) => _loading.getWidget());
_loading = CustomLoading(overlayEntry: entryLoading);
entryToast = SmartOverlayEntry(builder: (_) => _toast.getWidget());
_toast = CustomToast(overlayEntry: entryToast);
}

void initialize() {

}

Future<T?> show<T>({
required Widget widget,
required AlignmentGeometry alignment,
Expand Down
36 changes: 12 additions & 24 deletions lib/src/init_dialog.dart
Original file line number Diff line number Diff line change
Expand Up @@ -97,20 +97,6 @@ class _FlutterSmartDialogState extends State<FlutterSmartDialog> {
} catch (e) {}
});

var proxy = DialogProxy.instance;
// solve Flutter Inspector -> select widget mode function failure problem
proxy.initialize();
defaultToast(String msg) {
return ToastWidget(msg: msg);
}

defaultLoading(String msg) {
return LoadingWidget(msg: msg);
}

proxy.toastBuilder = widget.toastBuilder ?? defaultToast;
proxy.loadingBuilder = widget.loadingBuilder ?? defaultLoading;

// init param
styleBuilder = widget.styleBuilder ??
(Widget child) {
Expand All @@ -119,6 +105,16 @@ class _FlutterSmartDialogState extends State<FlutterSmartDialog> {
initType = widget.initType ??
{SmartInitType.custom, SmartInitType.attach, SmartInitType.loading, SmartInitType.toast};

// default toast / loading
if (initType.contains(SmartInitType.toast)){
DialogProxy.instance.toastBuilder =
widget.toastBuilder ?? (String msg) => ToastWidget(msg: msg);
}
if (initType.contains(SmartInitType.loading)){
DialogProxy.instance.loadingBuilder =
widget.loadingBuilder ?? (String msg) => LoadingWidget(msg: msg);
}

super.initState();
}

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

//provided separately for custom dialog
if (initType.contains(SmartInitType.custom) && !initType.contains(SmartInitType.attach))
if (initType.contains(SmartInitType.custom))
OverlayEntry(builder: (BuildContext context) {
DialogProxy.contextCustom = context;
return Container();
}),

//provided separately for attach dialog
if (initType.contains(SmartInitType.attach) && !initType.contains(SmartInitType.custom))
if (initType.contains(SmartInitType.attach))
OverlayEntry(builder: (BuildContext context) {
DialogProxy.contextAttach = context;
return Container();
}),

//provided separately for custom dialog and attach dialog
if (initType.contains(SmartInitType.custom) && initType.contains(SmartInitType.attach))
OverlayEntry(builder: (BuildContext context) {
DialogProxy.contextCustom = context;
DialogProxy.contextAttach = context;
return Container();
}),

//provided separately for loading
if (initType.contains(SmartInitType.loading)) DialogProxy.instance.entryLoading,

Expand Down
8 changes: 0 additions & 8 deletions lib/src/smart_dialog.dart
Original file line number Diff line number Diff line change
Expand Up @@ -180,10 +180,6 @@ class SmartDialog {
(useSystem == null || useSystem == false),
'useSystem is true; tag, keepSingle and permanent prohibit setting values',
);
assert(
displayTime == null || tag == null,
'displayTime is used, tag prohibit setting values',
);

return DialogProxy.instance.show<T>(
widget: DialogScope(
Expand Down Expand Up @@ -417,10 +413,6 @@ class SmartDialog {
(useSystem == null || useSystem == false),
'useSystem is true; tag, keepSingle and permanent prohibit setting values',
);
assert(
displayTime == null || tag == null,
'displayTime is used, tag prohibit setting values',
);

return DialogProxy.instance.showAttach<T>(
targetContext: targetContext,
Expand Down
Loading

0 comments on commit a192f46

Please sign in to comment.