dialog_engine.dart 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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,
  28. bool? clickMaskDismiss,
  29. bool? backDismiss,
  30. bool? keepSingle,
  31. }) {
  32. Alignment alignment;
  33. if (position == DialogPosition.bottom) {
  34. alignment = Alignment.bottomCenter;
  35. } else if (position == DialogPosition.top) {
  36. alignment = Alignment.topCenter;
  37. } else {
  38. alignment = Alignment.center;
  39. }
  40. SmartAnimationType? smartAnimType;
  41. if (animType == DialogAnimation.fade) {
  42. smartAnimType = SmartAnimationType.fade;
  43. } else if (animType == DialogAnimation.scale) {
  44. smartAnimType = SmartAnimationType.scale;
  45. } else if (animType == DialogAnimation.centerFade_otherSlide) {
  46. smartAnimType = SmartAnimationType.centerFade_otherSlide;
  47. } else if (animType == DialogAnimation.centerScale_otherSlide) {
  48. smartAnimType = SmartAnimationType.centerScale_otherSlide;
  49. }
  50. return SmartDialog.showAttach(
  51. targetContext: targetContext,
  52. builder: (context) => widget,
  53. usePenetrate: false,
  54. debounce: true,
  55. clickMaskDismiss: clickMaskDismiss,
  56. backDismiss: backDismiss,
  57. animationType: smartAnimType,
  58. alignment: alignment,
  59. onDismiss: onDismiss,
  60. tag: tag,
  61. maskColor: Colors.transparent,
  62. keepSingle: keepSingle,
  63. );
  64. }
  65. /// 展示默认弹窗
  66. static Future show({
  67. required Widget widget,
  68. DialogPosition position = DialogPosition.center,
  69. DialogAnimation? animType,
  70. VoidCallback? onDismiss,
  71. String? tag,
  72. Color? maskColor,
  73. bool? clickMaskDismiss,
  74. bool? backDismiss,
  75. bool? keepSingle,
  76. }) {
  77. Alignment alignment;
  78. if (position == DialogPosition.bottom) {
  79. alignment = Alignment.bottomCenter;
  80. } else if (position == DialogPosition.top) {
  81. alignment = Alignment.topCenter;
  82. } else {
  83. alignment = Alignment.center;
  84. }
  85. SmartAnimationType? smartAnimationType;
  86. if (animType == DialogAnimation.fade) {
  87. smartAnimationType = SmartAnimationType.fade;
  88. } else if (animType == DialogAnimation.scale) {
  89. smartAnimationType = SmartAnimationType.scale;
  90. } else if (animType == DialogAnimation.centerFade_otherSlide) {
  91. smartAnimationType = SmartAnimationType.centerFade_otherSlide;
  92. } else if (animType == DialogAnimation.centerScale_otherSlide) {
  93. smartAnimationType = SmartAnimationType.centerScale_otherSlide;
  94. }
  95. return SmartDialog.show(
  96. builder: (context) => widget,
  97. alignment: alignment,
  98. usePenetrate: false,
  99. debounce: true,
  100. clickMaskDismiss: clickMaskDismiss,
  101. backDismiss: backDismiss,
  102. animationType: smartAnimationType,
  103. onDismiss: onDismiss,
  104. tag: tag,
  105. maskColor: maskColor,
  106. keepSingle: keepSingle,
  107. );
  108. }
  109. // 隐藏
  110. static void dismiss({String? tag}) {
  111. SmartDialog.dismiss(status: SmartStatus.dialog, tag: tag);
  112. }
  113. }