dialog_engine.dart 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. import 'package:flutter/material.dart';
  2. import 'package:flutter_smart_dialog/flutter_smart_dialog.dart';
  3. enum DialogPosition {
  4. top,
  5. center,
  6. bottom,
  7. }
  8. enum DialogAnimation {
  9. /// 全部位置都为渐隐动画
  10. fade,
  11. /// 全部位置都为缩放动画
  12. scale,
  13. /// 中间位置的为渐隐动画, 其他位置为位移动画
  14. centerFade_otherSlide,
  15. /// 中间位置的为缩放, 其他位置为位移动画
  16. centerScale_otherSlide,
  17. }
  18. class DialogEngine {
  19. //展示依附弹窗
  20. static Future showAttach({
  21. required BuildContext? targetContext,
  22. required Widget widget,
  23. DialogPosition position = DialogPosition.center,
  24. DialogAnimation? animType,
  25. VoidCallback? onDismiss,
  26. String? tag,
  27. Color maskColor = Colors.transparent,
  28. bool? clickMaskDismiss,
  29. bool? backDismiss,
  30. bool? keepSingle,
  31. bool usePenetrate = false,
  32. }) {
  33. Alignment alignment;
  34. if (position == DialogPosition.bottom) {
  35. alignment = Alignment.bottomCenter;
  36. } else if (position == DialogPosition.top) {
  37. alignment = Alignment.topCenter;
  38. } else {
  39. alignment = Alignment.center;
  40. }
  41. SmartAnimationType? smartAnimType;
  42. if (animType == DialogAnimation.fade) {
  43. smartAnimType = SmartAnimationType.fade;
  44. } else if (animType == DialogAnimation.scale) {
  45. smartAnimType = SmartAnimationType.scale;
  46. } else if (animType == DialogAnimation.centerFade_otherSlide) {
  47. smartAnimType = SmartAnimationType.centerFade_otherSlide;
  48. } else if (animType == DialogAnimation.centerScale_otherSlide) {
  49. smartAnimType = SmartAnimationType.centerScale_otherSlide;
  50. }
  51. return SmartDialog.showAttach(
  52. targetContext: targetContext,
  53. bindWidget: targetContext,
  54. builder: (context) => widget,
  55. usePenetrate: usePenetrate,
  56. debounce: true,
  57. clickMaskDismiss: clickMaskDismiss,
  58. backDismiss: backDismiss,
  59. animationType: smartAnimType,
  60. alignment: alignment,
  61. onDismiss: onDismiss,
  62. tag: tag,
  63. maskColor: maskColor,
  64. keepSingle: keepSingle,
  65. );
  66. }
  67. /// 展示默认弹窗
  68. static Future show({
  69. required Widget widget,
  70. DialogPosition position = DialogPosition.center,
  71. DialogAnimation? animType,
  72. VoidCallback? onDismiss,
  73. String? tag,
  74. Color? maskColor,
  75. bool? clickMaskDismiss,
  76. bool? backDismiss,
  77. bool? keepSingle,
  78. bool usePenetrate = false,
  79. }) {
  80. Alignment alignment;
  81. if (position == DialogPosition.bottom) {
  82. alignment = Alignment.bottomCenter;
  83. } else if (position == DialogPosition.top) {
  84. alignment = Alignment.topCenter;
  85. } else {
  86. alignment = Alignment.center;
  87. }
  88. SmartAnimationType? smartAnimationType;
  89. if (animType == DialogAnimation.fade) {
  90. smartAnimationType = SmartAnimationType.fade;
  91. } else if (animType == DialogAnimation.scale) {
  92. smartAnimationType = SmartAnimationType.scale;
  93. } else if (animType == DialogAnimation.centerFade_otherSlide) {
  94. smartAnimationType = SmartAnimationType.centerFade_otherSlide;
  95. } else if (animType == DialogAnimation.centerScale_otherSlide) {
  96. smartAnimationType = SmartAnimationType.centerScale_otherSlide;
  97. }
  98. return SmartDialog.show(
  99. builder: (context) => widget,
  100. alignment: alignment,
  101. usePenetrate: usePenetrate,
  102. debounce: true,
  103. clickMaskDismiss: clickMaskDismiss,
  104. backDismiss: backDismiss,
  105. animationType: smartAnimationType,
  106. onDismiss: onDismiss,
  107. tag: tag,
  108. maskColor: maskColor,
  109. keepSingle: keepSingle,
  110. );
  111. }
  112. // 隐藏
  113. static void dismiss({String? tag}) {
  114. SmartDialog.dismiss(status: SmartStatus.dialog, tag: tag);
  115. }
  116. //检查是否已经显示弹窗
  117. static bool checkIsExist({String? tag}) {
  118. return SmartDialog.checkExist(tag: tag);
  119. }
  120. }