dialog_engine.dart 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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. builder: (context) => widget,
  54. usePenetrate: usePenetrate,
  55. debounce: true,
  56. clickMaskDismiss: clickMaskDismiss,
  57. backDismiss: backDismiss,
  58. animationType: smartAnimType,
  59. alignment: alignment,
  60. onDismiss: onDismiss,
  61. tag: tag,
  62. maskColor: maskColor,
  63. keepSingle: keepSingle,
  64. );
  65. }
  66. /// 展示默认弹窗
  67. static Future show({
  68. required Widget widget,
  69. DialogPosition position = DialogPosition.center,
  70. DialogAnimation? animType,
  71. VoidCallback? onDismiss,
  72. String? tag,
  73. Color? maskColor,
  74. bool? clickMaskDismiss,
  75. bool? backDismiss,
  76. bool? keepSingle,
  77. bool usePenetrate = false,
  78. }) {
  79. Alignment alignment;
  80. if (position == DialogPosition.bottom) {
  81. alignment = Alignment.bottomCenter;
  82. } else if (position == DialogPosition.top) {
  83. alignment = Alignment.topCenter;
  84. } else {
  85. alignment = Alignment.center;
  86. }
  87. SmartAnimationType? smartAnimationType;
  88. if (animType == DialogAnimation.fade) {
  89. smartAnimationType = SmartAnimationType.fade;
  90. } else if (animType == DialogAnimation.scale) {
  91. smartAnimationType = SmartAnimationType.scale;
  92. } else if (animType == DialogAnimation.centerFade_otherSlide) {
  93. smartAnimationType = SmartAnimationType.centerFade_otherSlide;
  94. } else if (animType == DialogAnimation.centerScale_otherSlide) {
  95. smartAnimationType = SmartAnimationType.centerScale_otherSlide;
  96. }
  97. return SmartDialog.show(
  98. builder: (context) => widget,
  99. alignment: alignment,
  100. usePenetrate: usePenetrate,
  101. debounce: true,
  102. clickMaskDismiss: clickMaskDismiss,
  103. backDismiss: backDismiss,
  104. animationType: smartAnimationType,
  105. onDismiss: onDismiss,
  106. tag: tag,
  107. maskColor: maskColor,
  108. keepSingle: keepSingle,
  109. );
  110. }
  111. // 隐藏
  112. static void dismiss({String? tag}) {
  113. SmartDialog.dismiss(status: SmartStatus.dialog, tag: tag);
  114. }
  115. //检查是否已经显示弹窗
  116. static bool checkIsExist({String? tag}) {
  117. return SmartDialog.checkExist(tag: tag);
  118. }
  119. }